HuaweiOnuTemperatureCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace HuaweiBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use HuaweiBundle\SNMP\SNMP as SNMP;
  8. //use RedisBundle\Services\RedisService;
  9. class HuaweiOnuTemperatureCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('huawei:onu:temperature')
  15. ->setDescription('Temperature de ONUs')
  16. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  17. ->setDefinition(array(
  18. new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT",1),
  19. new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT",1),
  20. new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
  21. new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
  22. new InputOption('olt-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP")
  23. ))
  24. ;
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. */
  30. protected function execute(InputInterface $input, OutputInterface $output)
  31. {
  32. parent::execute($input, $output);
  33. $this->output->writeln("INICIO");
  34. $inicio = microtime(true);
  35. $time = time();
  36. $oltDeviceId = (int) $input->getOption('olt-device-id');
  37. $oltServerId = (int) $input->getOption('olt-server-id');
  38. $oltIp = $input->getOption('olt-ip');
  39. $oltCommunity = $input->getOption('olt-community');
  40. $oltSnmpLibrary = $input->getOption('olt-snmp-library');
  41. $flag = "olt_onus_temperature_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  42. $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}";
  43. $key_onu_temperature = "onu_temperature_d_{$oltDeviceId}_s_{$oltServerId}";
  44. /* Control de bloqueo */
  45. if($this->lock($flag)) {return;}
  46. $SNMP = new SNMP($oltIp, $oltCommunity);
  47. $library = "use".$oltSnmpLibrary;
  48. $dataCached = $this->getData($key_olt_scan, true);
  49. $temperatureCached = array(); //$this->getData($key_onu_temperature, true);
  50. if(empty($dataCached)) {
  51. $this->output->writeln("Se requiere {$key_olt_scan}.");
  52. $this->removeLock($flag);
  53. return true;
  54. }
  55. $temperature = $SNMP->$library()->onuPonOpticalTemperature();
  56. $sendData = array();
  57. $metric = "onu_temperature_";
  58. $line = 0;
  59. foreach($temperature as $index => $temp) {
  60. if(isset($dataCached[$index])) {
  61. $value = 0.01 * $temp;
  62. $sn = strtolower($dataCached[$index]['serialNumber']);
  63. $k = $metric.strtolower($sn);
  64. //print_r("$line - $index - $k - $value".PHP_EOL);
  65. $sendData[$k] = "{$value}|g";
  66. $line++;
  67. $temperatureCached[$sn] = $value;
  68. }
  69. }
  70. $this->setData($key_onu_temperature, $temperatureCached, true);
  71. if($sendData) {
  72. $t_start_script = microtime(true);
  73. $statsdService = $this->getContainer()->get('statsd');
  74. $statsdService->send($sendData);
  75. $t_end_script = microtime(true);
  76. $time = $t_end_script - $t_start_script;
  77. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: {$line}".PHP_EOL);
  78. }
  79. //$this->collector->set($key,$dataCached,true);
  80. /* Fin de bloqueo */
  81. $this->removeLock($flag);
  82. }
  83. }