HuaweiPonOctetsCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace HuaweiBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use HuaweiBundle\SNMP\SNMP as SNMP;
  8. //use RedisBundle\Services\RedisService;
  9. class HuaweiPonOctetsCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('huawei:pon:octets')
  15. ->setDescription('Bandwidth PON PORTs de OLT')
  16. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  17. ->setDefinition(array(
  18. new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT",1),
  19. new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT",1),
  20. new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
  21. new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
  22. new InputOption('olt-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
  23. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  24. ))
  25. ;
  26. }
  27. /**
  28. * @param InputInterface $input
  29. * @param OutputInterface $output
  30. */
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. parent::execute($input, $output);
  34. $this->output->writeln("INICIO");
  35. $inicio = microtime(true);
  36. $oltDeviceId = (int) $input->getOption('olt-device-id');
  37. $oltServerId = (int) $input->getOption('olt-server-id');
  38. $oltIp = $input->getOption('olt-ip');
  39. $oltCommunity = $input->getOption('olt-community');
  40. $oltSnmpLibrary = $input->getOption('olt-snmp-library');
  41. $saveHistoric = (int) $input->getOption('save-historic');
  42. $flag = "olt_pon_octets_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  43. $key_olt_scan = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  44. $key_olt_pon_bandwidth = "olt_bandwidth_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  45. /* Control de bloqueo */
  46. if($this->lock($flag)) {return;}
  47. $SNMP = new SNMP($oltIp, $oltCommunity);
  48. $library = "use".$oltSnmpLibrary;
  49. $dataCached = $this->getData($key_olt_scan, true);
  50. $bandwidthCached = $this->getData($key_olt_pon_bandwidth, true);
  51. if(empty($dataCached)) {
  52. $this->output->writeln("Se requiere {$key_olt_scan}.");
  53. $this->removeLock($flag);
  54. return true;
  55. }
  56. //counter32
  57. $inOctets = $SNMP->$library()->ifInOctets();
  58. $outOctets = $SNMP->$library()->ifOutOctets();
  59. $sendData = array();
  60. $subId = "d_{$oltDeviceId}_s_{$oltServerId}";
  61. $t1 = time();
  62. foreach($dataCached as $index => $pon) {
  63. $ponPort = str_replace("/",".",$pon['ponPort']);
  64. //foreach($metrics as $data => $metric) {
  65. //if(isset($$data[$index]) && !empty($$data[$index])) {
  66. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  67. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  68. if(isset($bandwidthCached[$index])) {
  69. $t0 = $bandwidthCached[$index]['t'];
  70. $in0 = $bandwidthCached[$index]['inOct'];
  71. $out0 = $bandwidthCached[$index]['outOct'];
  72. $inAcc = $bandwidthCached[$index]['inAcc'];
  73. $outAcc = $bandwidthCached[$index]['outAcc'];
  74. $inBandwidth = $outBandwidth = 0;
  75. if(($in1 >= $in0) && ($t1 > $t0)) {
  76. $diff = $in1 - $in0;
  77. $inAcc += $diff;
  78. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  79. }
  80. if(($out1 >= $out0) && ($t1 > $t0)) {
  81. $diff = $out1 - $out0;
  82. $outAcc += $diff;
  83. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  84. }
  85. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  86. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  87. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  88. } else {
  89. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $in1, 'outAcc' => $out1, 'inBand' => 0, 'outBand' => 0);
  90. }
  91. }
  92. //print_r($bandwidthCached);
  93. //$bandwidthCached = array();
  94. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  95. if($sendData && $saveHistoric) {
  96. $t_start_script = microtime(true);
  97. $statsdService = $this->getContainer()->get('statsd');
  98. $statsdService->send($sendData);
  99. $t_end_script = microtime(true);
  100. $time = $t_end_script - $t_start_script;
  101. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  102. }
  103. /* Fin de bloqueo */
  104. $this->removeLock($flag);
  105. $fin = microtime(true);
  106. $time = $fin - $inicio;
  107. $this->output->writeln("Tiempo: $time segundos");
  108. }
  109. }