ZtePonStatsCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace ZteBundle\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 ZteBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class ZtePonStatsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('zte: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. $realIndex = $index;
  59. if(isset($pon['nexo'])) {
  60. $realIndex = $pon['nexo'];
  61. }
  62. $ponPort = str_replace ('/','.',$pon['ponPort']);
  63. $stats = array();
  64. $stats['ponPort'] = $pon['ponPort'];
  65. $stats['rxPower'] = array();
  66. foreach($metrics as $data => $metric) {
  67. if(isset($$data[$realIndex]) && !empty($$data[$realIndex])) {
  68. $m = "{$metric}{$ponPort}";
  69. $v = $$data[$realIndex] * 0.001;
  70. $stats[$data] = $v;
  71. $sendData[$m] = "{$v}|g";
  72. }
  73. }
  74. $ponStatsCached[$index] = $stats;
  75. }
  76. foreach($rxPower as $index => $rx) {
  77. $ponPortOnuId = explode(".",$index);
  78. if(count($ponPortOnuId) != 2) continue;
  79. $indexPon = $ponPortOnuId[0];
  80. if(isset($dataCached[$indexPon])) {
  81. $onuId = $ponPortOnuId[1];
  82. $ponPort = str_replace ('/','.',$dataCached[$indexPon]['ponPort']);
  83. $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
  84. $v = $rx * 0.001;
  85. if(isset($ponStatsCached[$indexPon]['rxPower'])) {
  86. $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
  87. }
  88. if(isset($dataCached[$indexPon]['nexo'])) {
  89. $index = $dataCached[$indexPon]['nexo'];
  90. if(isset($ponStatsCached[$index]['rxPower'])) {
  91. $ponStatsCached[$index]['rxPower'][$onuId] = $v;
  92. }
  93. }
  94. $sendData[$m] = "{$v}|g";
  95. }
  96. }
  97. if($ponStatsCached) {
  98. $key_pon_stats = "olt_stats_pons_{$this->d_s}";
  99. $this->setData($key_pon_stats, $ponStatsCached, true);
  100. }
  101. if($sendData && $saveHistoric) {
  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($this->flag);
  111. $fin = microtime(true);
  112. $time = $fin - $inicio;
  113. $this->output->writeln("Tiempo: $time segundos");
  114. }
  115. }