HuaweiPonStatsCommand.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 HuaweiPonStatsCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('huawei:pon:stats')
  15. ->setDescription('Stats de 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_stats_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  43. $key_olt_scan = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  44. /* Control de bloqueo */
  45. if($this->lock($flag)) {return;}
  46. $SNMP = new SNMP($oltIp, $oltCommunity);
  47. $library = "use".$oltSnmpLibrary;
  48. $dataCached = $this->getData($key_olt_scan, true);
  49. if(empty($dataCached)) {
  50. $this->output->writeln("Se requiere {$key_olt_scan}.");
  51. $this->removeLock($flag);
  52. return true;
  53. }
  54. //return [PONINDEX.ONUID] => value
  55. $rxPower = $SNMP->$library()->oltPonRxOpticalPower();
  56. //return [PONINDEX] => value
  57. $txPower = $SNMP->$library()->oltPonTxOpticalPower();
  58. $temperature = $SNMP->$library()->oltPonOpticalTemperature();
  59. $voltage = $SNMP->$library()->oltPonOpticalVltage();
  60. $biasCurrent = $SNMP->$library()->oltPonOpticalCurrent();
  61. $ponStatsCached = $sendData = array();
  62. $subId = "d_{$oltDeviceId}_s_{$oltServerId}";
  63. $metrics = array("txPower" => "{$subId}_pon_tx_", "temperature" => "{$subId}_pon_temperature_", "voltage" => "{$subId}_pon_voltage_", "biasCurrent" => "{$subId}_pon_biascurrent_");
  64. foreach($dataCached as $index => $pon) {
  65. $ponPort = str_replace ('/','.',$pon['ponPort']);
  66. $stats = array();
  67. $stats['ponPort'] = $pon['ponPort'];
  68. $stats['rxPower'] = array();
  69. foreach($metrics as $data => $metric) {
  70. if(isset($$data[$index]) && !empty($$data[$index])) {
  71. $m = "{$metric}{$ponPort}";
  72. if($data == "voltage" || $data == "txPower") {
  73. $v = $$data[$index] * 0.01;
  74. } else {
  75. $v = $$data[$index];
  76. }
  77. $stats[$data] = $v;
  78. $sendData[$m] = "{$v}|g";
  79. }
  80. }
  81. $ponStatsCached[$index] = $stats;
  82. }
  83. foreach($rxPower as $index => $rx) {
  84. if($rx == 2147483647) continue;
  85. $ponPortOnuId = explode(".",$index);
  86. if(count($ponPortOnuId) != 2) continue;
  87. $indexPon = $ponPortOnuId[0];
  88. if(isset($dataCached[$indexPon])) {
  89. $onuId = $ponPortOnuId[1];
  90. $ponPort = str_replace ('/','.',$dataCached[$indexPon]['ponPort']);
  91. $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
  92. $v = ($rx - 10000) * 0.01;
  93. if(isset($ponStatsCached[$indexPon]['rxPower'])) {
  94. $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
  95. }
  96. $sendData[$m] = "{$v}|g";
  97. }
  98. }
  99. if($ponStatsCached) {
  100. $key_pon_stats = "olt_stats_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  101. $this->setData($key_pon_stats, $ponStatsCached, true);
  102. }
  103. if($sendData && $saveHistoric) {
  104. $t_start_script = microtime(true);
  105. $statsdService = $this->getContainer()->get('statsd');
  106. $statsdService->send($sendData);
  107. $t_end_script = microtime(true);
  108. $time = $t_end_script - $t_start_script;
  109. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  110. }
  111. /* Fin de bloqueo */
  112. $this->removeLock($flag);
  113. $fin = microtime(true);
  114. $time = $fin - $inicio;
  115. $this->output->writeln("Tiempo: $time segundos");
  116. }
  117. }