FiberhomeOnuStatusCommand.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 FiberhomeOnuStatusCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('fiberhome:onu:status')
  16. ->setDescription('Status 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_status = "onu_status_{$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 = $statusCached = array(); //$this->getData($key_onu_status, true);
  45. if(empty($dataCached)) {
  46. $this->output->writeln("Se requiere {$key_olt_scan}.");
  47. $this->removeLock($this->flag);
  48. return true;
  49. }
  50. $status = $this->getSNMP("onuStatus","onuStatus");
  51. $metric = "onu_status_";
  52. $line = 0;
  53. foreach($status as $index => $s) {
  54. if(isset($dataCached[$index])) {
  55. ($s != 1)? $value = 0 : $value = 1;
  56. $sn = strtolower($dataCached[$index]['serialNumber']);
  57. $k = $metric.strtolower($sn);
  58. $sendData[$k] = "{$value}|g";
  59. $line++;
  60. $statusCached[$sn] = $value;
  61. }
  62. }
  63. $this->setData($key_onu_status, $statusCached, true);
  64. if($sendData && $saveHistoric) {
  65. $t_start_script = microtime(true);
  66. $statsdService = $this->getContainer()->get('statsd');
  67. $statsdService->send($sendData);
  68. $t_end_script = microtime(true);
  69. $time = $t_end_script - $t_start_script;
  70. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: {$line}".PHP_EOL);
  71. }
  72. /* Fin de bloqueo */
  73. $this->removeLock($this->flag);
  74. }
  75. }