HuaweiOnuTemperatureCommand.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace HuaweiBundle\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 HuaweiBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class HuaweiOnuTemperatureCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('huawei:onu:temperature')
  16. ->setDescription('Temperature de ONUs')
  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. $key_onu_temperature = "onu_temperature_{$this->d_s}";
  37. $saveHistoric = (int) $input->getOption('save-historic');
  38. $inicio = microtime(true);
  39. $time = time();
  40. $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
  41. $library = "use".$this->oltSnmpLibrary;
  42. $this->apiSNMP = $SNMP->$library();
  43. $dataCached = $this->getData($key_olt_scan, true);
  44. $sendData = $temperatureCached = array();
  45. if(empty($dataCached)) {
  46. $this->output->writeln("Se requiere {$key_olt_scan}.");
  47. $this->removeLock($this->flag);
  48. return true;
  49. }
  50. $temperature = $this->getSNMP("onuPonOpticalTemperature","onuTemperature");
  51. $metric = "onu_temperature_";
  52. $line = 0;
  53. foreach($temperature as $index => $temp) {
  54. if($temp == 2147483647) continue;
  55. if(isset($dataCached[$index])) {
  56. $value = $temp;
  57. $sn = strtolower($dataCached[$index]['serialNumber']);
  58. $k = $metric.strtolower($sn);
  59. //print_r("$line - $index - $k - $value".PHP_EOL);
  60. $sendData[$k] = "{$value}|g";
  61. $line++;
  62. $temperatureCached[$sn] = $value;
  63. }
  64. }
  65. $this->setData($key_onu_temperature, $temperatureCached, true);
  66. if($sendData && $saveHistoric) {
  67. $t_start_script = microtime(true);
  68. $statsdService = $this->getContainer()->get('statsd');
  69. $statsdService->send($sendData);
  70. $t_end_script = microtime(true);
  71. $time = $t_end_script - $t_start_script;
  72. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: {$line}".PHP_EOL);
  73. }
  74. /* Fin de bloqueo */
  75. $this->removeLock($this->flag);
  76. }
  77. }