HuaweiPonStatsCommand.php 5.5 KB

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