Explorar o código

Merge branch 'stats_command#1' into 'master'

Stats command#1

See merge request interlink-sa/flowdat3/modules/stats_command!9
Jean Sumara Leopoldo %!s(int64=5) %!d(string=hai) anos
pai
achega
f58eb636a0

+ 66 - 62
src/App/Service/Zte/ZteService.php

@@ -190,10 +190,10 @@ class ZteService
                 if(count($ponPortOnuId) != 2) continue;
                 $indexPon = $ponPortOnuId[0];
 
-                if(isset($dataCached[$indexPon])) {
+                if(isset($oltScanPonsCached[$indexPon])) {
 
                     $onuId = $ponPortOnuId[1];
-                    $ponPort = str_replace ('/','.',$dataCached[$indexPon]['ponPort']);
+                    $ponPort = str_replace ('/','.',$oltScanPonsCached[$indexPon]['ponPort']);
 
                     $m = "{$subId}_pon_rx_{$ponPort}.{$onuId}";
 
@@ -203,8 +203,8 @@ class ZteService
                         $ponStatsCached[$indexPon]['rxPower'][$onuId] = $v;
                     }
 
-                    if(isset($dataCached[$indexPon]['nexo'])) {
-                        $index = $dataCached[$indexPon]['nexo'];
+                    if(isset($oltScanPonsCached[$indexPon]['nexo'])) {
+                        $index = $oltScanPonsCached[$indexPon]['nexo'];
                         if(isset($ponStatsCached[$index]['rxPower'])) {
                             $ponStatsCached[$index]['rxPower'][$onuId] = $v;
                         }
@@ -309,9 +309,6 @@ class ZteService
                 $stats[$metric] = array();
             }
 
-            $bandwidthCached = $this->redisHelper->getData($keyOltPonBandwith, true);
-
-            $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
             foreach($serialNumberCache as $index => $onu) {
                 $statusIndex = $index;
                 $index = $index.".1";
@@ -346,25 +343,9 @@ class ZteService
                     }
                 }
 
-                $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);
+            $this->setBandwidth($serialNumberCache, $sendData, $inOctets, $outOctets, $keyOltPonBandwith, $subId);
 
             foreach($stats as $metric => $data) {
                 $keyOnuStats = "{$metric}{$subId}";
@@ -389,60 +370,83 @@ class ZteService
         $this->output->writeln("Tiempo: $time segundos");
     }
 
-    private function setOltBandwith(&$sendData, $pon, $index, &$bandwidthCached, $subId, $inOctets, $outOctets, &$totalIn, &$totalOut, &$totalConsIn, &$totalConsOut){
+    private function setBandwidth($dataCached, &$sendData, $inOctets, $outOctets, $keyOltPonBandwidth, $subId){
         $t1 = time();
+        $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
+        $bandwidthCached = $this->redisHelper->getData($keyOltPonBandwidth, true);
+        foreach($dataCached as $index => $pon) {
 
-        (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
-        (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
+            if(isset($pon['nexo'])){
+                $realIndex = $pon['nexo'];
+            }
 
-        $ponPort = str_replace("/",".",$pon['ponPort']);
+            $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'];
+            (isset($inOctets[$realIndex]))? $in1 = $inOctets[$realIndex] : $in1 = 0;
+            (isset($outOctets[$realIndex]))? $out1 = $outOctets[$realIndex] : $out1 = 0;
 
-            $inBandwidth = $outBandwidth = 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'];
 
-            if(($in1 >= $in0) && ($t1 > $t0)) {
-                $diff = $in1 - $in0;
-                $inAcc += $diff;
-                $inBandwidth = ($diff / ($t1 - $t0)) * 8;
-            }
+                $inBandwidth = $outBandwidth = 0;
 
-            if(($out1 >= $out0) && ($t1 > $t0)) {
-                $diff = $out1 - $out0;
-                $outAcc += $diff;
-                $outBandwidth = ($diff / ($t1 - $t0)) * 8;
-            }
+                if(($in1 >= $in0) && ($t1 > $t0)) {
+                    $diff = $in1 - $in0;
+                    $inAcc += $diff;
+                    $inBandwidth = ($diff / ($t1 - $t0)) * 8;
+                }
 
-            $totalIn += $inBandwidth;
-            $totalOut += $outBandwidth;
+                if(($out1 >= $out0) && ($t1 > $t0)) {
+                    $diff = $out1 - $out0;
+                    $outAcc += $diff;
+                    $outBandwidth = ($diff / ($t1 - $t0)) * 8;
+                }
 
-            $sendData["{$subId}_inbandwidth_pon_{$ponPort}"] = "{$inBandwidth}|g";
-            $sendData["{$subId}_outbandwidth_pon_{$ponPort}"] = "{$outBandwidth}|g";
+                $totalIn += $inBandwidth;
+                $totalOut += $outBandwidth;
 
-            $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";
+                $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;
+                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);
+                $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->redisHelper->setData($keyOltPonBandwidth, $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);
     }
 }

+ 26 - 46
src/Command/NAS/NasOnuOctetsCommand.php

@@ -27,17 +27,6 @@ class NasOnuOctetsCommand extends Command
     protected $inicio;
     protected $fin;
     private $redisHelper;
-    private $snmpService;
-
-    /**
-     * NasOnuOctetsCommand constructor.
-     */
-    public function __construct()
-    {
-        parent::__construct();
-
-        $this->redisHelper = new RedisHelper();
-    }
 
     protected function configure()
     {
@@ -58,53 +47,44 @@ class NasOnuOctetsCommand extends Command
 
     public function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->inicio = microtime(true);
-        $this->output = $output;
-
+        $redisHelper = new RedisHelper();
         $msg = PHP_EOL."################################################# (".date("Y-m-d H:i:s").")";
-        $this->output->writeln($msg);
+        $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}";
+        $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());
-        $this->output->writeln("INICIO COMANDO {$cmd} | {$this->d_s}");
+        $output->writeln("INICIO COMANDO {$cmd} | {$d_s}");
 
-        $this->flag = "{$this->d_s}_cmd_$cmd";
+        $flag = "{$d_s}_cmd_$cmd";
 
-        if (!RedisHelper::lock($this->flag)) {
+        if (!RedisHelper::lock($flag)) {
             exit(1);
         }
 
-        $this->snmpService = new SNMPService($this->flag);
+        $SNMP = new SNMP($nasIp, $nasCommunity);
+        $snmpService = new SNMPService($flag);
 
-        //$key_nas_scan = "nas_scan_{$this->d_s}";
-        $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$this->d_s}";
-
-        $saveHistoric = (int) $input->getOption('save-historic');
+        $key_nas_onu_bandwidth = "nas_bandwidth_onu_{$d_s}";
         $inicio = microtime(true);
 
-        $SNMP = new SNMP($this->nasIp, $this->nasCommunity);
-        $library = "use".$this->nasSnmpLibrary;
-        $this->apiSNMP = $SNMP->$library();
-
-        $bandwidthCached = $this->redisHelper->getData($key_nas_onu_bandwidth, true);
-
-
-        $onus = $this->snmpService->getSNMP($this->apiSNMP, "onuSerialNumber","onuSerialNumber", $this-$output);
+        $library = "use".$nasSnmpLibrary;
+        $apiSNMP = $SNMP->$library();
 
+        $bandwidthCached = $redisHelper->getData($key_nas_onu_bandwidth, true);
+        $onus = $snmpService->getSNMP($apiSNMP, "onuSerialNumber","onu", $output);
         //counter64
-        $inOctets = $this->snmpService->getSNMP($this->apiSNMP, "onuInOctets","onuInOctets", $this->output);
-        $outOctets = $this->snmpService->getSNMP($this->apiSNMP, "onuOutOctets","onuOutOctets", $this->output);
+        $inOctets = $snmpService->getSNMP($apiSNMP, "onuInOctets","onuInOctets", $output);
+        $outOctets = $snmpService->getSNMP($apiSNMP, "onuOutOctets","onuOutOctets", $output);
 
         $consumptionData = $sendData = array();
 
-        $subId = $this->d_s;
-
         $t1 = time();
         $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
         foreach($onus as $index => $onu) {
@@ -155,8 +135,8 @@ class NasOnuOctetsCommand extends Command
                 $div = 1073741824; //bytes => giga
                 $inGB =  number_format($inDiff / $div, 5,".","");
                 $outGB = number_format($outDiff / $div, 5,".","");
-                $fatherDevice = $this->nasDeviceId;
-                $server = $this->nasServerId;
+                $fatherDevice = $nasDeviceId;
+                $server = $nasServerId;
                 $fatherDeviceType = 3;
                 $device = $sn;
                 $date = date("Y-m-d");
@@ -176,7 +156,7 @@ class NasOnuOctetsCommand extends Command
 
         }
 
-        $this->redisHelper->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
+        $redisHelper->setData($key_nas_onu_bandwidth, $bandwidthCached, true);
 
         $this->saveConsumption($consumptionData);
 
@@ -190,11 +170,11 @@ class NasOnuOctetsCommand extends Command
 
 
         /* Fin de bloqueo */
-        RedisHelper::removeLock($this->flag);
+        RedisHelper::removeLock($flag);
 
         $fin = microtime(true);
         $time = $fin - $inicio;
-        $this->output->writeln("Tiempo: $time segundos");
+        $output->writeln("Tiempo: $time segundos");
 
     }
 

+ 19 - 2
src/SNMP/NAS/MIBS/OIDSBase.php

@@ -11,10 +11,15 @@ class OIDSBase extends MIB {
     
     const OID_mtxrQueueSimpleBytesIn    = "1.3.6.1.4.1.14988.1.1.2.1.1.8";
     const OID_mtxrQueueSimpleBytesOut   = "1.3.6.1.4.1.14988.1.1.2.1.1.9";
-    
+
 
     public function onuSerialNumber($index = null) {
-        return $this->getSNMP()->lastOidWalk(self::OID_mtxrQueueSimpleName,14);
+        if(is_null($index)) {
+            return $this->getSNMP()->lastOidWalk(self::OID_mtxrQueueSimpleName,14);
+        }
+
+        $values = $this->getSNMP()->lastOidWalk(self::OID_mtxrQueueSimpleName,14);
+        return $this->convertIndex($index, $values);
     }
     
     public function onuInOctets() {
@@ -24,5 +29,17 @@ class OIDSBase extends MIB {
     public function onuOutOctets() {
         return $this->getSNMP()->lastOidWalk(self::OID_mtxrQueueSimpleBytesOut,14);
     }
+
+    function convertIndex($index, $values) {
+        $data = array();
+        foreach($values as $i => $v) {
+            if(isset($index[$i])) {
+                $data[$index[$i]] = $v;
+            }
+        }
+        if(!empty($index)) return $data;
+
+        return $values;
+    }
     
 }

+ 4 - 2
src/SNMP/NAS/SNMP.php

@@ -2,9 +2,10 @@
 
 namespace Flowdat\Stats\SNMP\NAS;
 use \Exception;
+use OSS_SNMP\Cache;
 use OSS_SNMP\Cache\Basic;
 
-class SNMP
+class SNMP extends Basic
 {
     /**
      * The SNMP community to use when polling SNMP services. Defaults to 'public' by the constructor.
@@ -99,6 +100,7 @@ class SNMP
      */
     public function __construct( $host = '127.0.0.1', $community = 'public' )
     {
+        parent::__construct();
         return $this->setHost( $host )
                     ->setCommunity( $community )
                     ->setOidOutputFormat( self::OID_OUTPUT_NUMERIC );
@@ -583,7 +585,7 @@ class SNMP
      *
      * We would suggest disableCache() / enableCache() used in pairs only when really needed.
      *
-     * @return \OSS_SNMP\Cache The cache object
+     * @return Cache The cache object
      */
     public function getCache()
     {