NasOnuOctetsCommand.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace NasBundle\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 NasBundle\SNMP\SNMP as SNMP;
  9. //use RedisBundle\Services\RedisService;
  10. class NasOnuOctetsCommand extends BaseCommand
  11. {
  12. protected $nasDeviceId;
  13. protected $nasServerId;
  14. protected $nasIp;
  15. protected $nasCommunity;
  16. protected $nasSnmpLibrary;
  17. protected $flag;
  18. protected $apiSNMP;
  19. protected $inicio;
  20. protected $fin;
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('nas:onu:octets')
  25. ->setDescription('Bandwidth ONTs de NAS')
  26. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  27. ->setDefinition(array(
  28. new InputOption('nas-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del NAS",1),
  29. new InputOption('nas-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del NAS",1),
  30. new InputOption('nas-ip', false, InputOption::VALUE_OPTIONAL, "IP del NAS"),
  31. new InputOption('nas-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY del NAS"),
  32. new InputOption('nas-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
  33. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  34. ))
  35. ;
  36. }
  37. /**
  38. * @param InputInterface $input
  39. * @param OutputInterface $output
  40. */
  41. protected function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. $this->inicio = microtime(true);
  44. $this->output = $output;
  45. $this->setCollector();
  46. $msg = PHP_EOL."################################################# (".date("Y-m-d H:i:s").")";
  47. $this->output->writeln($msg);
  48. $this->nasDeviceId = (int) $input->getOption('nas-device-id');
  49. $this->nasServerId = (int) $input->getOption('nas-server-id');
  50. $this->nasIp = $input->getOption('nas-ip');
  51. $this->nasCommunity = $input->getOption('nas-community');
  52. $this->nasSnmpLibrary = $input->getOption('nas-snmp-library');
  53. $this->d_s = "d_{$this->nasDeviceId}_s_{$this->nasServerId}";
  54. $cmd = str_replace(":","_",$this->getName());
  55. $this->output->writeln("INICIO COMANDO {$cmd} | {$this->d_s}");
  56. $this->flag = "{$this->d_s}_cmd_$cmd";
  57. if(!$this->lock($this->flag)) {exit(1);}
  58. //$key_nas_scan = "nas_scan_{$this->d_s}";
  59. $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$this->d_s}";
  60. $saveHistoric = (int) $input->getOption('save-historic');
  61. $inicio = microtime(true);
  62. $SNMP = new SNMP($this->nasIp, $this->nasCommunity);
  63. $library = "use".$this->nasSnmpLibrary;
  64. $this->apiSNMP = $SNMP->$library();
  65. $bandwidthCached = $this->getData($key_nas_onu_bandwidth, true);
  66. $onus = $this->getSNMP("onuSerialNumber","onuSerialNumber");
  67. //counter64
  68. $inOctets = $this->getSNMP("onuInOctets","onuInOctets");
  69. $outOctets = $this->getSNMP("onuOutOctets","onuOutOctets");
  70. $sendData = array();
  71. $subId = $this->d_s;
  72. $t1 = time();
  73. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  74. foreach($onus as $index => $onu) {
  75. if(preg_match("/^<pppoe-([^-]+)[-\d]*>$/",$onu, $match)){
  76. $sn = strtolower($match[1]);
  77. } else {
  78. continue;
  79. }
  80. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  81. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  82. if(isset($bandwidthCached[$index])) {
  83. $t0 = $bandwidthCached[$index]['t'];
  84. $in0 = $bandwidthCached[$index]['inOct'];
  85. $out0 = $bandwidthCached[$index]['outOct'];
  86. $inAcc = $bandwidthCached[$index]['inAcc'];
  87. $outAcc = $bandwidthCached[$index]['outAcc'];
  88. $inBandwidth = $outBandwidth = 0;
  89. if(($in1 >= $in0) && ($t1 > $t0)) {
  90. $diff = $in1 - $in0;
  91. $inAcc += $diff;
  92. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  93. }
  94. if(($out1 >= $out0) && ($t1 > $t0)) {
  95. $diff = $out1 - $out0;
  96. $outAcc += $diff;
  97. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  98. }
  99. $totalIn += $inBandwidth;
  100. $totalOut += $outBandwidth;
  101. $sendData["inbandwidth_onu_{$sn}"] = "{$inBandwidth}|g";
  102. $sendData["outbandwidth_onu_{$sn}"] = "{$outBandwidth}|g";
  103. if(date("d",$t0) != date("d",$t1)) {
  104. $inAcc = $outAcc = 0;
  105. }
  106. $totalConsOut += $outAcc;
  107. $totalConsIn += $inAcc;
  108. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  109. } else {
  110. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  111. }
  112. }
  113. $this->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
  114. if($sendData && $saveHistoric) {
  115. $t_start_script = microtime(true);
  116. $statsdService = $this->getContainer()->get('statsd');
  117. $statsdService->send($sendData);
  118. $t_end_script = microtime(true);
  119. $time = $t_end_script - $t_start_script;
  120. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  121. }
  122. /* Fin de bloqueo */
  123. $this->removeLock($this->flag);
  124. $fin = microtime(true);
  125. $time = $fin - $inicio;
  126. $this->output->writeln("Tiempo: $time segundos");
  127. }
  128. /**
  129. *
  130. * @param string $function
  131. * @param string $log
  132. * @return array
  133. *
  134. * Description: Ejecuta SNMP mientras se mide tiempo y errores
  135. */
  136. protected function getSNMP($function, $log) {
  137. $inicio = microtime(true);
  138. $error = array();
  139. try {
  140. $data = $this->apiSNMP->$function();
  141. } catch (\Exception $e) {
  142. $error['msg'] = $e->getMessage();
  143. $data = array();
  144. //LOG REPORT ERROR
  145. }
  146. $fin = microtime(true);
  147. $time = $fin - $inicio;
  148. $statsdService = $this->getContainer()->get('statsd');
  149. $m = "{$this->d_s}_snmp_$log";
  150. $statsdService->send(array($m => "{$time}|g"));
  151. $this->output->writeln("Tiempo {$function} / {$log}: $time segundos");
  152. if($error) {
  153. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  154. $device = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceServer' => $this->nasServerId, 'deviceType' => 'RadiusBundle\Entity\NAS', 'deviceId' => $this->nasDeviceId));
  155. if($device) {
  156. $reportManager = $this->getContainer()->get('stats.report.manager');
  157. $reportManager->create("RadiusBundle\Entity\NAS", "SNMP_ERROR", $this->nasDeviceId, $this->nasServerId, $device->getTenancyId(), $error);
  158. }
  159. }
  160. return $data;
  161. }
  162. }