FiberlinkPonOctetsCommand.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace FiberlinkBundle\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 FiberlinkBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class FiberlinkPonOctetsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('fiberlink:pon:octets')
  16. ->setDescription('Bandwidth PON PORTs de OLT')
  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_pons_{$this->d_s}";
  36. $saveHistoric = (int) $input->getOption('save-historic');
  37. $key_olt_pon_bandwidth = "olt_bandwidth_pons_{$this->d_s}";
  38. $inicio = microtime(true);
  39. $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
  40. $library = "use".$this->oltSnmpLibrary;
  41. $this->apiSNMP = $SNMP->$library();
  42. $dataCached = $this->getData($key_olt_scan, true);
  43. $bandwidthCached = $this->getData($key_olt_pon_bandwidth, true);
  44. if(empty($dataCached)) {
  45. $this->output->writeln("Se requiere {$key_olt_scan}.");
  46. $this->removeLock($this->flag);
  47. return true;
  48. }
  49. $ifDescr = $this->getSNMP("ifDescr","ifDescr");
  50. $inverse = $interfaces = array();
  51. foreach($ifDescr as $index => $d) {
  52. if (preg_match("/p/i", $d)) {
  53. $s = str_replace(array("p","P"," "),"",$d);
  54. $s_p = explode("/",trim($s));
  55. if(count($s_p) == 2) {
  56. $s = $s_p[0];
  57. $p = $s_p[1];
  58. $interfaces[$index] = array("ponPort"=>"{$s}.{$p}",'slot'=>$s,'port'=>$p);
  59. $inverse["{$s}.{$p}"] = $index;
  60. }
  61. }
  62. }
  63. //counter64
  64. $inOctets = $this->getSNMP("ifHCInOctets","portInOctets");
  65. $outOctets = $this->getSNMP("ifHCOutOctets","portOutOctets");
  66. $sendData = array();
  67. $subId = $this->d_s;
  68. $t1 = time();
  69. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  70. foreach($dataCached as $index => $pon) {
  71. $ponPort = $index;
  72. if(!isset($inverse[$index])) continue;
  73. $inverseIndex = $inverse[$index];
  74. //foreach($metrics as $data => $metric) {
  75. //if(isset($$data[$index]) && !empty($$data[$index])) {
  76. (isset($inOctets[$inverseIndex]))? $in1 = $inOctets[$inverseIndex] : $in1 = 0;
  77. (isset($outOctets[$inverseIndex]))? $out1 = $outOctets[$inverseIndex] : $out1 = 0;
  78. if(isset($bandwidthCached[$index])) {
  79. $t0 = $bandwidthCached[$index]['t'];
  80. $in0 = $bandwidthCached[$index]['inOct'];
  81. $out0 = $bandwidthCached[$index]['outOct'];
  82. $inAcc = $bandwidthCached[$index]['inAcc'];
  83. $outAcc = $bandwidthCached[$index]['outAcc'];
  84. $inBandwidth = $outBandwidth = 0;
  85. if(($in1 >= $in0) && ($t1 > $t0)) {
  86. $diff = $in1 - $in0;
  87. $inAcc += $diff;
  88. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  89. }
  90. if(($out1 >= $out0) && ($t1 > $t0)) {
  91. $diff = $out1 - $out0;
  92. $outAcc += $diff;
  93. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  94. }
  95. $totalIn += $inBandwidth;
  96. $totalOut += $outBandwidth;
  97. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  98. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  99. if(date("d",$t0) != date("d",$t1)) {
  100. $inAcc = $outAcc = 0;
  101. }
  102. $totalConsOut += $outAcc;
  103. $totalConsIn += $inAcc;
  104. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  105. } else {
  106. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  107. }
  108. }
  109. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  110. $oltBandwidth = array();
  111. $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
  112. $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
  113. $oltConsumption = array();
  114. $oltConsumption["{$subId}_inconsumption_olt"] = "{$totalConsIn}|g";
  115. $oltConsumption["{$subId}_outconsumption_olt"] = "{$totalConsOut}|g";
  116. $statsdService = $this->getContainer()->get('statsd');
  117. $statsdService->send($oltBandwidth);
  118. $statsdService->send($oltConsumption);
  119. if($sendData && $saveHistoric) {
  120. $t_start_script = microtime(true);
  121. $statsdService->send($sendData);
  122. $t_end_script = microtime(true);
  123. $time = $t_end_script - $t_start_script;
  124. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  125. }
  126. /* Fin de bloqueo */
  127. $this->removeLock($this->flag);
  128. $fin = microtime(true);
  129. $time = $fin - $inicio;
  130. $this->output->writeln("Tiempo: $time segundos");
  131. }
  132. }