Просмотр исходного кода

Se añade un comando que forkea los cmts:cm:stats. Por otro lado se actualiza el comando común de cmts:cm:stats para recibir una lista de CM y procesarlos linealmente.

Maxi Schvindt 6 лет назад
Родитель
Сommit
1cf1d10313
2 измененных файлов с 177 добавлено и 43 удалено
  1. 76 43
      Command/CmtsCmStatsCommand.php
  2. 101 0
      Command/ForkCmtsCmStatsCommand.php

+ 76 - 43
Command/CmtsCmStatsCommand.php

@@ -31,6 +31,7 @@ class CmtsCmStatsCommand extends BaseCmCommand
                 new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
                 new InputOption('cm-ip', false, InputOption::VALUE_OPTIONAL, "IP del CM"),
                 new InputOption('cm-mac', false, InputOption::VALUE_OPTIONAL, "MAC del CM"),
+                new InputOption('cm', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, "List of CM: --cm=ip1,mac1 --cm=ip2,mac2"),
                 new InputOption('cm-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS",'public'),
                 new InputOption('cm-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP",'OIDSBase'),
                 new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
@@ -45,13 +46,78 @@ class CmtsCmStatsCommand extends BaseCmCommand
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         parent::execute($input, $output);
-        
+
+        $statsdService = $this->getContainer()->get('statsd');
         
         $key_cm_stats = "cm_stats_{$this->d_s}"; // group by cmts
         $saveHistoric = (int) $input->getOption('save-historic');
         $inicio = microtime(true);
 
-        $SNMP = new SNMP($this->cmIp, $this->cmCommunity);
+        $count = 0;
+        
+        if(is_null($this->cmIp)) {
+            $cablemodems = $input->getOption('cm');
+            
+            if(is_null($cablemodems) || empty($cablemodems)) {
+                $this->removeLock($this->flag);
+                exit();
+            } 
+            
+            foreach($cablemodems as $dupla) {
+                list($ip, $mac) = explode(",",$dupla);
+                $metrics = $this->metrics($key_cm_stats, $ip, $mac);
+                $count++;
+                if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
+            }
+        
+        } else {
+            $metrics = $this->metrics($key_cm_stats, $this->cmIp, $this->cmMac);
+            $count++;
+            if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
+        }
+
+        /* End Of Blockout */
+        $this->removeLock($this->flag);
+
+        $fin = microtime(true);
+        $time = $fin - $inicio;
+        $this->output->writeln("Cantidad CM: {$count} / Tiempo: {$time} segundos / Date: ".date("Y-m-d H:i:s"));
+        
+        exit();
+    }
+
+    private function sendData($data, $service)
+    {
+        $t_start_script = microtime(true); 
+        $service->send($data);
+        $t_end_script = microtime(true); 
+        $time = $t_end_script - $t_start_script;
+        $this->output->writeln("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($data));
+
+    }
+
+    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();
+    }
+
+    private function metrics($key, $ip, $mac)
+    {
+        $inicio = microtime(true);
+        $SNMP = new SNMP($ip, $this->cmCommunity);
         $SNMP->setTimeout(2500000); // 2.5 seconds of timeout
         $SNMP->setRetry(1); // 1 query per metric
         $library = "use".$this->cmSnmpLibrary;
@@ -70,7 +136,7 @@ class CmtsCmStatsCommand extends BaseCmCommand
             $fin = microtime(true);
             $time = $fin - $inicio;
             $this->output->writeln("CM no responde uptime, tx y rx => cortamos! Tiempo: $time segundos");
-            return true;
+            return false;
         }
 
         $signal = $this->getSNMP("docsIfSigQSignalNoise","cmSignal");
@@ -79,7 +145,7 @@ class CmtsCmStatsCommand extends BaseCmCommand
         $correcteds = $this->getSNMP("docsIfSigQCorrecteds","cmCorrecteds");
         $uncorrectables = $this->getSNMP("docsIfSigQUncorrectables","cmUncorrectables");
 
-        $slaJson = $this->sla($this->cmIp);
+        $slaJson = $this->sla($ip);
         try{
             $sla = json_decode($slaJson,true);
         } catch(\Exception $e) {
@@ -102,7 +168,7 @@ class CmtsCmStatsCommand extends BaseCmCommand
 
             foreach($$var as $channel => $value) {
                 $v = $value * $multiplicator;
-                $_m = "{$m}{$this->cmMac}.{$channel}";
+                $_m = "{$m}{$mac}.{$channel}";
                 $data[$var][$channel] = $v;
                 $sendData[$_m] = "{$v}|g";
             }
@@ -116,56 +182,23 @@ class CmtsCmStatsCommand extends BaseCmCommand
 
         if(isset($sla['latency'])) {
             $data['latency'] = $sla['latency'];
-            $sendData["cm_latency_{$this->cmMac}"] = "{$sla['latency']}|g";
+            $sendData["cm_latency_{$mac}"] = "{$sla['latency']}|g";
         }
         
         if(isset($sla['jitter'])) {
             $data['jitter'] = $sla['jitter'];
-            $sendData["cm_jitter_{$this->cmMac}"] = "{$sla['jitter']}|g";
+            $sendData["cm_jitter_{$mac}"] = "{$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);
-
-        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;
-            $this->output->writeln("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData));
+            $sendData["cm_loss_{$mac}"] = "{$sla['loss']}|g";
         }
 
-        /* End Of Blockout */
-        $this->removeLock($this->flag);
-
-        $fin = microtime(true);
-        $time = $fin - $inicio;
-        $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();
+        $this->hset($key, $mac, $data, true);
 
-        $application->run($input, $output);
+        return $sendData;
 
-        return $output->fetch();
     }
 
 }

+ 101 - 0
Command/ForkCmtsCmStatsCommand.php

@@ -0,0 +1,101 @@
+<?php
+# 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.50.2.3 --cm-mac=001a6663dcde --cm-community=public
+
+namespace CmtsBundle\Command;
+
+use BaseStatsBundle\Command\BaseCmCommand;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputArgument;
+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;
+use Symfony\Component\Process\Process;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+
+use Graze\ParallelProcess\Pool;
+
+class ForkCmtsCmStatsCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('fork:cmts:cm:stats')
+            ->setDescription('Obtener estadísticas de un CM')
+            ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
+            ->setDefinition(array(
+                new InputOption('cm', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, "List of CM: --cm=ip1,mac1 --cm=ip2,mac2"),
+                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('cm-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS",'public'),
+                new InputOption('cm-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP",'OIDSBase'),
+                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)
+    {
+        $this->inicio = microtime(true);
+        $pool = new Pool();
+        
+        $exception = function(Process $process, $duration) {throw new ProcessFailedException($process);};
+        
+        $pool->setOnFailure($exception);
+        
+        $cablemodems = $input->getOption('cm');
+        $cmtsDeviceId = (int) $input->getOption('cmts-device-id');
+        $cmtsServerId = (int) $input->getOption('cmts-server-id');
+        $cmCommunity = $input->getOption('cm-community');
+        $cmSnmpLibrary = $input->getOption('cm-snmp-library');
+        $saveHistoric = (int) $input->getOption('save-historic');
+        
+        if(is_null($cablemodems) || empty($cablemodems)) exit();
+
+        $path = "/opt/stats/bin/console";
+        $cmd = "cmts:cm:stats";
+
+        $args = "--cmts-device-id={$cmtsDeviceId} --cmts-server-id={$cmtsServerId} --cm-community={$cmCommunity} --cm-snmp-library={$cmSnmpLibrary} --save-historic={$saveHistoric}";
+
+        $total = 0;
+        foreach($cablemodems as $dupla) {
+            list($ip, $mac) = explode(",",$dupla);
+            if(is_null($ip) || is_null($mac)) continue;
+
+            $process = "{$path} {$cmd} {$args} --cm-ip={$ip} --cm-mac={$mac}";
+            
+            $pool->add(new Process($process));
+            $total++;
+        }
+        
+        try {
+            $pool->run(); // blocking that will run till it finishes
+        } catch(\ProcessFailedException $e) {
+            print_r($e);
+            print_r(PHP_EOL);
+        }
+
+        $this->fin = microtime(true);
+        
+        print_r("Fin");
+        print_r(PHP_EOL);
+
+        exit(0);
+
+        
+        
+    }
+
+}