123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace NasBundle\Command;
- use BaseStatsBundle\Command\BaseCommand;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use NasBundle\SNMP\SNMP as SNMP;
- //use RedisBundle\Services\RedisService;
- class NasOnuOctetsCommand extends BaseCommand
- {
- protected $nasDeviceId;
- protected $nasServerId;
- protected $nasIp;
- protected $nasCommunity;
- protected $nasSnmpLibrary;
- protected $flag;
- protected $apiSNMP;
- protected $inicio;
- protected $fin;
- protected function configure()
- {
- $this
- ->setName('nas:onu:octets')
- ->setDescription('Bandwidth ONTs de NAS')
- ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
- ->setDefinition(array(
- new InputOption('nas-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del NAS",1),
- new InputOption('nas-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del NAS",1),
- new InputOption('nas-ip', false, InputOption::VALUE_OPTIONAL, "IP del NAS"),
- new InputOption('nas-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY del NAS"),
- new InputOption('nas-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
- new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
- ))
- ;
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $this->inicio = microtime(true);
- $this->output = $output;
- $this->setCollector();
-
- $msg = PHP_EOL."################################################# (".date("Y-m-d H:i:s").")";
- $this->output->writeln($msg);
-
- $this->nasDeviceId = (int) $input->getOption('nas-device-id');
- $this->nasServerId = (int) $input->getOption('nas-server-id');
- $this->nasIp = $input->getOption('nas-ip');
- $this->nasCommunity = $input->getOption('nas-community');
- $this->nasSnmpLibrary = $input->getOption('nas-snmp-library');
- $this->d_s = "d_{$this->nasDeviceId}_s_{$this->nasServerId}";
-
- $cmd = str_replace(":","_",$this->getName());
- $this->output->writeln("INICIO COMANDO {$cmd} | {$this->d_s}");
-
- $this->flag = "{$this->d_s}_cmd_$cmd";
-
- if(!$this->lock($this->flag)) {exit(1);}
- //$key_nas_scan = "nas_scan_{$this->d_s}";
- $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$this->d_s}";
- $saveHistoric = (int) $input->getOption('save-historic');
- $inicio = microtime(true);
- $SNMP = new SNMP($this->nasIp, $this->nasCommunity);
- $library = "use".$this->nasSnmpLibrary;
- $this->apiSNMP = $SNMP->$library();
-
- $bandwidthCached = $this->getData($key_nas_onu_bandwidth, true);
-
- $onus = $this->getSNMP("onuSerialNumber","onuSerialNumber");
-
- //counter64
- $inOctets = $this->getSNMP("onuInOctets","onuInOctets");
- $outOctets = $this->getSNMP("onuOutOctets","onuOutOctets");
- $sendData = array();
-
- $subId = $this->d_s;
-
- $t1 = time();
- $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
- foreach($onus as $index => $onu) {
- if(preg_match("/^<pppoe-([^-]+)[-\d]*>$/",$onu, $match)){
- $sn = strtolower($match[1]);
- } else {
- continue;
- }
- (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
- (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
- if(isset($bandwidthCached[$index])) {
- $t0 = $bandwidthCached[$index]['t'];
- $in0 = $bandwidthCached[$index]['inOct'];
- $out0 = $bandwidthCached[$index]['outOct'];
- $inAcc = $bandwidthCached[$index]['inAcc'];
- $outAcc = $bandwidthCached[$index]['outAcc'];
- $inBandwidth = $outBandwidth = 0;
-
- if(($in1 >= $in0) && ($t1 > $t0)) {
- $diff = $in1 - $in0;
- $inAcc += $diff;
- $inBandwidth = ($diff / ($t1 - $t0)) * 8;
- }
- if(($out1 >= $out0) && ($t1 > $t0)) {
- $diff = $out1 - $out0;
- $outAcc += $diff;
- $outBandwidth = ($diff / ($t1 - $t0)) * 8;
- }
- $totalIn += $inBandwidth;
- $totalOut += $outBandwidth;
- $sendData["inbandwidth_onu_{$sn}"] = "{$inBandwidth}|g";
- $sendData["outbandwidth_onu_{$sn}"] = "{$outBandwidth}|g";
- $div = 1073741824; //bytes => giga
- $consIn = number_format(($inAcc / $div),3,'.','');
- $consOut = number_format(($outAcc / $div),3,'.','');
-
- $sendData["inconsumption_onu_{$sn}"] = "{$consIn}|g";
- $sendData["outconsumption_onu_{$sn}"] = "{$consOut}|g";
- if(date("n",$t0) != date("n",$t1)) {
- $inAcc = $outAcc = 0;
- }
- $totalConsOut += $outAcc;
- $totalConsIn += $inAcc;
-
- $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
- } else {
- $bandwidthCached[$index] = array('sn' => $sn, 't' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
- }
- }
- $this->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
- if($sendData && $saveHistoric) {
- $t_start_script = microtime(true);
- $statsdService = $this->getContainer()->get('statsd');
- $statsdService->send($sendData);
- $t_end_script = microtime(true);
- $time = $t_end_script - $t_start_script;
- print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
- }
- /* Fin de bloqueo */
- $this->removeLock($this->flag);
- $fin = microtime(true);
- $time = $fin - $inicio;
- $this->output->writeln("Tiempo: $time segundos");
-
- }
- /**
- *
- * @param string $function
- * @param string $log
- * @return array
- *
- * Description: Ejecuta SNMP mientras se mide tiempo y errores
- */
- protected function getSNMP($function, $log) {
-
- $inicio = microtime(true);
- $error = array();
- try {
- $data = $this->apiSNMP->$function();
- } catch (\Exception $e) {
- $error['msg'] = $e->getMessage();
- $data = array();
- //LOG REPORT ERROR
- }
- $fin = microtime(true);
- $time = $fin - $inicio;
-
- $statsdService = $this->getContainer()->get('statsd');
- $m = "{$this->d_s}_snmp_$log";
- $statsdService->send(array($m => "{$time}|g"));
-
- $this->output->writeln("Tiempo {$function} / {$log}: $time segundos");
- if($error) {
- $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
- $device = $doctrine->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceServer' => $this->nasServerId, 'deviceType' => 'RadiusBundle\Entity\NAS', 'deviceId' => $this->nasDeviceId));
- if($device) {
- $reportManager = $this->getContainer()->get('stats.report.manager');
- $reportManager->create("RadiusBundle\Entity\NAS", "SNMP_ERROR", $this->nasDeviceId, $this->nasServerId, $device->getTenancyId(), $error);
- }
-
- }
- return $data;
- }
- }
|