HuaweiPonStatsCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace HuaweiBundle\Command;
  3. use BaseStatsBundle\Command\BaseCommand;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use HuaweiBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class HuaweiPonStatsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('huawei:pon:stats')
  16. ->setDescription('Stats de PON PORTs de OLT')
  17. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  18. ->setDefinition(array(
  19. new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT",1),
  20. new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT",1),
  21. new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
  22. new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
  23. new InputOption('olt-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
  24. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  25. ))
  26. ;
  27. }
  28. /**
  29. * @param InputInterface $input
  30. * @param OutputInterface $output
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. parent::execute($input, $output);
  35. $key_olt_scan = "olt_scan_pons_{$this->d_s}";
  36. $saveHistoric = (int) $input->getOption('save-historic');
  37. $inicio = microtime(true);
  38. $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
  39. $library = "use".$this->oltSnmpLibrary;
  40. $this->apiSNMP = $SNMP->$library();
  41. $dataCached = $this->getData($key_olt_scan, true);
  42. if(empty($dataCached)) {
  43. $this->output->writeln("Se requiere {$key_olt_scan}.");
  44. $this->removeLock($this->flag);
  45. return true;
  46. }
  47. //return [PONINDEX.ONUID] => value
  48. $rxPower = $this->getSNMP("oltPonRxOpticalPower","ponRxPower");
  49. //return [PONINDEX] => value
  50. $txPower = $this->getSNMP("oltPonTxOpticalPower","ponTxPower");
  51. $temperature = $this->getSNMP("oltPonOpticalTemperature","ponTemperature");
  52. $voltage = $this->getSNMP("oltPonOpticalVltage","ponVoltage");
  53. $biasCurrent = $this->getSNMP("oltPonOpticalCurrent","ponBiasCurrent");
  54. $ponStatsCached = $sendData = array();
  55. $subId = $this->d_s;
  56. $metrics = array("txPower" => "{$subId}_pon_tx_", "temperature" => "{$subId}_pon_temperature_", "voltage" => "{$subId}_pon_voltage_", "biasCurrent" => "{$subId}_pon_biascurrent_");
  57. foreach($dataCached as $index => $pon) {
  58. $ponPort = str_replace ('/','.',$pon['ponPort']);
  59. $stats = array();
  60. $stats['ponPort'] = $pon['ponPort'];
  61. $stats['rxPower'] = array();
  62. foreach($metrics as $data => $metric) {
  63. if(isset($$data[$index]) && !empty($$data[$index])) {
  64. $m = "{$metric}{$ponPort}";
  65. if($data == "voltage" || $data == "txPower") {
  66. $v = $$data[$index] * 0.01;
  67. } else {
  68. $v = $$data[$index];
  69. }
  70. $stats[$data] = $v;
  71. $sendData[$m] = "{$v}|g";
  72. }
  73. }
  74. $ponStatsCached[$index] = $stats;
  75. }
  76. foreach($rxPower as $index => $rx) {
  77. if($rx == 2147483647) continue;
  78. $ponPortOnuId = explode(".",$index);
  79. if(count($ponPortOnuId) != 2) continue;
  80. $indexPon = $ponPortOnuId[0];
  81. if(isset($dataCached[$indexPon])) {
  82. $onuId = $ponPortOnuId[1];
  83. $ponPort = str_replace ('/','.',$dataCached[$indexPon]['ponPort']);
  84. $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
  85. $v = ($rx - 10000) * 0.01;
  86. if(isset($ponStatsCached[$indexPon]['rxPower'])) {
  87. $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
  88. }
  89. $sendData[$m] = "{$v}|g";
  90. }
  91. }
  92. if($ponStatsCached) {
  93. $key_pon_stats = "olt_stats_pons_{$this->d_s}";
  94. $this->setData($key_pon_stats, $ponStatsCached, true);
  95. }
  96. if($sendData && $saveHistoric) {
  97. $t_start_script = microtime(true);
  98. $statsdService = $this->getContainer()->get('statsd');
  99. $statsdService->send($sendData);
  100. $t_end_script = microtime(true);
  101. $time = $t_end_script - $t_start_script;
  102. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  103. }
  104. /* Fin de bloqueo */
  105. $this->removeLock($this->flag);
  106. $fin = microtime(true);
  107. $time = $fin - $inicio;
  108. $this->output->writeln("Tiempo: $time segundos");
  109. }
  110. }