FiberhomePonOctetsCommand.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 FiberhomePonOctetsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('fiberhome: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. $key_olt_pon_bandwidth = "olt_bandwidth_pons_{$this->d_s}";
  37. $saveHistoric = (int) $input->getOption('save-historic');
  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. //counter64
  50. $inOctets = $this->getSNMP("ifHCInOctets","portInOctets");
  51. $outOctets = $this->getSNMP("ifHCOutOctets","portOutOctets");
  52. $sendData = array();
  53. $subId = $this->d_s;
  54. $t1 = time();
  55. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  56. foreach($dataCached as $index => $pon) {
  57. $ponPort = "{$pon['slot']}.{$pon['port']}";
  58. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  59. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  60. if(isset($bandwidthCached[$index])) {
  61. $t0 = $bandwidthCached[$index]['t'];
  62. $in0 = $bandwidthCached[$index]['inOct'];
  63. $out0 = $bandwidthCached[$index]['outOct'];
  64. $inAcc = $bandwidthCached[$index]['inAcc'];
  65. $outAcc = $bandwidthCached[$index]['outAcc'];
  66. $inBandwidth = $outBandwidth = 0;
  67. if(($in1 >= $in0) && ($t1 > $t0)) {
  68. $diff = $in1 - $in0;
  69. $inAcc += $diff;
  70. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  71. }
  72. if(($out1 >= $out0) && ($t1 > $t0)) {
  73. $diff = $out1 - $out0;
  74. $outAcc += $diff;
  75. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  76. }
  77. $totalIn += $inBandwidth;
  78. $totalOut += $outBandwidth;
  79. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  80. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  81. if(date("d",$t0) != date("d",$t1)) {
  82. $inAcc = $outAcc = 0;
  83. }
  84. $totalConsOut += $outAcc;
  85. $totalConsIn += $inAcc;
  86. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  87. } else {
  88. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  89. }
  90. }
  91. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  92. $oltBandwidth = array();
  93. $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
  94. $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
  95. $oltConsumption = array();
  96. $oltConsumption["{$subId}_inconsumption_olt"] = "{$totalConsIn}|g";
  97. $oltConsumption["{$subId}_outconsumption_olt"] = "{$totalConsOut}|g";
  98. $statsdService = $this->getContainer()->get('statsd');
  99. $statsdService->send($oltBandwidth);
  100. $statsdService->send($oltConsumption);
  101. if($sendData && $saveHistoric) {
  102. $t_start_script = microtime(true);
  103. $statsdService->send($sendData);
  104. $t_end_script = microtime(true);
  105. $time = $t_end_script - $t_start_script;
  106. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  107. }
  108. /* Fin de bloqueo */
  109. $this->removeLock($this->flag);
  110. $fin = microtime(true);
  111. $time = $fin - $inicio;
  112. $this->output->writeln("Tiempo: $time segundos");
  113. }
  114. }