HuaweiOnuOctetsCommand.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace HuaweiBundle\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 HuaweiBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class HuaweiOnuOctetsCommand extends BaseCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('huawei:onu:octets')
  16. ->setDescription('Bandwidth ONTs 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_{$this->d_s}";
  36. $key_olt_onu_bandwidth = "olt_bandwidth_onu_{$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_onu_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("onuInOctets","onuInOctets");
  51. $outOctets = $this->getSNMP("onuOutOctets","onuOutOctets");
  52. $consumptionData = $sendData = array();
  53. $subId = $this->d_s;
  54. $t1 = time();
  55. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  56. foreach($dataCached as $index => $onu) {
  57. $inDiff = $outDiff = 0;
  58. $sn = $onu["serialNumber"];
  59. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  60. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  61. if(isset($bandwidthCached[$index])) {
  62. $t0 = $bandwidthCached[$index]['t'];
  63. $in0 = $bandwidthCached[$index]['inOct'];
  64. $out0 = $bandwidthCached[$index]['outOct'];
  65. $inAcc = $bandwidthCached[$index]['inAcc'];
  66. $outAcc = $bandwidthCached[$index]['outAcc'];
  67. $inBandwidth = $outBandwidth = 0;
  68. if(($in1 >= $in0) && ($t1 > $t0)) {
  69. $inDiff = $diff = $in1 - $in0;
  70. $inAcc += $diff;
  71. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  72. }
  73. if(($out1 >= $out0) && ($t1 > $t0)) {
  74. $outDiff = $diff = $out1 - $out0;
  75. $outAcc += $diff;
  76. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  77. }
  78. $totalIn += $inBandwidth;
  79. $totalOut += $outBandwidth;
  80. $sendData["inbandwidth_onu_{$sn}"] = "{$inBandwidth}|g";
  81. $sendData["outbandwidth_onu_{$sn}"] = "{$outBandwidth}|g";
  82. $div = 1073741824; //bytes => giga
  83. $consIn = number_format(($inAcc / $div),3,'.','');
  84. $consOut = number_format(($outAcc / $div),3,'.','');
  85. $sendData["inconsumption_onu_{$sn}"] = "{$consIn}|g";
  86. $sendData["outconsumption_onu_{$sn}"] = "{$consOut}|g";
  87. $div = 1073741824; //bytes => giga
  88. $inGB = number_format($inDiff / $div, 5,".","");
  89. $outGB = number_format($outDiff / $div, 5,".","");
  90. $server = $this->oltServerId;
  91. $fatherDevice = $this->oltDeviceId;
  92. $fatherDeviceType = 2;
  93. $device = $sn;
  94. $date = date("Y-m-d");
  95. $consumptionData[] = "({$server},{$fatherDevice}, {$fatherDeviceType},'{$device}','{$date}',{$inGB},{$outGB})";
  96. if(date("n",$t0) != date("n",$t1)) {
  97. $inAcc = $outAcc = 0;
  98. }
  99. $totalConsOut += $outAcc;
  100. $totalConsIn += $inAcc;
  101. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  102. } else {
  103. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  104. }
  105. }
  106. $this->setData($key_olt_onu_bandwidth, $bandwidthCached, true);
  107. $this->saveConsumption($consumptionData);
  108. if($sendData && $saveHistoric) {
  109. $t_start_script = microtime(true);
  110. $statsdService = $this->getContainer()->get('statsd');
  111. $statsdService->send($sendData);
  112. $t_end_script = microtime(true);
  113. $time = $t_end_script - $t_start_script;
  114. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  115. }
  116. /* Fin de bloqueo */
  117. $this->removeLock($this->flag);
  118. $fin = microtime(true);
  119. $time = $fin - $inicio;
  120. $this->output->writeln("Tiempo: $time segundos");
  121. }
  122. /**
  123. * @param array $data
  124. */
  125. function saveConsumption($data)
  126. {
  127. if ($data) {
  128. $query = "INSERT LOW_PRIORITY INTO `device_consumption` (`server_id`, `father_device_id`, `father_device_type`, `device`, `date`,`cons_in`,`cons_out`) VALUES " . implode(",", $data) . " ON DUPLICATE KEY UPDATE cons_out = cons_out + VALUES(cons_out), cons_in = cons_in + VALUES(cons_in);";
  129. $this->executeQueries(array($query));
  130. }
  131. }
  132. }