NasOnuOctetsCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Flowdat\Stats\Command\NAS;
  3. use Doctrine\DBAL\DBALException;
  4. use Flowdat\Stats\App\Helper\RedisHelper;
  5. use Flowdat\Stats\App\Service\Doctrine\DoctrineService;
  6. use Flowdat\Stats\App\Service\SNMP\SNMPService;
  7. use Flowdat\Stats\App\Service\StatsD\StatsDService;
  8. use Flowdat\Stats\SNMP\NAS\SNMP;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class NasOnuOctetsCommand extends Command
  14. {
  15. protected $nasDeviceId;
  16. protected $nasServerId;
  17. protected $nasIp;
  18. protected $nasCommunity;
  19. protected $nasSnmpLibrary;
  20. protected $flag;
  21. protected $apiSNMP;
  22. protected $inicio;
  23. protected $fin;
  24. private $redisHelper;
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('nas:onu:octets')
  29. ->setDescription('Bandwidth ONTs de NAS')
  30. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  31. ->setDefinition(array(
  32. new InputOption('nas-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del NAS",1),
  33. new InputOption('nas-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del NAS",1),
  34. new InputOption('nas-ip', false, InputOption::VALUE_OPTIONAL, "IP del NAS"),
  35. new InputOption('nas-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY del NAS"),
  36. new InputOption('nas-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
  37. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  38. ))
  39. ;
  40. }
  41. public function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. $redisHelper = new RedisHelper();
  44. $msg = PHP_EOL."################################################# (".date("Y-m-d H:i:s").")";
  45. $output->writeln($msg);
  46. $nasDeviceId = (int) $input->getOption('nas-device-id');
  47. $nasServerId = (int) $input->getOption('nas-server-id');
  48. $nasIp = $input->getOption('nas-ip');
  49. $nasCommunity = $input->getOption('nas-community');
  50. $nasSnmpLibrary = $input->getOption('nas-snmp-library');
  51. $saveHistoric = (int) $input->getOption('save-historic');
  52. $d_s = "d_{$nasDeviceId}_s_{$nasServerId}";
  53. $cmd = str_replace(":","_",$this->getName());
  54. $output->writeln("INICIO COMANDO {$cmd} | {$d_s}");
  55. $flag = "{$d_s}_cmd_$cmd";
  56. if (!RedisHelper::lock($flag)) {
  57. exit(1);
  58. }
  59. $SNMP = new SNMP($nasIp, $nasCommunity);
  60. $snmpService = new SNMPService($flag);
  61. $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$d_s}";
  62. $inicio = microtime(true);
  63. $library = "use".$nasSnmpLibrary;
  64. $apiSNMP = $SNMP->$library();
  65. $bandwidthCached = $redisHelper->getData($key_nas_onu_bandwidth, true);
  66. $onus = $snmpService->getSNMP($apiSNMP, "onuSerialNumber","onu", $output);
  67. //counter64
  68. $inOctets = $snmpService->getSNMP($apiSNMP, "onuInOctets","onuInOctets", $output);
  69. $outOctets = $snmpService->getSNMP($apiSNMP, "onuOutOctets","onuOutOctets", $output);
  70. $consumptionData = $sendData = array();
  71. $t1 = time();
  72. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  73. foreach($onus as $index => $onu) {
  74. if(preg_match("/^<pppoe-([^-]+)[-\d]*>$/",$onu, $match)){
  75. $sn = strtolower($match[1]);
  76. } else {
  77. continue;
  78. }
  79. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  80. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  81. if(isset($bandwidthCached[$index])) {
  82. $t0 = $bandwidthCached[$index]['t'];
  83. $in0 = $bandwidthCached[$index]['inOct'];
  84. $out0 = $bandwidthCached[$index]['outOct'];
  85. $inAcc = $bandwidthCached[$index]['inAcc'];
  86. $outAcc = $bandwidthCached[$index]['outAcc'];
  87. $inDiff = $outDiff = $inBandwidth = $outBandwidth = 0;
  88. if(($in1 >= $in0) && ($t1 > $t0)) {
  89. $inDiff = $diff = $in1 - $in0;
  90. $inAcc += $diff;
  91. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  92. }
  93. if(($out1 >= $out0) && ($t1 > $t0)) {
  94. $outDiff = $diff = $out1 - $out0;
  95. $outAcc += $diff;
  96. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  97. }
  98. $totalIn += $inBandwidth;
  99. $totalOut += $outBandwidth;
  100. $sendData["inbandwidth_onu_{$sn}"] = "{$inBandwidth}|g";
  101. $sendData["outbandwidth_onu_{$sn}"] = "{$outBandwidth}|g";
  102. $div = 1073741824; //bytes => giga
  103. $consIn = number_format(($inAcc / $div),3,'.','');
  104. $consOut = number_format(($outAcc / $div),3,'.','');
  105. $sendData["inconsumption_onu_{$sn}"] = "{$consIn}|g";
  106. $sendData["outconsumption_onu_{$sn}"] = "{$consOut}|g";
  107. $div = 1073741824; //bytes => giga
  108. $inGB = number_format($inDiff / $div, 5,".","");
  109. $outGB = number_format($outDiff / $div, 5,".","");
  110. $fatherDevice = $nasDeviceId;
  111. $server = $nasServerId;
  112. $fatherDeviceType = 3;
  113. $device = $sn;
  114. $date = date("Y-m-d");
  115. $consumptionData[] = "({$server},{$fatherDevice},{$fatherDeviceType},'{$device}','{$date}',{$inGB},{$outGB})";
  116. if(date("n",$t0) != date("n",$t1)) {
  117. $inAcc = $outAcc = 0;
  118. }
  119. $totalConsOut += $outAcc;
  120. $totalConsIn += $inAcc;
  121. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  122. } else {
  123. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  124. }
  125. }
  126. $redisHelper->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
  127. $this->saveConsumption($consumptionData);
  128. if($sendData && $saveHistoric) {
  129. $t_start_script = microtime(true);
  130. (new StatsDService())->send($sendData);
  131. $t_end_script = microtime(true);
  132. $time = $t_end_script - $t_start_script;
  133. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  134. }
  135. /* Fin de bloqueo */
  136. RedisHelper::removeLock($flag);
  137. $fin = microtime(true);
  138. $time = $fin - $inicio;
  139. $output->writeln("Tiempo: $time segundos");
  140. }
  141. private function saveConsumption($data)
  142. {
  143. if ($data) {
  144. $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);";
  145. try {
  146. (new DoctrineService())->executeQueries(array($query));
  147. } catch (DBALException $e) {
  148. }
  149. }
  150. }
  151. }