ZtePonOctetsCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace ZteBundle\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 ZteBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class ZtePonOctetsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('zte: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. if(isset($pon['nexo'])) continue;
  58. $ponPort = str_replace("/",".",$pon['ponPort']);
  59. //foreach($metrics as $data => $metric) {
  60. //if(isset($$data[$index]) && !empty($$data[$index])) {
  61. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  62. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  63. if(isset($bandwidthCached[$index])) {
  64. $t0 = $bandwidthCached[$index]['t'];
  65. $in0 = $bandwidthCached[$index]['inOct'];
  66. $out0 = $bandwidthCached[$index]['outOct'];
  67. $inAcc = $bandwidthCached[$index]['inAcc'];
  68. $outAcc = $bandwidthCached[$index]['outAcc'];
  69. $inBandwidth = $outBandwidth = 0;
  70. if(($in1 >= $in0) && ($t1 > $t0)) {
  71. $diff = $in1 - $in0;
  72. $inAcc += $diff;
  73. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  74. }
  75. if(($out1 >= $out0) && ($t1 > $t0)) {
  76. $diff = $out1 - $out0;
  77. $outAcc += $diff;
  78. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  79. }
  80. $totalIn += $inBandwidth;
  81. $totalOut += $outBandwidth;
  82. $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
  83. $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
  84. if(date("d",$t0) != date("d",$t1)) {
  85. $inAcc = $outAcc = 0;
  86. }
  87. $totalConsOut += $outAcc;
  88. $totalConsIn += $inAcc;
  89. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  90. } else {
  91. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  92. }
  93. }
  94. $this->setData($key_olt_pon_bandwidth, $bandwidthCached, true);
  95. $oltBandwidth = array();
  96. $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
  97. $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
  98. $div = 1073741824; //bytes => giga
  99. $consIn = number_format(($totalConsIn / $div),3,'.','');
  100. $consOut = number_format(($totalConsOut / $div),3,'.','');
  101. $oltConsumption = array();
  102. $oltConsumption["{$subId}_inconsumption_olt"] = "{$consIn}|g";
  103. $oltConsumption["{$subId}_outconsumption_olt"] = "{$consOut}|g";
  104. $statsdService = $this->getContainer()->get('statsd');
  105. $statsdService->send($oltBandwidth);
  106. $statsdService->send($oltConsumption);
  107. if($sendData && $saveHistoric) {
  108. $t_start_script = microtime(true);
  109. $statsdService->send($sendData);
  110. $t_end_script = microtime(true);
  111. $time = $t_end_script - $t_start_script;
  112. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  113. }
  114. /* Fin de bloqueo */
  115. $this->removeLock($this->flag);
  116. $fin = microtime(true);
  117. $time = $fin - $inicio;
  118. $this->output->writeln("Tiempo: $time segundos");
  119. }
  120. }