FurukawaOnuStatsCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace FurukawaBundle\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 FurukawaBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class FurukawaOnuStatsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('furukawa: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. $catvRxPower = $this->getSNMP("onuCatvRxOpticalPower","onuCatvRxPower");
  52. $status = $txPower;
  53. $stats = $sendData = array();
  54. $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_", "catvRxPower" => "onu_catvrx_");
  55. foreach($metrics as $m => $metric) {
  56. $stats[$metric] = array();
  57. }
  58. foreach($dataCached as $index => $onu) {
  59. $sn = strtolower($onu['serialNumber']);
  60. foreach($metrics as $data => $metric) {
  61. if(isset($$data[$index]) && !empty($$data[$index])) {
  62. $m = "{$metric}{$sn}";
  63. if($$data[$index] == 2147483647 && $data != "status") continue;
  64. if($data == "voltage") {
  65. $v = $$data[$index] * 0.001;
  66. } elseif ($data == "temperature") {
  67. $v = $$data[$index];
  68. } elseif ($data == "status") {
  69. ($$data[$index] == 2147483647)? $v = 0 : $v = 1;
  70. } else {
  71. $v = $$data[$index] * 0.01;
  72. }
  73. $sendData[$m] = "{$v}|g";
  74. $stats[$metric][$sn] = $v;
  75. }
  76. }
  77. }
  78. foreach($stats as $metric => $data) {
  79. $key_onu_stats = "{$metric}{$this->d_s}";
  80. $this->setData($key_onu_stats, $data, true);
  81. }
  82. if($sendData && $saveHistoric) {
  83. $t_start_script = microtime(true);
  84. $statsdService = $this->getContainer()->get('statsd');
  85. $statsdService->send($sendData);
  86. $t_end_script = microtime(true);
  87. $time = $t_end_script - $t_start_script;
  88. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  89. }
  90. /* Fin de bloqueo */
  91. $this->removeLock($this->flag);
  92. $fin = microtime(true);
  93. $time = $fin - $inicio;
  94. $this->output->writeln("Tiempo: $time segundos");
  95. }
  96. }