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("/^$/",$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"; if(date("d",$t0) != date("d",$t1)) { $inAcc = $outAcc = 0; } $totalConsOut += $outAcc; $totalConsIn += $inAcc; $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth); } else { $bandwidthCached[$index] = array('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; } }