NasOnuOctetsCommand.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $div = 1073741824; //bytes => giga
  104. $consIn = number_format(($inAcc / $div),3);
  105. $consOut = number_format(($outAcc / $div),3);
  106. $sendData["inconsumption_onu_{$sn}"] = "{$consIn}|g";
  107. $sendData["outconsumption_onu_{$sn}"] = "{$consOut}|g";
  108. if(date("d",$t0) != date("d",$t1)) {
  109. $inAcc = $outAcc = 0;
  110. }
  111. $totalConsOut += $outAcc;
  112. $totalConsIn += $inAcc;
  113. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  114. } else {
  115. $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  116. }
  117. }
  118. $this->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
  119. if($sendData && $saveHistoric) {
  120. $t_start_script = microtime(true);
  121. $statsdService = $this->getContainer()->get('statsd');
  122. $statsdService->send($sendData);
  123. $t_end_script = microtime(true);
  124. $time = $t_end_script - $t_start_script;
  125. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  126. }
  127. /* Fin de bloqueo */
  128. $this->removeLock($this->flag);
  129. $fin = microtime(true);
  130. $time = $fin - $inicio;
  131. $this->output->writeln("Tiempo: $time segundos");
  132. }
  133. /**
  134. *
  135. * @param string $function
  136. * @param string $log
  137. * @return array
  138. *
  139. * Description: Ejecuta SNMP mientras se mide tiempo y errores
  140. */
  141. protected function getSNMP($function, $log) {
  142. $inicio = microtime(true);
  143. $error = array();
  144. try {
  145. $data = $this->apiSNMP->$function();
  146. } catch (\Exception $e) {
  147. $error['msg'] = $e->getMessage();
  148. $data = array();
  149. //LOG REPORT ERROR
  150. }
  151. $fin = microtime(true);
  152. $time = $fin - $inicio;
  153. $statsdService = $this->getContainer()->get('statsd');
  154. $m = "{$this->d_s}_snmp_$log";
  155. $statsdService->send(array($m => "{$time}|g"));
  156. $this->output->writeln("Tiempo {$function} / {$log}: $time segundos");
  157. if($error) {
  158. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  159. $device = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceServer' => $this->nasServerId, 'deviceType' => 'RadiusBundle\Entity\NAS', 'deviceId' => $this->nasDeviceId));
  160. if($device) {
  161. $reportManager = $this->getContainer()->get('stats.report.manager');
  162. $reportManager->create("RadiusBundle\Entity\NAS", "SNMP_ERROR", $this->nasDeviceId, $this->nasServerId, $device->getTenancyId(), $error);
  163. }
  164. }
  165. return $data;
  166. }
  167. }