123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace Flowdat\Stats\Command\NAS;
- use Doctrine\DBAL\DBALException;
- use Flowdat\Stats\App\Helper\RedisHelper;
- use Flowdat\Stats\App\Service\Doctrine\DoctrineService;
- use Flowdat\Stats\App\Service\SNMP\SNMPService;
- use Flowdat\Stats\App\Service\StatsD\StatsDService;
- use Flowdat\Stats\SNMP\NAS\SNMP;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- class NasOnuOctetsCommand extends Command
- {
- protected $nasDeviceId;
- protected $nasServerId;
- protected $nasIp;
- protected $nasCommunity;
- protected $nasSnmpLibrary;
- protected $flag;
- protected $apiSNMP;
- protected $inicio;
- protected $fin;
- private $redisHelper;
- 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)
- ))
- ;
- }
- public function execute(InputInterface $input, OutputInterface $output)
- {
- $redisHelper = new RedisHelper();
- $msg = PHP_EOL."################################################# (".date("Y-m-d H:i:s").")";
- $output->writeln($msg);
- $nasDeviceId = (int) $input->getOption('nas-device-id');
- $nasServerId = (int) $input->getOption('nas-server-id');
- $nasIp = $input->getOption('nas-ip');
- $nasCommunity = $input->getOption('nas-community');
- $nasSnmpLibrary = $input->getOption('nas-snmp-library');
- $saveHistoric = (int) $input->getOption('save-historic');
- $d_s = "d_{$nasDeviceId}_s_{$nasServerId}";
- $cmd = str_replace(":","_",$this->getName());
- $output->writeln("INICIO COMANDO {$cmd} | {$d_s}");
- $flag = "{$d_s}_cmd_$cmd";
- if (!RedisHelper::lock($flag)) {
- exit(1);
- }
- $SNMP = new SNMP($nasIp, $nasCommunity);
- $snmpService = new SNMPService($flag);
- $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$d_s}";
- $inicio = microtime(true);
- $library = "use".$nasSnmpLibrary;
- $apiSNMP = $SNMP->$library();
- $bandwidthCached = $redisHelper->getData($key_nas_onu_bandwidth, true);
- $onus = $snmpService->getSNMP($apiSNMP, "onuSerialNumber","onu", $output);
- //counter64
- $inOctets = $snmpService->getSNMP($apiSNMP, "onuInOctets","onuInOctets", $output);
- $outOctets = $snmpService->getSNMP($apiSNMP, "onuOutOctets","onuOutOctets", $output);
- $consumptionData = $sendData = array();
- $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'];
- $inDiff = $outDiff = $inBandwidth = $outBandwidth = 0;
- if(($in1 >= $in0) && ($t1 > $t0)) {
- $inDiff = $diff = $in1 - $in0;
- $inAcc += $diff;
- $inBandwidth = ($diff / ($t1 - $t0)) * 8;
- }
- if(($out1 >= $out0) && ($t1 > $t0)) {
- $outDiff = $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";
- $div = 1073741824; //bytes => giga
- $inGB = number_format($inDiff / $div, 5,".","");
- $outGB = number_format($outDiff / $div, 5,".","");
- $fatherDevice = $nasDeviceId;
- $server = $nasServerId;
- $fatherDeviceType = 3;
- $device = $sn;
- $date = date("Y-m-d");
- $consumptionData[] = "({$server},{$fatherDevice},{$fatherDeviceType},'{$device}','{$date}',{$inGB},{$outGB})";
- 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);
- }
- }
- $redisHelper->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
- $this->saveConsumption($consumptionData);
- if($sendData && $saveHistoric) {
- $t_start_script = microtime(true);
- (new 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 */
- RedisHelper::removeLock($flag);
- $fin = microtime(true);
- $time = $fin - $inicio;
- $output->writeln("Tiempo: $time segundos");
- }
- private function saveConsumption($data)
- {
- if ($data) {
- $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);";
- try {
- (new DoctrineService())->executeQueries(array($query));
- } catch (DBALException $e) {
- }
- }
- }
- }
|