Kaynağa Gözat

Se agrega bandwidth de interfaces.

Maxi Schvindt 7 yıl önce
ebeveyn
işleme
0b8150371d
2 değiştirilmiş dosya ile 123 ekleme ve 17 silme
  1. 113 17
      Command/CmtsInterfaceStatsCommand.php
  2. 10 0
      SNMP/MIBS/OIDSBase.php

+ 113 - 17
Command/CmtsInterfaceStatsCommand.php

@@ -24,7 +24,8 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
                 new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
                 new InputOption('cmts-ip', false, InputOption::VALUE_OPTIONAL, "IP del CMTS"),
                 new InputOption('cmts-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS"),
-                new InputOption('cmts-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP")
+                new InputOption('cmts-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)
             ))
         ;
     }
@@ -39,6 +40,7 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
         
         $key_cmts_scan = "cmts_scan_ifs_{$this->d_s}";
         $key_cm_scan = "cmts_scan_{$this->d_s}";
+        $saveHistoric = (int) $input->getOption('save-historic');
         $inicio = microtime(true);
         
         $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
@@ -74,11 +76,6 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
         $signal = $this->getSNMP("docsIfSigQSignalNoise","cmtsIfSignal");
 
         
-        print_r($utilization);
-        print_r($microreflection);
-        print_r($signal);
-
-
         $cmCached = $this->getData($key_cm_scan, true);
         $states = array();
         if(empty($cmCached)) {
@@ -101,9 +98,10 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
 
         $subId = $this->d_s;
         $metrics = array("utilization" => "{$subId}_if_utilization_", "microreflection" => "{$subId}_if_microreflection_", "signal" => "{$subId}_if_signal_", "states" => "{$subId}_if_state_");
+        $countIfs = 0;
 
         foreach($dataCached as $index => $interface) {
-            
+            $countIfs++;
             $stats = array();
             
             foreach($metrics as $data => $metric) {
@@ -136,20 +134,23 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
             $ifStatsCached[$index] = $stats;
         }
 
-        print_r($ifStatsCached);
-        print_r($sendData);
-
-        die;
+        $this->octets($ifStatsCached, $dataCached, $saveHistoric);
 
-        $dataCached = array();
-        $countIfs = 0;
+        if($ifStatsCached) {
+            $key_ifs_stats = "olt_stats_ifs_{$this->d_s}";
+            $this->setData($key_ifs_stats, $ifStatsCached, true);
+        }
 
-        $interfaces = $this->getSNMP("docsIfDescription","cmtsIf");
-        
+        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);
+        }
         
 
-        $this->setData($key_cmts_scan, $dataCached, true);
-
         /* Fin de bloqueo */
         $this->removeLock($this->flag);
 
@@ -158,4 +159,99 @@ class CmtsInterfaceStatsCommand extends BaseCmtsCommand
         $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
     }
 
+    private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
+
+        $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
+        $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
+
+        $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
+        $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
+        if(empty($bandwidthCached)) $bandwidthCached = array();
+
+        $t1 = time();
+        $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
+        $sendData = array(); $subId = $this->d_s;
+
+        foreach($dataCached as $index => $data) {
+            
+            (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
+            (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
+
+            if(isset($ifStatsCached[$index])) {
+                $ifStatsCached[$index]['inOctets'] = 0;
+                $ifStatsCached[$index]['outOctets'] = 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["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
+                $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$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);
+
+                if(isset($ifStatsCached[$index])) {
+                    $ifStatsCached[$index]['inOctets'] = $inBandwidth;
+                    $ifStatsCached[$index]['outOctets'] = $outBandwidth;
+                }
+            } else {
+                $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
+            }
+        }
+
+        $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
+        
+        $cmtsBandwidth = array();
+        $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
+        $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
+        
+        $div = 1073741824; //bytes => giga
+        $consIn = number_format(($totalConsIn / $div),3);
+        $consOut = number_format(($totalConsOut / $div),3);
+        $cmtsConsumption = array();
+        $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
+        $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
+
+        $statsdService = $this->getContainer()->get('statsd');
+        $statsdService->send($cmtsBandwidth);
+        $statsdService->send($cmtsConsumption);
+        
+        if($sendData && $saveHistoric) {
+            $t_start_script = microtime(true); 
+            $statsdService->send($sendData);
+            $t_end_script = microtime(true); 
+            $time = $t_end_script - $t_start_script;
+            print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
+        }
+        
+    }
+
 }

+ 10 - 0
SNMP/MIBS/OIDSBase.php

@@ -18,6 +18,8 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
     
     const OID_if_desc = "1.3.6.1.2.1.2.2.1.2";  //if_index > description
     const OID_system_description = '1.3.6.1.2.1.1.1.0';  //HOST SNMP DESCRIPTION
+    const OID_if_in_octets = '1.3.6.1.2.1.2.2.1.10';  //if_index > in octets (counter32)
+    const OID_if_out_octets = '1.3.6.1.2.1.2.2.1.16';  //if_index > out octets (counter32)
     
     const OID_docsIfCmtsChannelUtilization          = "1.3.6.1.2.1.10.127.1.3.9.1.3";
     const OID_docsIfSigQMicroreflections            = "1.3.6.1.2.1.10.127.1.1.4.1.6";
@@ -84,6 +86,14 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
     public function docsIfSigQSignalNoise() {
         return $this->getSNMP()->lastOidWalk(self::OID_docsIfSigQSignalNoise,14);
     }
+    
+    public function inOctets() {
+        return $this->getSNMP()->lastOidWalk(self::OID_if_in_octets,11);
+    }
+    
+    public function outOctets() {
+        return $this->getSNMP()->lastOidWalk(self::OID_if_out_octets,11);
+    }