Browse Source

Se agregan comandos

Maxi Schvindt 7 năm trước cách đây
mục cha
commit
7e72468597
2 tập tin đã thay đổi với 430 bổ sung4 xóa
  1. 293 0
      Command/CmtsOctetsCommand.php
  2. 137 4
      SNMP/MIBS/OIDSBase.php

+ 293 - 0
Command/CmtsOctetsCommand.php

@@ -0,0 +1,293 @@
+<?php
+
+
+
+namespace CmtsBundle\Command;
+
+use BaseStatsBundle\Command\BaseCmtsCommand;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use CmtsBundle\SNMP\SNMP as SNMP;
+use Symfony\Component\Yaml\Parser;
+
+class CmtsOctetsCommand extends BaseCmtsCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('cmts:octets')
+            ->setDescription('Obtener Octets de CMTS')
+            ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
+            ->setDefinition(array(
+                new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
+                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-docs', false, InputOption::VALUE_OPTIONAL, "Versión DOCS-QOS del CMTS.", 1),
+                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);
+        
+        $key_cm_octets = "cm_octets_{$this->d_s}";
+        $key_cmts_octets = "cmts_octets_{$this->d_s}";
+        $saveHistoric = (int) $input->getOption('save-historic');
+        $cmtsDocs = (int) $input->getOption('cmts-docs');
+        $inicio = microtime(true);
+        $time = time();
+        $date = date("Y-m-d");
+        
+        $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
+        $library = "use".$this->cmtsSnmpLibrary;
+        $this->apiSNMP = $SNMP->$library();
+        
+        $cmOctetsCached = $this->getData($key_cm_octets, true);
+        $cmtsOctetsCached = $this->getData($key_cmts_octets, true);
+
+        $inBandTotal = $outBandTotal = $inAccTotal = $outAccTotal = 0;
+        
+        if(empty($cmOctetsCached)) {
+            $this->output->writeln("Se inicializa {$key_cm_octets}.");
+            $cmOctetsCached = array();
+        }
+        
+        if(empty($cmtsOctetsCached)) {
+            $this->output->writeln("Se inicializa {$key_cmts_octets}.");
+            $cmtsOctetsCached = array();
+        }
+
+        switch($cmtsDocs) {
+
+            case 1:
+                $index = $this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs");
+                $flows = $this->getSNMP("docsQosServiceFlowDirection","cmtsFlows");
+                $octets = $this->getSNMP("docsQosServiceFlowOctets","cmtsOctets");
+                break;
+                
+            case 2:
+                $index = array_merge($this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs"),$this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs"));
+                $flows = array_merge($this->getSNMP("docsQosServiceFlowDirection","cmtsFlows"),$this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows"));
+                $octets = array_merge($this->getSNMP("docsQosServiceFlowOctets","cmtsOctets"),$this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets"));
+                break;
+                
+            case 3:
+                $index = $this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs");
+                $flows = $this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows");
+                $octets = $this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets");
+                break;
+        }
+        
+        //Flow Direction: 1 = download(in) / 2 = upload(out)
+
+        $data = array();
+        $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
+        
+        foreach($index as $mac => $_flows) {
+
+            $new = array('flows' => array());
+            $down = $up = 0;
+
+            $flowsCached = array();
+            if(isset($cmOctetsCached[$mac])) {
+                $flowsCached = $cmOctetsCached[$mac]['flows'];
+                $inBand = $cmOctetsCached[$mac]['inBand'];
+                $outBand = $cmOctetsCached[$mac]['outBand'];
+                $inAcc = $cmOctetsCached[$mac]['inAcc'];
+                $outAcc = $cmOctetsCached[$mac]['outAcc'];
+                $t0 = $cmOctetsCached[$mac]['t'];
+            } else {
+                $flowsCached = array();
+                $outAcc = $inAcc = $outBand = $inBand = 0; $t0 = $time;
+            }
+            
+            $inBandwidth = $outBandwidth = 0;
+
+            foreach($_flows as $flow) {
+                
+                if(!isset($flows[$flow]) || !isset($octets[$flow])) continue;
+
+                $d = $flows[$flow]; $o1 = $octets[$flow]; $t1 = $time; $acc = 0;
+                
+                if(isset($flowsCached[$flow])) {
+                    $o0 = $flowsCached[$flow]['oct'];
+                    
+                    if($d == 1) {
+                        $band = "inBand";
+                        $acc = "inAcc";
+                    } else {
+                        $band = "outBand";
+                        $acc = "outAcc";
+                    }
+                        
+                    if(($o1 >= $o0) && ($t1 > $t0)) {
+                        $diff = $o1 - $o0;
+                        $$acc += $diff;
+                        $$band += ($diff / ($t1 - $t0)) * 8;
+                    }
+                }
+                
+                $new['flows'][$flow] = array('d' => $d, 'oct' => $o1);
+            }
+
+            $new['inBand'] = $inBand;
+            $new['outBand'] = $outBand;
+            $new['inAcc'] = $inAcc;
+            $new['outAcc'] = $outAcc;
+            $new['t'] = $time;
+            
+            $data[$mac] = $new;
+            $sendData["inbandwidth_cm_{$mac}"] = "{$inBand}|g";
+            $sendData["outbandwidth_cm_{$mac}"] = "{$outBand}|g";
+
+            $div = 1073741824; //bytes => giga
+            $consIn = number_format(($inAcc / $div),3);
+            $consOut = number_format(($outAcc / $div),3);
+            
+            $sendData["inconsumption_cm_{$mac}"] = "{$consIn}|g";
+            $sendData["outconsumption_cm_{$mac}"] = "{$consOut}|g";
+
+            $inBandTotal += $inBand;
+            $outBandTotal += $outBand;
+            $inAccTotal += $inAcc;
+            $outAccTotal += $outAcc;
+
+        }
+
+        if($data) $this->setData($key_cm_octets, $data, true);
+
+        $sendData["{$this->d_s}_inbandwidth_cmts_x_cm"] = "{$inBandTotal}|g";
+        $sendData["{$this->d_s}_outbandwidth_cmts_x_cm"] = "{$outBandTotal}|g";
+
+        $div = 1073741824; //bytes => giga
+        $consIn = number_format(($inAccTotal / $div),3);
+        $consOut = number_format(($outAccTotal / $div),3);
+
+        $sendData["{$this->d_s}_inconsumption_cmts_x_cm"] = $consIn;
+        $sendData["{$this->d_s}_outconsumption_cmts_x_cm"] = $consOut;
+
+        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);
+        }
+        
+
+        /* Fin de bloqueo */
+        $this->removeLock($this->flag);
+
+        $fin = microtime(true);
+        $time = $fin - $inicio;
+        $this->output->writeln("Tiempo: $time segundos");
+    }
+
+    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);
+        }
+        
+    }
+
+}

+ 137 - 4
SNMP/MIBS/OIDSBase.php

@@ -10,11 +10,15 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
     const OID_docsIfDownChannelPower = "1.3.6.1.2.1.10.127.1.1.1.1.6"; //SNMP contra CM .channelCm > RxPower (v / 10)
     const OID_docsIfSigQSignalNoise = "1.3.6.1.2.1.10.127.1.1.4.1.5"; //SNMP contra CM .channelCm > SNR (v / 10)
     const OID_docsIfSigQMicroreflections = "1.3.6.1.2.1.10.127.1.1.4.1.6"; //SNMP contra CM .channelCm > Microreflection (v)
+    
+    
+    //Se utilizará en la vista de diagnóstico 
     const OID_docsIfSigQUnerroreds = "1.3.6.1.2.1.10.127.1.1.4.1.2"; //SNMP contra CM .channelCm > Unerroreds
     const OID_docsIfSigQCorrecteds = "1.3.6.1.2.1.10.127.1.1.4.1.3"; //SNMP contra CM .channelCm > Correcteds
     const OID_docsIfSigQUncorrectables = "1.3.6.1.2.1.10.127.1.1.4.1.4"; //SNMP contra CM .channelCm > Uncorrectables
     const OID_sysUpTimeInstance = "1.3.6.1.2.1.1.3.0"; //SNMP contra CM > uptime CM Format: Timeticks: (213988400) 24 days, 18:24:44.00
 
+
     //CMTS
     const OID_docsIfCmtsCmStatusMacAddress	= "1.3.6.1.2.1.10.127.1.3.3.1.2";  //cm_index > mac
     const OID_docsIfCmtsCmStatusIpAddress	= "1.3.6.1.2.1.10.127.1.3.3.1.3";  //cm_index > ip
@@ -37,6 +41,7 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
     const OID_docsIfCmtsCmStatusMicroreflections = "1.3.6.1.2.1.10.127.1.3.3.1.14"; //cm_index > MX de cm en CMTS
     const OID_docsIf3CmtsCmUsStatusMicroreflections = "1.3.6.1.4.1.4491.2.1.20.1.4.1.5"; 
     
+    // Utilizar estos para medir lo que llega a la interfaz desde el cm
     const OID_docsIfCmtsCmStatusExtUnerroreds = "1.3.6.1.2.1.10.127.1.3.3.1.15";
     const OID_docsIf3CmtsCmUsStatusUnerroreds = "1.3.6.1.4.1.4491.2.1.20.1.4.1.7";
 
@@ -46,18 +51,21 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
     const OID_docsIfCmtsCmStatusExtUncorrectables = "1.3.6.1.2.1.10.127.1.3.3.1.17";
     const OID_docsIf3CmtsCmUsStatusUncorrectables = "1.3.6.1.4.1.4491.2.1.20.1.4.1.9";
 
-
-    
     const OID_docsIfCmtsChannelUtilization          = "1.3.6.1.2.1.10.127.1.3.9.1.3";
 
+    //Contra CMTS return mac > flows
     const OID_docsQosCmtsIfIndex                = "1.3.6.1.2.1.10.127.7.1.11.1.3";
     const OID_docsQos3CmtsIfIndex		= "1.3.6.1.4.1.4491.2.1.21.1.11.1.3";
-    const OID_docsQosServiceFlowOctets		= "1.3.6.1.2.1.10.127.7.1.4.1.2";
+
+    //Contra CMTS return flow > direction
     const OID_docsQosServiceFlowDirection	= "1.3.6.1.2.1.10.127.7.1.3.1.7";
     const OID_docsQos3ServiceFlowDirection	= "1.3.6.1.4.1.4491.2.1.21.1.3.1.7";
-    const OID_docsIfCmStatusRxPower		= "1.3.6.1.2.1.10.127.1.1.1.1.6";
+    
+    //Contra CMTS return flow > octets
+    const OID_docsQosServiceFlowOctets		= "1.3.6.1.2.1.10.127.7.1.4.1.2";
     const OID_docsQos3ServiceFlowOctets         = "1.3.6.1.4.1.4491.2.1.21.1.4.1.2";
     
+    
     const OID_ipNetToMediaPhysAddress 		= "1.3.6.1.2.1.4.22.1.2";
     const OID_docsIfCmtsCmPtr  			= "1.3.6.1.2.1.10.127.1.3.7.1.2";
     
@@ -177,5 +185,130 @@ class OIDSBase extends \CmtsBundle\SNMP\MIB {
         }
     }
 
+    public function docsQos3CmtsIfIndex($mac=null) {
+	    $return = array();
+        
+        if(!is_null($mac)){
+		    $oid = self::OID_docsQos3CmtsIfIndex .'.'.$this->getDecMac($mac);
+	    }else{
+		    $oid = self::OID_docsQos3CmtsIfIndex;
+	    }
+        
+        foreach((array)$this->getSNMP()->realWalk($oid) as $k => $v) {
+		    $k_prim = preg_split("|\.|",$k);
+            
+            if(count($k_prim) < 20)
+                continue;
+                
+		    $k_mac = sprintf("%02x%02x%02x%02x%02x%02x",
+		    		 $k_prim[15], $k_prim[16], $k_prim[17], $k_prim[18], $k_prim[19], $k_prim[20]);
+            
+            $return[$k_mac][] = $this->getSNMP()->parseSnmpValue($v). ".".$k_prim[21];
+        }
+        
+	    return $return;
+    }
+
+    public function docsQos3ServiceFlowDirection($translate=false) {
+        $return = array();
+        
+        $oid = self::OID_docsQos3ServiceFlowDirection;
+        
+        $stats = $this->getSNMP()->realWalk($oid);
+        foreach((array)$stats as $k => $v){
+            $k_prim = preg_split("|\.|",$k);
+            if(!isset($k_prim[15])) continue;
+            
+            $return[$k_prim[15].".".$k_prim[16]] = $this->getSNMP()->parseSnmpValue($v);
+        }   
+        
+        return $return;
+    }
+
+    public function docsQos3ServiceFlowOctets($ifId=null) {
+	
+	    $return = array();
+        
+        if(!is_null($ifId)){
+		    $oid = self::OID_docsQos3ServiceFlowOctets ."." .$ifId;
+	    }else{
+		    $oid = self::OID_docsQos3ServiceFlowOctets;
+        }
+        
+        foreach($this->getSNMP()->realWalk($oid) as $k => $v){
+		    $k_prim = preg_split("|\.|",$k);
+		    $return[$k_prim[15].".".$k_prim[16]] = $this->getSNMP()->parseSnmpValue($v);
+        }
+        
+	    return $return;
+	
+	}
+
+    public function docsQosCmtsIfIndex($mac=null) {
+        
+        $return = array();
+        
+        if(!is_null($mac)){
+            $oid = self::OID_docsQosCmtsIfIndex .'.'.$this->getDecMac($mac);
+        }else{
+            $oid = self::OID_docsQosCmtsIfIndex;
+        }
+        
+        $ifIndex = $this->getSNMP()->realWalk($oid);
+
+        if(!is_array($ifIndex)) return array();
+        
+        foreach($ifIndex as $k => $v){
+            $k_prim = preg_split("|\.|",$k);
+            
+            if(!isset($k_prim[14]) OR !isset($k_prim[15]) OR !isset($k_prim[16]) OR !isset($k_prim[17]) OR !isset($k_prim[18]) OR !isset($k_prim[19])) continue;
+            
+            $k_mac = sprintf("%02x%02x%02x%02x%02x%02x", $k_prim[14], $k_prim[15], $k_prim[16], $k_prim[17], $k_prim[18], $k_prim[19]);
+        
+            $return[$k_mac][] = $this->getSNMP()->parseSnmpValue($v).".". $k_prim[20];
+        }
+
+        return $return;
+    }
+
+    public function docsQosServiceFlowDirection($translate=false) {
+        $return = array();
+
+        $oid = self::OID_docsQosServiceFlowDirection;
+        
+        $states = $this->getSNMP()->realWalk($oid);
+        
+        if(!is_array($states)) return array();
+        
+        foreach($states as $k => $v){
+            $oid_arr = explode(".", $k);
+            $return[$oid_arr[count($oid_arr) -2] . "." . $oid_arr[count($oid_arr) -1]] = $this->getSNMP()->parseSnmpValue($v);
+        }
+
+        return $return;
+    }
+
+    public function docsQosServiceFlowOctets($ifId=null) {
+        
+        $return = array();
+        
+        if(!is_null($ifId)){
+            $oid = self::OID_docsQosServiceFlowOctets ."." .$ifId;
+        }else{
+            $oid = self::OID_docsQosServiceFlowOctets;
+        }	
+
+        $stats = $this->getSNMP()->realWalk($oid);
+        
+        if(!is_array($stats)) return array();
+        
+        foreach($stats as $k => $v){
+            $k_prim = explode(".",$k);
+            $return[ $k_prim[count($k_prim) - 2] . "." . $k_prim[count($k_prim) - 1]] = $this->getSNMP()->parseSnmpValue($v);
+        }
+
+        return $return;
+    }
+
     
 }