Browse Source

Finished the implementation with Huawei

Jean Sumara 5 năm trước cách đây
mục cha
commit
b8fa2aad31

+ 2 - 4
bin/console

@@ -2,16 +2,14 @@
 <?php
 require __DIR__ . '/../vendor/autoload.php';
 
+use Flowdat\Stats\Command\Huawei\HuaweiOltScanCommand;
 use Flowdat\Stats\Command\Huawei\HuaweiOnuScanCommand;
-use Flowdat\Stats\Command\Huawei\HuaweiPonOctetsCommand;
-use Flowdat\Stats\Command\Huawei\HuaweiPonStatsCommand;
 use Symfony\Component\Console\Application;
 
 $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
 $dotenv->load();
 
 $application = new Application();
+$application->add(new HuaweiOltScanCommand());
 $application->add(new HuaweiOnuScanCommand());
-$application->add(new HuaweiPonOctetsCommand());
-$application->add(new HuaweiPonStatsCommand());
 $application->run();

+ 13 - 0
src/App/Service/Doctrine/DoctrineService.php

@@ -27,4 +27,17 @@ class DoctrineService
 
         return $result;
     }
+
+    /**
+     * @param string $sql
+     * @return Statement|null
+     * @throws DBALException
+     */
+    public function executeQuery($sql)
+    {
+        $doctrine = new Doctrine();
+        $result = $doctrine->getEm()->getConnection()->query($sql);
+
+        return $result;
+    }
 }

+ 82 - 0
src/App/Service/Huawei/HuaweiOltService.php

@@ -0,0 +1,82 @@
+<?php
+
+
+namespace Flowdat\Stats\App\Service\Huawei;
+
+
+use Flowdat\Stats\Domain\Huawei;
+
+class HuaweiOltService
+{
+    /**
+     * @var Huawei
+     */
+    private $huawei;
+    private $sendData;
+    private $stats;
+
+    /**
+     * HuaweiOltService constructor.
+     * @param $sendData
+     * @param $stats
+     */
+    public function __construct(&$sendData)
+    {
+        $this->sendData = &$sendData;
+        $this->stats = &$stats;
+    }
+
+    public function configure(Huawei $huawei){
+        $this->huawei = $huawei;
+        return $this;
+    }
+
+    public function setVoltageOrTxPower($data, $subId, $metricName, $metricCodeName){
+        if(isset($data[$this->huawei->getIndex()]) && !empty($data[$this->huawei->getIndex()])) {
+
+            $metric = $subId.$metricCodeName;
+            $m = "{$metric}{$this->huawei->getPonPort()}";
+            $v = $data[$this->huawei->getIndex()] * 0.01;
+            $this->sendData[$m] = "{$v}|g";
+
+            $this->stats[$metricName] = $v;
+        }
+
+        return $this;
+    }
+
+    public function setTemperatureOrBiasCurrent($data, $subId, $metricName, $metricCodeName){
+        if(isset($data[$this->huawei->getIndex()]) && !empty($data[$this->huawei->getIndex()])) {
+
+            $metric = $subId.$metricCodeName;
+            $m = "{$metric}{$this->huawei->getPonPort()}";
+            $v = $data[$this->huawei->getIndex()];
+            $this->sendData[$m] = "{$v}|g";
+
+            $this->stats[$metricName] = $v;
+        }
+
+        return $this;
+    }
+
+    public function setRxPower($ponPortOnuId, $ponsCache, $subId, $rx, &$ponStatsCached){
+        $indexPon = $ponPortOnuId[0];
+
+        if(isset($ponsCache[$indexPon])) {
+            $onuId = $ponPortOnuId[1];
+            $ponPort = str_replace ('/','.',$ponsCache[$indexPon]['ponPort']);
+
+            $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
+
+            $v = ($rx - 10000) * 0.01;
+
+            if(isset($ponStatsCached[$indexPon]['rxPower'])) {
+                $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
+            }
+
+            $this->sendData[$m] = "{$v}|g";
+
+        }
+    }
+
+}

+ 97 - 0
src/App/Service/Huawei/HuaweiService.php

@@ -198,6 +198,103 @@ class HuaweiService
         $this->output->writeln("Tiempo: $time segundos");
     }
 
+    public function searchOltMemoryAndCpu($keyOltScanCard){
+        $startCmd = microtime(true);
+        $oltScanCardCache = $this->redisHelper->getData($keyOltScanCard, true);
+
+        if(empty($oltScanCardCache)){
+            $oltScanCardCache = [];
+
+            $cpu = $this->snmpService->getSNMP($this->snmpLibrary, "oltCardCpu","cardCpu", $this->output);
+            $memory = $this->snmpService->getSNMP($this->snmpLibrary,"oltCardMemory","cardMemory", $this->output);
+
+            $_memory = $_cpu = array();
+            foreach($cpu as $index => $value) {
+                if($value == -1) continue;
+                $_cpu[$index] = $value;
+            }
+
+            foreach($memory as $index => $value) {
+                if($value == -1) continue;
+                $_memory[$index] = $value;
+            }
+
+            if($_cpu) {$oltScanCardCache['cpu'] = $_cpu;}
+
+            if($_memory) {$oltScanCardCache['memory'] = $_memory;}
+
+            $this->redisHelper->setData($keyOltScanCard, $oltScanCardCache, true);
+        }
+
+        $endCmd = microtime(true);
+        $time = $endCmd - $startCmd;
+        $this->output->writeln("Tiempo: $time segundos");
+        return $this;
+    }
+
+    public function searchInformationsAboutOltAndSave($keyOltScanPons, $keyPonStats, $subId, $saveHistoric){
+        $startCmd = microtime(true);
+
+        $oltScanPonsCached = $this->redisHelper->getData($keyOltScanPons, true);
+        if(!empty($oltScanPonsCached)){
+            //return [PONINDEX.ONUID] => value
+            $rxPower = $this->snmpService->getSNMP($this->snmpLibrary, "oltPonRxOpticalPower","ponRxPower", $this->output);
+            //return [PONINDEX] => value
+            $txPower = $this->snmpService->getSNMP($this->snmpLibrary, "oltPonTxOpticalPower","ponTxPower", $this->output);
+            $temperature = $this->snmpService->getSNMP($this->snmpLibrary, "oltPonOpticalTemperature","ponTemperature", $this->output);
+            $voltage = $this->snmpService->getSNMP($this->snmpLibrary, "oltPonOpticalVltage","ponVoltage", $this->output);
+            $biasCurrent = $this->snmpService->getSNMP($this->snmpLibrary, "oltPonOpticalCurrent","ponBiasCurrent", $this->output);
+
+            $ponStatsCached = [];
+            $sendData = [];
+
+            $huaweiOltService = new HuaweiOltService($sendData);
+
+            foreach ($oltScanPonsCached as $index => $pon){
+                $ponPort = str_replace ('/','.',$pon['ponPort']);
+
+                $stats = [];
+                $stats['ponPort'] = $pon['ponPort'];
+                $stats['rxPower'] = [];
+
+                $huaweiOltService
+                    ->configure(new Huawei($index, null, $ponPort, $stats))
+                    ->setVoltageOrTxPower($txPower, $subId, "txPower", "_pon_tx_")
+                    ->setVoltageOrTxPower($voltage, $subId, "voltage", "_pon_voltage_")
+                    ->setTemperatureOrBiasCurrent($temperature, $subId, "temperature", "_pon_temperature_")
+                    ->setTemperatureOrBiasCurrent($biasCurrent, $subId, "biasCurrent", "_pon_biascurrent_");
+
+                $ponStatsCached[$index] = $stats;
+            }
+
+            foreach($rxPower as $index => $rx) {
+                $ponPortOnuId = explode(".",$index);
+                if($rx != 2147483647 && count($ponPortOnuId) == 2){
+                    $huaweiOltService->setRxPower($ponPortOnuId, $oltScanPonsCached, $subId, $rx, $ponStatsCached);
+                }
+            }
+
+            if($ponStatsCached) {
+                $this->redisHelper->setData($keyPonStats, $ponStatsCached, true);
+            }
+
+            if($sendData && $saveHistoric) {
+                $timeStartScript = microtime(true);
+                (new StatsDService())->send($sendData);
+                $timeEndScript = microtime(true);
+                $time = $timeEndScript - $timeStartScript;
+                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 saveConsumption($data)
     {
         if ($data) {

+ 12 - 0
src/App/Service/Stats/StatsService.php

@@ -0,0 +1,12 @@
+<?php
+
+
+namespace Flowdat\Stats\App\Service\Stats;
+
+
+class StatsService
+{
+    public function updateStatsOnu($oltDeviceId, $oltServerId){
+
+    }
+}

+ 19 - 51
src/Command/Huawei/HuaweiOltScanCommand.php

@@ -1,7 +1,10 @@
 <?php
 
+
 namespace Flowdat\Stats\Command\Huawei;
 
+
+use Flowdat\Stats\App\Service\Huawei\HuaweiService;
 use Flowdat\Stats\Command\Base\BaseCommand;
 use Flowdat\Stats\SNMP\Huawei\SNMP as SNMP;
 use Symfony\Component\Console\Input\InputInterface;
@@ -21,63 +24,28 @@ class HuaweiOltScanCommand extends BaseCommand
                 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('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
-     * @return int
-     */
-    public function execute(InputInterface $input, OutputInterface $output)
-    {
+    public function execute(InputInterface $input, OutputInterface $output){
         parent::execute($input, $output);
-        
-        $key_olt_scan = "olt_scan_card_{$this->d_s}";
-        $inicio = microtime(true);
-        
+        $saveHistoric = (int)$input->getOption('save-historic');
+
+        /*Definir o SNMP*/
         $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
         $library = "use".$this->oltSnmpLibrary;
-        $this->apiSNMP = $SNMP->$library();
-
-        $dataCached = $this->getData($key_olt_scan, true);
-
-        if(!empty($dataCached)){
-            return 1;
-        }
-
-        $dataCached = [];
 
-        $cpu = $this->getSNMP("oltCardCpu","cardCpu");
-        $memory = $this->getSNMP("oltCardMemory","cardMemory");
+        /*Definindo as Keys*/
+        $keyOltScanCard = "olt_scan_card_{$this->d_s}";
+        $keyOltScanPons = "olt_scan_pons_{$this->d_s}";
+        $keyPonStats = "olt_stats_pons_{$this->d_s}";
+        $huaweiService = new HuaweiService($SNMP->$library(), $output, $this->flag);
 
-        $_memory = $_cpu = array();
-        foreach($cpu as $index => $value) {
-            if($value == -1) continue;
-            $_cpu[$index] = $value;
-        }
-        
-        foreach($memory as $index => $value) {
-            if($value == -1) continue;
-            $_memory[$index] = $value;
-        }
-
-        if($_cpu) {$dataCached['cpu'] = $_cpu;}
-        
-        if($_memory) {$dataCached['memory'] = $_memory;}
-
-        $this->setData($key_olt_scan, $dataCached, true);
-
-        /* Fin de bloqueo */
-        $this->removeLock($this->flag);
-
-        $fin = microtime(true);
-        $time = $fin - $inicio;
-        $this->output->writeln("Tiempo: $time segundos");
-
-        return 1;
+        $huaweiService
+            ->searchOltMemoryAndCpu($keyOltScanCard)
+            ->searchPonAndSaveCache($keyOltScanPons)
+            ->searchInformationsAboutOltAndSave($keyOltScanPons, $keyPonStats, $this->d_s, $saveHistoric);
     }
-
 }

+ 0 - 78
src/Command/Huawei/HuaweiPonScanCommand.php

@@ -1,78 +0,0 @@
-<?php
-
-namespace Flowdat\Stats\Command\Huawei;
-
-use Flowdat\Stats\Command\Base\BaseCommand;
-use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-use Flowdat\Stats\SNMP\Huawei\SNMP as SNMP;
-
-class HuaweiPonScanCommand extends BaseCommand
-{
-
-    protected function configure()
-    {
-        $this
-            ->setName('huawei:pon:scan')
-            ->setDescription('Escanear OLT para obtener PON PORTs')
-            ->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")
-            ))
-        ;
-    }
-
-    /**
-     * @param InputInterface $input
-     * @param OutputInterface $output
-     * @return bool
-     */
-    protected function execute(InputInterface $input, OutputInterface $output)
-    {
-        parent::execute($input, $output);
-        
-        $key_olt_scan = "olt_scan_pons_{$this->d_s}";
-        $inicio = microtime(true);
-        
-        $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
-        $library = "use".$this->oltSnmpLibrary;
-        $this->apiSNMP = $SNMP->$library();
-
-        $dataCached = array();
-        $ports = $this->getSNMP("oltPonDesc","ponScan");
-        $countPons = 0;
-
-        foreach($ports as $index => $d) {
-            //PON s/p
-            if (preg_match("/pon/i", $d)) {
-                $s = str_replace(array("gpon","GPON","pon","PON"," "),"",$d);
-                $s_p = explode("/",trim($s));
-                if(count($s_p) == 3) {
-                    $countPons++;
-                    $b = $s_p[0];
-                    $s = $s_p[1];
-                    $p = $s_p[2];
-                    
-                    $dataCached[$index] = array("ponPort"=>"{$b}/{$s}/{$p}",'board'=>$b,'slot'=>$s,'port'=>$p);
-                }
-            } 
-        }
-
-        $this->setData($key_olt_scan, $dataCached, true);
-
-        /* Fin de bloqueo */
-        $this->removeLock($this->flag);
-
-        $fin = microtime(true);
-        $time = $fin - $inicio;
-        $this->output->writeln("Tiempo: $time segundos / Cantidad Puertos: {$countPons}");
-
-        return empty($dataCached);
-    }
-
-}

+ 23 - 1
src/Domain/Huawei.php

@@ -8,16 +8,22 @@ class Huawei implements AbstractOnu
 {
     private $index;
     private $sn;
+    private $ponPort;
+    private $stats;
 
     /**
      * Huawei constructor.
      * @param $index
      * @param $sn
+     * @param $ponPort
+     * @param $stats
      */
-    public function __construct($index, $sn)
+    public function __construct($index, $sn, $ponPort = null, $stats = null)
     {
         $this->index = $index;
         $this->sn = $sn;
+        $this->ponPort = $ponPort;
+        $this->stats = $stats;
     }
 
     /**
@@ -56,5 +62,21 @@ class Huawei implements AbstractOnu
         return $this;
     }
 
+    /**
+     * @return null
+     */
+    public function getPonPort()
+    {
+        return $this->ponPort;
+    }
 
+    /**
+     * @param null $ponPort
+     * @return Huawei
+     */
+    public function setPonPort($ponPort)
+    {
+        $this->ponPort = $ponPort;
+        return $this;
+    }
 }

+ 19 - 0
src/Infrastructure/Repository/StatsRepository.php

@@ -0,0 +1,19 @@
+<?php
+
+
+namespace Flowdat\Stats\Infrastructure\Repository;
+
+
+use Doctrine\DBAL\DBALException;
+use Flowdat\Stats\App\Service\Doctrine\DoctrineService;
+
+class StatsRepository
+{
+    public function getTenancyFromDeviceAndServer($oltDeviceId, $oltServerId){
+        try {
+            (new DoctrineService())->executeQuery("SELECT tenancy_id FROM device WHERE device_id = {$oltDeviceId} AND device_server_id = {$oltServerId} AND device_type = 'FTTHBundle\Entity\OLT' LIMIT 1;");
+        } catch (DBALException $e) {
+            throw new \Exception($e->getMessage());
+        }
+    }
+}