HuaweiPonOctetsCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ))
  24. ;
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. */
  30. protected function execute(InputInterface $input, OutputInterface $output)
  31. {
  32. parent::execute($input, $output);
  33. $this->output->writeln("INICIO");
  34. $inicio = microtime(true);
  35. $oltDeviceId = (int) $input->getOption('olt-device-id');
  36. $oltServerId = (int) $input->getOption('olt-server-id');
  37. $oltIp = $input->getOption('olt-ip');
  38. $oltCommunity = $input->getOption('olt-community');
  39. $oltSnmpLibrary = $input->getOption('olt-snmp-library');
  40. $flag = "olt_pon_octets_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  41. $key_olt_scan = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  42. $key_olt_pon_bandwidth = "olt_bandwidth_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  43. /* Control de bloqueo */
  44. if($this->lock($flag)) {return;}
  45. $SNMP = new SNMP($oltIp, $oltCommunity);
  46. $library = "use".$oltSnmpLibrary;
  47. $dataCached = $this->getData($key_olt_scan, true);
  48. $bandwidthCached = $this->getData($key_olt_pon_bandwidth, true);
  49. if(empty($dataCached)) {
  50. $this->output->writeln("Se requiere {$key_olt_scan}.");
  51. $this->removeLock($flag);
  52. return true;
  53. }
  54. //counter32
  55. $inOctets = $SNMP->$library()->ifInOctets();
  56. $outOctets = $SNMP->$library()->ifOutOctets();
  57. $sendData = array();
  58. $subId = "d_{$oltDeviceId}_s_{$oltServerId}";
  59. $t1 = time();
  60. foreach($dataCached as $index => $pon) {
  61. $ponPort = str_replace("/",".",$pon['ponPort']);
  62. //foreach($metrics as $data => $metric) {
  63. //if(isset($$data[$index]) && !empty($$data[$index])) {
  64. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  65. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  66. if(isset($bandwidthCached[$index])) {
  67. $t0 = $bandwidthCached[$index]['t'];
  68. $in0 = $bandwidthCached[$index]['inOct'];
  69. $out0 = $bandwidthCached[$index]['outOct'];
  70. $inAcc = $bandwidthCached[$index]['inAcc'];
  71. $outAcc = $bandwidthCached[$index]['outAcc'];
  72. $inBandwidth = $outBandwidth = 0;
  73. if(($in1 >= $in0) && ($t1 > $t0)) {
  74. $diff = $in1 - $in0;
  75. $inAcc += $diff;
  76. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  77. }
  78. if(($out1 >= $out0) && ($t1 > $t0)) {
  79. $diff = $out1 - $out0;
  80. $outAcc += $diff;
  81. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  82. }
  83. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  84. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  85. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  86. } else {
  87. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $in1, 'outAcc' => $out1, 'inBand' => 0, 'outBand' => 0);
  88. }
  89. }
  90. //print_r($bandwidthCached);
  91. //$bandwidthCached = array();
  92. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  93. if($sendData) {
  94. $t_start_script = microtime(true);
  95. $statsdService = $this->getContainer()->get('statsd');
  96. $statsdService->send($sendData);
  97. $t_end_script = microtime(true);
  98. $time = $t_end_script - $t_start_script;
  99. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  100. }
  101. /* Fin de bloqueo */
  102. $this->removeLock($flag);
  103. $fin = microtime(true);
  104. $time = $fin - $inicio;
  105. $this->output->writeln("Tiempo: $time segundos");
  106. }
  107. }