FiberhomeOnuStatsCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 FiberhomeOnuStatsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('fiberhome:onu:stats')
  16. ->setDescription('Stats de ONUs 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_{$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. $txPower = $this->getSNMP("onuPonTxOpticalPower","onuTxPower");
  48. $rxPower = $this->getSNMP("onuPonRxOpticalPower","onuRxPower");
  49. $temperature = $this->getSNMP("onuPonOpticalTemperature","onuTemperature");
  50. $voltage = $this->getSNMP("onuPonOpticalVltage","onuVoltage");
  51. $status = $this->getSNMP("onuStatus","onuStatus");
  52. $stats = $sendData = array();
  53. $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_");
  54. foreach($metrics as $m => $metric) {
  55. $stats[$metric] = array();
  56. }
  57. foreach($dataCached as $index => $onu) {
  58. $sn = strtolower($onu['serialNumber']);
  59. foreach($metrics as $data => $metric) {
  60. if(isset($$data[$index]) && !empty($$data[$index])) {
  61. $m = "{$metric}{$sn}";
  62. if($data != "status") {
  63. $v = $$data[$index] * 0.01;
  64. } else {
  65. ($$data[$index] != 1)? $v = 0 : $v = 1;
  66. }
  67. $sendData[$m] = "{$v}|g";
  68. $stats[$metric][$sn] = $v;
  69. }
  70. }
  71. }
  72. foreach($stats as $metric => $data) {
  73. $key_onu_stats = "{$metric}{$this->d_s}";
  74. $this->setData($key_onu_stats, $data, true);
  75. }
  76. if($sendData && $saveHistoric) {
  77. $t_start_script = microtime(true);
  78. $statsdService = $this->getContainer()->get('statsd');
  79. $statsdService->send($sendData);
  80. $t_end_script = microtime(true);
  81. $time = $t_end_script - $t_start_script;
  82. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  83. }
  84. /* Fin de bloqueo */
  85. $this->removeLock($this->flag);
  86. $fin = microtime(true);
  87. $time = $fin - $inicio;
  88. $this->output->writeln("Tiempo: $time segundos");
  89. }
  90. }