FiberlinkPonOctetsCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 FiberlinkPonOctetsCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('fiberlink:pon:octets')
  15. ->setDescription('Bandwidth PON PORTs 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_pon_octets_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  43. $key_olt_scan = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  44. $key_olt_pon_bandwidth = "olt_bandwidth_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  45. /* Control de bloqueo */
  46. if($this->lock($flag)) {return;}
  47. $SNMP = new SNMP($oltIp, $oltCommunity);
  48. $library = "use".$oltSnmpLibrary;
  49. $dataCached = $this->getData($key_olt_scan, true);
  50. $bandwidthCached = $this->getData($key_olt_pon_bandwidth, true);
  51. if(empty($dataCached)) {
  52. $this->output->writeln("Se requiere {$key_olt_scan}.");
  53. $this->removeLock($flag);
  54. return true;
  55. }
  56. $ifDescr = $SNMP->$library()->ifDescr();
  57. $inverse = $interfaces = array();
  58. foreach($ifDescr as $index => $d) {
  59. if (preg_match("/p/i", $d)) {
  60. $s = str_replace(array("p","P"," "),"",$d);
  61. $s_p = explode("/",trim($s));
  62. if(count($s_p) == 2) {
  63. $s = $s_p[0];
  64. $p = $s_p[1];
  65. $interfaces[$index] = array("ponPort"=>"{$s}.{$p}",'slot'=>$s,'port'=>$p);
  66. $inverse["{$s}.{$p}"] = $index;
  67. }
  68. }
  69. }
  70. //counter64
  71. $inOctets = $SNMP->$library()->ifHCInOctets();
  72. $outOctets = $SNMP->$library()->ifHCOutOctets();
  73. $sendData = array();
  74. $subId = "d_{$oltDeviceId}_s_{$oltServerId}";
  75. $t1 = time();
  76. foreach($dataCached as $index => $pon) {
  77. $ponPort = $index;
  78. if(!isset($inverse[$index])) continue;
  79. $inverseIndex = $inverse[$index];
  80. //foreach($metrics as $data => $metric) {
  81. //if(isset($$data[$index]) && !empty($$data[$index])) {
  82. (isset($inOctets[$inverseIndex]))? $in1 = $inOctets[$inverseIndex] : $in1 = 0;
  83. (isset($outOctets[$inverseIndex]))? $out1 = $outOctets[$inverseIndex] : $out1 = 0;
  84. if(isset($bandwidthCached[$index])) {
  85. $t0 = $bandwidthCached[$index]['t'];
  86. $in0 = $bandwidthCached[$index]['inOct'];
  87. $out0 = $bandwidthCached[$index]['outOct'];
  88. $inAcc = $bandwidthCached[$index]['inAcc'];
  89. $outAcc = $bandwidthCached[$index]['outAcc'];
  90. $inBandwidth = $outBandwidth = 0;
  91. if(($in1 >= $in0) && ($t1 > $t0)) {
  92. $diff = $in1 - $in0;
  93. $inAcc += $diff;
  94. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  95. }
  96. if(($out1 >= $out0) && ($t1 > $t0)) {
  97. $diff = $out1 - $out0;
  98. $outAcc += $diff;
  99. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  100. }
  101. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  102. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  103. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  104. } else {
  105. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $in1, 'outAcc' => $out1, 'inBand' => 0, 'outBand' => 0);
  106. }
  107. }
  108. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  109. if($sendData && $saveHistoric) {
  110. $t_start_script = microtime(true);
  111. $statsdService = $this->getContainer()->get('statsd');
  112. $statsdService->send($sendData);
  113. $t_end_script = microtime(true);
  114. $time = $t_end_script - $t_start_script;
  115. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  116. }
  117. /* Fin de bloqueo */
  118. $this->removeLock($flag);
  119. $fin = microtime(true);
  120. $time = $fin - $inicio;
  121. $this->output->writeln("Tiempo: $time segundos");
  122. }
  123. }