FiberlinkOnuStatsCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace FiberlinkBundle\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 FiberlinkBundle\SNMP\SNMP as SNMP;
  8. //use RedisBundle\Services\RedisService;
  9. class FiberlinkOnuStatsCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('fiberlink:onu:stats')
  15. ->setDescription('Stats de ONUs de OLT')
  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. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  24. ))
  25. ;
  26. }
  27. /**
  28. * @param InputInterface $input
  29. * @param OutputInterface $output
  30. */
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. parent::execute($input, $output);
  34. $this->output->writeln("INICIO");
  35. $inicio = microtime(true);
  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. $saveHistoric = (int) $input->getOption('save-historic');
  42. $flag = "olt_onu_stats_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  43. $key_olt_scan = "olt_scan_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. if(empty($dataCached)) {
  50. $this->output->writeln("Se requiere {$key_olt_scan}.");
  51. $this->removeLock($flag);
  52. return true;
  53. }
  54. $txPower = $SNMP->$library()->onuPonTxOpticalPower();
  55. $rxPower = $SNMP->$library()->onuPonRxOpticalPower();
  56. $temperature = $SNMP->$library()->onuPonOpticalTemperature();
  57. $voltage = $SNMP->$library()->onuPonOpticalVltage();
  58. $status = $SNMP->$library()->onuStatus();
  59. $uptime = $SNMP->$library()->onuUptime();
  60. /* print_r($txPower);
  61. print_r($rxPower);
  62. print_r($temperature);
  63. print_r($voltage);
  64. print_r($status); */
  65. $sendData = array();
  66. $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_", "uptime" => "onu_uptime");
  67. $stats = array();
  68. foreach($metrics as $m => $metric) {
  69. $stats[$metric] = array();
  70. }
  71. foreach($dataCached as $index => $onu) {
  72. $sn = strtolower($onu['serialNumber']);
  73. foreach($metrics as $data => $metric) {
  74. if(isset($$data[$index]) && !empty($$data[$index])) {
  75. $m = "{$metric}{$sn}";
  76. $v = $$data[$index];
  77. $sendData[$m] = "{$v}|g";
  78. $stats[$metric][$sn] = $v;
  79. }
  80. }
  81. }
  82. foreach($stats as $metric => $data) {
  83. $key_onu_stats = "{$metric}d_{$oltDeviceId}_s_{$oltServerId}";
  84. $this->setData($key_onu_stats, $data, true);
  85. }
  86. if($sendData && $saveHistoric) {
  87. $t_start_script = microtime(true);
  88. $statsdService = $this->getContainer()->get('statsd');
  89. $statsdService->send($sendData);
  90. $t_end_script = microtime(true);
  91. $time = $t_end_script - $t_start_script;
  92. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  93. }
  94. /* Fin de bloqueo */
  95. $this->removeLock($flag);
  96. $fin = microtime(true);
  97. $time = $fin - $inicio;
  98. $this->output->writeln("Tiempo: $time segundos");
  99. }
  100. }