Parcourir la source

Finished ZTE OLT and ONU

Jean Sumara Leopoldo il y a 5 ans
Parent
commit
afb944df27

+ 2 - 0
bin/console

@@ -7,6 +7,7 @@ use Flowdat\Stats\Command\Fiberhome\FiberhomeOnuScanCommand;
 use Flowdat\Stats\Command\Huawei\HuaweiOltScanCommand;
 use Flowdat\Stats\Command\Huawei\HuaweiOnuScanCommand;
 use Flowdat\Stats\Command\Zte\ZteOltScanCommand;
+use Flowdat\Stats\Command\Zte\ZteOnuScanCommand;
 use Symfony\Component\Console\Application;
 
 $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
@@ -20,4 +21,5 @@ $application->add(new FiberhomeOltScanCommand());
 $application->add(new FiberhomeOnuScanCommand());
 
 $application->add(new ZteOltScanCommand());
+$application->add(new ZteOnuScanCommand());
 $application->run();

+ 22 - 33
src/App/Service/Fiberhome/FiberhomeService.php

@@ -256,6 +256,7 @@ class FiberhomeService
             }
 
             $bandwidthCached = $this->redisHelper->getData($keyOltPonBandwith, true);
+            $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
 
             foreach($serialNumberCache as $index => $onu) {
                 $sn = strtolower($onu['serialNumber']);
@@ -277,7 +278,7 @@ class FiberhomeService
                     }
                 }
 
-                $this->setOltBandwith($onu, $index, $bandwidthCached, $keyOltPonBandwith, $subId, $inOctets, $outOctets);
+                $this->setOltBandwith($onu, $index, $bandwidthCached, $keyOltPonBandwith, $subId, $inOctets, $outOctets, $totalIn, $totalOut, $totalConsIn, $totalConsOut);
             }
 
             foreach($stats as $metric => $data) {
@@ -286,6 +287,25 @@ class FiberhomeService
                 $this->redisHelper->setData($keyOnuStats, $data, true);
             }
 
+            $oltBandwidth = array();
+            $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
+            $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
+
+            $div = 1073741824; //bytes => giga
+            $consIn = number_format(($totalConsIn / $div),3,'.','');
+            $consOut = number_format(($totalConsOut / $div),3,'.','');
+            $oltConsumption = array();
+            $oltConsumption["{$subId}_inconsumption_olt"] = "{$consIn}|g";
+            $oltConsumption["{$subId}_outconsumption_olt"] = "{$consOut}|g";
+
+            $statsdService = new StatsDService();
+            $statsdService->send($oltBandwidth);
+            $statsdService->send($oltConsumption);
+
+            $endCmd = microtime(true);
+            $time = $endCmd - $startCmd;
+            $this->output->writeln("Tiempo: $time segundos");
+
             if($sendData && $saveHistoric) {
                 $startScript = microtime(true);
                 (new StatsDService())->send($sendData);
@@ -303,12 +323,8 @@ class FiberhomeService
         $this->output->writeln("Tiempo: $time segundos");
     }
 
-    private function setOltBandwith($pon, $index, $bandwidthCached, $keyOltPonBandwith, $subId, $inOctets, $outOctets){
-        $startCmd = microtime(true);
-        $sendData = array();
-
+    private function setOltBandwith($pon, $index, $bandwidthCached, $keyOltPonBandwith, $subId, $inOctets, $outOctets, &$totalIn, &$totalOut, &$totalConsIn, &$totalConsOut){
         $t1 = time();
-        $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
         $ponPort = "{$pon['slot']}.{$pon['port']}";
         $serialNumber = strtolower($pon['serialNumber']);
 
@@ -370,32 +386,5 @@ class FiberhomeService
         }
 
         $this->redisHelper->setData($keyOltPonBandwith, $bandwidthCached, true);
-
-        $oltBandwidth = array();
-        $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
-        $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
-
-        $div = 1073741824; //bytes => giga
-        $consIn = number_format(($totalConsIn / $div),3,'.','');
-        $consOut = number_format(($totalConsOut / $div),3,'.','');
-        $oltConsumption = array();
-        $oltConsumption["{$subId}_inconsumption_olt"] = "{$consIn}|g";
-        $oltConsumption["{$subId}_outconsumption_olt"] = "{$consOut}|g";
-
-        $statsdService = new StatsDService();
-        $statsdService->send($oltBandwidth);
-        $statsdService->send($oltConsumption);
-
-        if($sendData) {
-            $startCript = microtime(true);
-            $statsdService->send($sendData);
-            $endScript = microtime(true);
-            $time = $endScript - $startCript;
-            print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
-        }
-
-        $endCmd = microtime(true);
-        $time = $endCmd - $startCmd;
-        $this->output->writeln("Tiempo: $time segundos");
     }
 }

+ 212 - 0
src/App/Service/Zte/ZteService.php

@@ -4,6 +4,7 @@
 namespace Flowdat\Stats\App\Service\Zte;
 
 
+use Flowdat\Stats\App\Helper\OnuHelper;
 use Flowdat\Stats\App\Helper\RedisHelper;
 use Flowdat\Stats\App\Service\SNMP\SNMPService;
 use Flowdat\Stats\App\Service\StatsD\StatsDService;
@@ -233,4 +234,215 @@ class ZteService
             $this->output->writeln("Tiempo: $time segundos");
         }
     }
+
+    public function searchNumberSerialAndSaveCache($keyOltScan, $keyOltPons){
+        $startCmd = microtime(true);
+
+        $serialNumbersCached = $this->redisHelper->getData($keyOltScan);
+        $portCached = $this->redisHelper->getData($keyOltPons, true);
+
+        if(!empty($serialNumbersCached)){
+            $endCmd = microtime(true);
+            $time = $endCmd - $startCmd;
+            $countOnusCached = count($serialNumbersCached);
+            $this->output->writeln("Tiempo: $time segundos / Cantidad ONUs: {$countOnusCached}");
+
+            return $this;
+        }
+
+        $serialNumbers = $this->snmpService->getSNMP($this->snmpLibrary, "onuSerialNumber","onuScan", $this->output);
+        $countOnus = 0;
+        $serialNumbersCached = [];
+
+        foreach($serialNumbers as $index => $hexSerialNumber) {
+            $vendoId = OnuHelper::hex2str(substr($hexSerialNumber,0,8));
+            $rest = substr($hexSerialNumber,8);
+
+            $sn = strtolower($vendoId.$rest);
+
+            $portOnu = explode(".",$index);
+            if(count($portOnu) != 2) continue;
+            $portIndex = $portOnu[0];
+            $onuId = $portOnu[1];
+            $countOnus++;
+
+            if(isset($portCached[$portIndex])) {
+                $p = $portCached[$portIndex];
+
+                $data = $p;
+                $data['onuId'] = $onuId;
+                $data['serialNumber'] = $sn;
+                $data['hexaSerialNumber'] = $hexSerialNumber;
+                $data['ponport'] = "{$p['ponPort']}/{$onuId}";
+                $serialNumbersCached[$index] = $data;
+            }
+        }
+
+        $this->redisHelper->setData($keyOltScan, $serialNumbersCached, true);
+
+        $endCmd = microtime(true);
+        $time = $endCmd - $startCmd;
+        $this->output->writeln("Tiempo: $time segundos / Cantidad ONUs: {$countOnus}");
+
+        return $this;
+    }
+
+    public function searchInformationsAboutOnuAndSave($keyOltScan, $keyOltPonBandwith, $subId, $saveHistoric){
+        $startCmd = microtime(true);
+        $serialNumberCache = $this->redisHelper->getData($keyOltScan, true);
+
+        if(!empty($serialNumberCache)){
+            $txPower = $this->snmpService->getSNMP($this->snmpLibrary, "onuPonTxOpticalPower","onuTxPower", $this->output);
+            $rxPower = $this->snmpService->getSNMP($this->snmpLibrary,"onuPonRxOpticalPower","onuRxPower", $this->output);
+            $temperature = $this->snmpService->getSNMP($this->snmpLibrary,"onuPonOpticalTemperature","onuTemperature", $this->output);
+            $voltage = $this->snmpService->getSNMP($this->snmpLibrary,"onuPonOpticalVltage","onuVoltage", $this->output);
+            $status = $this->snmpService->getSNMP($this->snmpLibrary,"onuStatus","onuStatus", $this->output);
+            //counter64
+            $inOctets = $this->snmpService->getSNMP($this->snmpLibrary, "ifHCInOctets","portInOctets", $this->output);
+            $outOctets = $this->snmpService->getSNMP($this->snmpLibrary, "ifHCOutOctets","portOutOctets", $this->output);
+
+            $stats = $sendData = array();
+
+            $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_");
+
+            foreach($metrics as $m => $metric) {
+                $stats[$metric] = array();
+            }
+
+            $bandwidthCached = $this->redisHelper->getData($keyOltPonBandwith, true);
+
+            $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
+            foreach($serialNumberCache as $index => $onu) {
+                $statusIndex = $index;
+                $index = $index.".1";
+                $sn = strtolower($onu['serialNumber']);
+
+                foreach($metrics as $data => $metric) {
+
+                    $m = "{$metric}{$sn}";
+                    if(isset($$data[$index]) && !empty($$data[$index])) {
+
+                        if($data == 'txPower') {
+                            $v = $$data[$index] * 0.002 - 30;
+                        } elseif($data == "rxPower") {
+                            $v = $$data[$index] * 0.002 - 30;
+                        } elseif($data == "voltage") {
+                            $v = ($$data[$index] * 20) / 1000;
+                        } elseif ($data == "temperature") {
+                            $v = $$data[$index] / 256;
+                        } else {
+                            continue;
+                        }
+
+                        $sendData[$m] = "{$v}|g";
+
+                        $stats[$metric][$sn] = $v;
+                    } elseif($data == "status") {
+                        $v = 0;
+                        if(isset($$data[$statusIndex]) && $$data[$statusIndex] == 3) $v = 1;
+
+                        $sendData[$m] = "{$v}|g";
+                        $stats[$metric][$sn] = $v;
+                    }
+                }
+
+                $this->setOltBandwith($sendData, $onu, $index, $bandwidthCached, $subId, $inOctets, $outOctets, $totalIn, $totalOut, $totalConsIn, $totalConsOut);
+            }
+
+            $this->redisHelper->setData($keyOltPonBandwith, $bandwidthCached, true);
+
+            $oltBandwidth = array();
+            $oltBandwidth["{$subId}_inbandwidth_olt"] = "{$totalIn}|g";
+            $oltBandwidth["{$subId}_outbandwidth_olt"] = "{$totalOut}|g";
+
+            $div = 1073741824; //bytes => giga
+            $consIn = number_format(($totalConsIn / $div),3,'.','');
+            $consOut = number_format(($totalConsOut / $div),3,'.','');
+            $oltConsumption = array();
+            $oltConsumption["{$subId}_inconsumption_olt"] = "{$consIn}|g";
+            $oltConsumption["{$subId}_outconsumption_olt"] = "{$consOut}|g";
+
+            $statsdService = new StatsDService();
+            $statsdService->send($oltBandwidth);
+            $statsdService->send($oltConsumption);
+
+            foreach($stats as $metric => $data) {
+                $keyOnuStats = "{$metric}{$subId}";
+
+                $this->redisHelper->setData($keyOnuStats, $data, true);
+            }
+
+            if($sendData && $saveHistoric) {
+                $startScript = microtime(true);
+                (new StatsDService())->send($sendData);
+                $endScript = microtime(true);
+                $time = $endScript - $startScript;
+                print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
+            }
+        }
+
+        /* Fin de bloqueo */
+        RedisHelper::removeLock($this->flagCmd);
+
+        $endCmd = microtime(true);
+        $time = $endCmd - $startCmd;
+        $this->output->writeln("Tiempo: $time segundos");
+    }
+
+    private function setOltBandwith(&$sendData, $pon, $index, &$bandwidthCached, $subId, $inOctets, $outOctets, &$totalIn, &$totalOut, &$totalConsIn, &$totalConsOut){
+        $t1 = time();
+
+        (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
+        (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
+
+        $ponPort = str_replace("/",".",$pon['ponPort']);
+
+        if(isset($bandwidthCached[$index]) && !isset($pon['nexo'])) {
+            $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["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
+            $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
+
+            $div = 1073741824; //bytes => giga
+            $_in = number_format(($inAcc / $div),3,'.','');
+            $_out = number_format(($outAcc / $div),3,'.','');
+            $sendData["{$subId}_inconsumption_pon_{$ponPort}"] = "{$_in}|g";
+            $sendData["{$subId}_outconsumption_pon_{$ponPort}"] = "{$_out}|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);
+        }
+
+
+    }
 }

+ 60 - 0
src/Command/Zte/ZteOnuScanCommand.php

@@ -0,0 +1,60 @@
+<?php
+
+
+namespace Flowdat\Stats\Command\Zte;
+
+
+use Flowdat\Stats\App\Service\Stats\StatsService;
+use Flowdat\Stats\App\Service\Zte\ZteService;
+use Flowdat\Stats\Command\Base\BaseCommand;
+use Flowdat\Stats\SNMP\Fiberhome\SNMP as SNMP;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ZteOnuScanCommand extends BaseCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('zte:onu:scan')
+            ->setDescription('Scan de ONUs de OLT')
+            ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
+            ->setDefinition(array(
+                new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT", 1),
+                new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT", 1),
+                new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
+                new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
+                new InputOption('olt-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)
+    {
+        parent::execute($input, $output);
+        $saveHistoric = (int)$input->getOption('save-historic');
+
+        /*Definir o SNMP*/
+        $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
+        $library = "use".$this->oltSnmpLibrary;
+
+        /*Definição das KEYS*/
+        $keyOltScan = "olt_scan_{$this->d_s}";
+        $keyOltScanPon = "olt_scan_pons_{$this->d_s}";
+        $keyOltBandwidthOnu = "olt_bandwidth_pons_{$this->d_s}";
+
+        $zteService = new ZteService($SNMP->$library(), $output, $this->flag);
+        $zteService
+            ->searchPonAndSaveCache($keyOltScanPon)
+            ->searchNumberSerialAndSaveCache($keyOltScan, $keyOltScanPon)
+            ->searchInformationsAboutOnuAndSave($keyOltScan, $keyOltBandwidthOnu, $this->d_s, $saveHistoric);
+
+        $statsService = new StatsService();
+        $statsService->updateStatsOnu($this->oltDeviceId, $this->oltServerId);
+    }
+}

+ 1 - 1
src/SNMP/Fiberhome/SNMP.php

@@ -620,7 +620,7 @@ class SNMP extends Basic
      */
     public function useExtension($mib, $args)
     {
-        $mib = 'Flowdat\\Stats\\SNMP\\Fiberhome\\MIBS\\' . str_replace('_', '\\', $mib);
+        $mib = 'Flowdat\\Stats\\SNMP\\Zte\\MIBS\\' . str_replace('_', '\\', $mib);
         $m = new $mib();
         $m->setSNMP($this);
         return $m;