Pārlūkot izejas kodu

FD3-594 Se agrega comando para obtener métricas SLA de un CM. Se realiza la medición en el comando que obtiene valores del CM. Se envía todo al mongodb y se almacena en redis.

Maxi Schvindt 6 gadi atpakaļ
vecāks
revīzija
aac7b89cde
2 mainītis faili ar 153 papildinājumiem un 1 dzēšanām
  1. 45 1
      Command/CmtsCmStatsCommand.php
  2. 108 0
      Command/PingCommand.php

+ 45 - 1
Command/CmtsCmStatsCommand.php

@@ -2,7 +2,7 @@
 # bin/console cmts:cm:stats --cmts-device-id=14 --cmts-server-id=1 --cm-ip=10.42.0.27 --cm-mac=00237406d524 --cm-community=public
 # http://200.50.168.115/Provisioning/admin/cablemodem/00237406d524
 
-# bin/console cmts:cm:stats --cmts-device-id=14 --cmts-server-id=1 --cm-ip=10.42.0.27 --cm-mac=0011e3039a4a --cm-community=public
+# bin/console cmts:cm:stats --cmts-device-id=14 --cmts-server-id=1 --cm-ip=10.50.2.3 --cm-mac=001a6663dcde --cm-community=public
 
 namespace CmtsBundle\Command;
 
@@ -13,6 +13,10 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use CmtsBundle\SNMP\SNMP as SNMP;
 
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Output\BufferedOutput;
+
 class CmtsCmStatsCommand extends BaseCmCommand
 {
 
@@ -75,6 +79,13 @@ class CmtsCmStatsCommand extends BaseCmCommand
         $correcteds = $this->getSNMP("docsIfSigQCorrecteds","cmCorrecteds");
         $uncorrectables = $this->getSNMP("docsIfSigQUncorrectables","cmUncorrectables");
 
+        $slaJson = $this->sla($this->cmIp);
+        try{
+            $sla = json_decode($slaJson,true);
+        } catch(\Exception $e) {
+            $sla = array();
+        }
+        
         $metrics = array('txPower' => 'cm_tx_', 'rxPower' => 'cm_rx_', 'signal' => 'cm_signal_', 'microreflection' => 'cm_microreflection_', 'unerroreds' => 'cm_unerroreds_', 'correcteds' => 'cm_correcteds_', 'uncorrectables' => 'cm_uncorrectables_');
         $sendData = $data = array();
 
@@ -103,6 +114,21 @@ class CmtsCmStatsCommand extends BaseCmCommand
             }
         }
 
+        if(isset($sla['latency'])) {
+            $data['latency'] = $sla['latency'];
+            $sendData["cm_latency_{$this->cmMac}"] = "{$sla['latency']}|g";
+        }
+        
+        if(isset($sla['jitter'])) {
+            $data['jitter'] = $sla['jitter'];
+            $sendData["cm_jitter_{$this->cmMac}"] = "{$sla['jitter']}|g";
+        }
+
+        if(isset($sla['loss'])) {
+            $data['loss'] = $sla['loss'];
+            $sendData["cm_loss_{$this->cmMac}"] = "{$sla['loss']}|g";
+        }
+
         $this->hset($key_cm_stats, $this->cmMac, $data, true);
 
         //$data = $this->collector->hgetall($key_cm_stats);
@@ -124,4 +150,22 @@ class CmtsCmStatsCommand extends BaseCmCommand
         $this->output->writeln("Tiempo: $time segundos");
     }
 
+    private function sla($ip)
+    {
+        $kernel = $this->getContainer()->get('kernel');
+
+        $application = new Application($kernel);
+        $application->setAutoExit(false);
+
+        $args = ['','ping',$ip];
+        
+        $input = new ArgvInput($args);
+
+        $output = new BufferedOutput();
+
+        $application->run($input, $output);
+
+        return $output->fetch();
+    }
+
 }

+ 108 - 0
Command/PingCommand.php

@@ -0,0 +1,108 @@
+<?php
+
+namespace CmtsBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class PingCommand  extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('ping')
+            ->setDescription('Ping to host and return SLA metrics')
+            ->setHelp('Se requieren parámetros para poder realizar la correcta consulta.')
+            ->setDefinition(array(
+                new InputArgument('ip', InputArgument::OPTIONAL,"IP"),
+                new InputOption('timeout', null, InputOption::VALUE_NONE,"Timeout", null),
+                new InputOption('interval', null, InputOption::VALUE_NONE,"Interval", null),
+                new InputOption('count', null, InputOption::VALUE_NONE,"Count", null)
+            ))
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $ip = $input->getArgument('ip');
+        
+        if(!$ip) return $output->writeln(json_encode(array('message' => 'IP is required')));
+        
+        $timeout = $input->getOption('timeout');
+        $interval = $input->getOption('interval');
+        $count = $input->getOption('count');
+
+        $data = $this->ping($ip, $timeout, $interval, $count);
+        if(!is_array($data)) {
+            $data = array();
+        }
+
+        $output->writeln(json_encode($data));
+    }
+
+
+    function ping($ip, $timeout = 1, $int = 0.5, $i = 5) {
+        
+        if(! $timeout ) $timeout = 1;
+        if(! $int ) $int = 0.5;
+        if(! $i ) $i = 5;
+
+
+        /* ICMP ping packet with a pre-calculated checksum */
+        $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
+        $result = array();
+        $socket  = socket_create(AF_INET, SOCK_RAW, 1);
+        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
+        socket_connect($socket, $ip, null);
+
+        $n = $c = $total = 0; 
+        $jitter = array();
+        $last = null;
+
+        for($a = 1; $a <= $i; $a++) {
+            $ts = microtime(true);
+            socket_send($socket, $package, strLen($package), 0);
+            if (socket_read($socket, 255)) {
+                $r = ((microtime(true) - $ts) / 2) * 1000;
+                if(is_null($last)) {
+                    $last = $r;
+                } else {
+                    $jitter[] = abs(($last - $r));
+                }
+                $total += $r;
+                $result[] = $r;
+                $c++;
+            } else {
+                $result[] = false;
+                $n++;
+            }
+
+            if ($int< 1) {usleep($int * 1000000);}else{sleep($int);}
+
+        }
+
+        socket_close($socket);
+
+        $data = array();
+
+        if($c > 0) {
+            $data['latency'] = round(($total) / $c,2);
+        }
+        if($jitter) {
+            $data['jitter'] = round(array_sum($jitter) / count($jitter),2);
+        }
+        if($i > 0) {
+            $data['loss'] = round(($n / $i) * 100,2);
+        }
+
+        return $data;
+    }
+}