FiberhomePonStatsCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace FiberhomeBundle\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 FiberhomeBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class FiberhomePonStatsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('fiberhome: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. $txPower = $this->getSNMP("oltPonTxOpticalPower","ponTxPower");
  50. $temperature = $this->getSNMP("oltPonOpticalTemperature","ponTemperature");
  51. $voltage = $this->getSNMP("oltPonOpticalVltage","ponVoltage");
  52. $biasCurrent = $this->getSNMP("oltPonOpticalCurrent","ponBiasCurrent");
  53. $ponStatsCached = $sendData = array();
  54. $subId = $this->d_s;
  55. $metrics = array("txPower" => "{$subId}_pon_tx_", "temperature" => "{$subId}_pon_temperature_", "voltage" => "{$subId}_pon_voltage_", "biasCurrent" => "{$subId}_pon_biascurrent_");
  56. foreach($dataCached as $index => $pon) {
  57. $ponPort = "{$pon['slot']}.{$pon['port']}";
  58. $stats = array();
  59. $stats['ponPort'] = "{$pon['slot']}/{$pon['port']}";
  60. $stats['rxPower'] = array();
  61. foreach($metrics as $data => $metric) {
  62. if(isset($$data[$index]) && !empty($$data[$index])) {
  63. $m = "{$metric}{$ponPort}";
  64. $v = $$data[$index] * 0.01;
  65. $stats[$data] = $v;
  66. $sendData[$m] = "{$v}|g";
  67. }
  68. }
  69. $ponStatsCached[$index] = $stats;
  70. }
  71. foreach($rxPower as $index => $rx) {
  72. $ponPortOnuId = explode(".",$index);
  73. if(count($ponPortOnuId) != 2) continue;
  74. $indexPon = $ponPortOnuId[0];
  75. if(isset($dataCached[$indexPon])) {
  76. $onuId = $ponPortOnuId[1];
  77. $slot = $dataCached[$indexPon]['slot'];
  78. $port = $dataCached[$indexPon]['port'];
  79. $ponPort = "{$slot}.{$port}";
  80. $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
  81. $v = $rx * 0.01;
  82. if(isset($ponStatsCached[$indexPon]['rxPower'])) {
  83. $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
  84. }
  85. $sendData[$m] = "{$v}|g";
  86. }
  87. }
  88. if($ponStatsCached) {
  89. $key_pon_stats = "olt_stats_pons_{$this->d_s}";
  90. $this->setData($key_pon_stats, $ponStatsCached, true);
  91. }
  92. if($sendData && $saveHistoric) {
  93. $t_start_script = microtime(true);
  94. $statsdService = $this->getContainer()->get('statsd');
  95. $statsdService->send($sendData);
  96. $t_end_script = microtime(true);
  97. $time = $t_end_script - $t_start_script;
  98. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  99. }
  100. /* Fin de bloqueo */
  101. $this->removeLock($this->flag);
  102. $fin = microtime(true);
  103. $time = $fin - $inicio;
  104. $this->output->writeln("Tiempo: $time segundos");
  105. }
  106. }