HuaweiOnuStatsCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 HuaweiOnuStatsCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('huawei: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. $catvRxPower = $SNMP->$library()->onuCatvRxOpticalPower();
  59. $status = $txPower;
  60. $sendData = array();
  61. $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_", "catvRxPower" => "onu_catvrx_");
  62. $stats = array();
  63. foreach($metrics as $m => $metric) {
  64. $stats[$metric] = array();
  65. }
  66. foreach($dataCached as $index => $onu) {
  67. $sn = strtolower($onu['serialNumber']);
  68. foreach($metrics as $data => $metric) {
  69. if(isset($$data[$index]) && !empty($$data[$index])) {
  70. $m = "{$metric}{$sn}";
  71. if($$data[$index] == 2147483647 && $data != "status") continue;
  72. if($data == "voltage") {
  73. $v = $$data[$index] * 0.001;
  74. } elseif ($data == "temperature") {
  75. $v = $$data[$index];
  76. } elseif ($data == "status") {
  77. ($$data[$index] == 2147483647)? $v = 0 : $v = 1;
  78. } else {
  79. $v = $$data[$index] * 0.01;
  80. }
  81. $sendData[$m] = "{$v}|g";
  82. $stats[$metric][$sn] = $v;
  83. }
  84. }
  85. }
  86. foreach($stats as $metric => $data) {
  87. $key_onu_stats = "{$metric}d_{$oltDeviceId}_s_{$oltServerId}";
  88. $this->setData($key_onu_stats, $data, true);
  89. }
  90. if($sendData && $saveHistoric) {
  91. $t_start_script = microtime(true);
  92. $statsdService = $this->getContainer()->get('statsd');
  93. $statsdService->send($sendData);
  94. $t_end_script = microtime(true);
  95. $time = $t_end_script - $t_start_script;
  96. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  97. }
  98. /* Fin de bloqueo */
  99. $this->removeLock($flag);
  100. $fin = microtime(true);
  101. $time = $fin - $inicio;
  102. $this->output->writeln("Tiempo: $time segundos");
  103. }
  104. }