Преглед изворни кода

Merged in FD3-231 (pull request #16)

FD3-231
Guillermo Espinoza пре 7 година
родитељ
комит
09eb0b47ba

+ 5 - 0
app/config/parameters.yml.dist

@@ -35,3 +35,8 @@ parameters:
     geoserver_user: admin
     geoserver_pass: geoserver
     geoserver_path_shapes: '/var/flowdat/geoserver-shapes'
+
+    # Url del webservice de crontab para crear los archivos
+    url_crontab: http://127.0.0.1/cron.php?wsdl
+
+    env(HOST_BASE): base

+ 2 - 0
app/config/parameters.yml.docker

@@ -44,3 +44,5 @@ parameters:
     geoserver_user: admin
     geoserver_pass: geoserver
     geoserver_path_shapes: '/var/flowdat/geoserver-shapes'
+
+    url_crontab: http://supervisord/cron.php?wsdl

+ 1 - 0
composer.json

@@ -152,6 +152,7 @@
         "ik/webservice-bundle": "dev-master",
         "ik/workflow-bundle": "dev-workflow_update",
         "ik/auth-bundle": "dev-master",
+        "ik/workflow-bundle": "dev-master",
         "incenteev/composer-parameter-handler": "^2.0",
         "jms/serializer-bundle": "^1.1",
         "opensolutions/oss-snmp": "dev-master",

+ 4 - 4
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "90f173f667f24759c462b49b2af73460",
+    "content-hash": "473e31584e4af0ad5d5e8b248db675b1",
     "packages": [
         {
             "name": "behat/transliterator",
@@ -1651,11 +1651,11 @@
         },
         {
             "name": "ik/workflow-bundle",
-            "version": "dev-workflow_update",
+            "version": "dev-master",
             "source": {
                 "type": "git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/WorkflowBundle.git",
-                "reference": "d1a52302c4d86dea6b3a39b6100ac03f2b3eb65e"
+                "reference": "8a30206efe0b94fd556c22b5bebdb2931623f29f"
             },
             "require": {
                 "php-amqplib/rabbitmq-bundle": "^1.12"
@@ -1685,7 +1685,7 @@
                 "bundle",
                 "workflow"
             ],
-            "time": "2017-10-12T15:00:52+00:00"
+            "time": "2017-10-13T11:07:50+00:00"
         },
         {
             "name": "incenteev/composer-parameter-handler",

+ 169 - 0
src/StatsBundle/Command/GenerateRemoteCrontabCommand.php

@@ -0,0 +1,169 @@
+<?php
+
+namespace StatsBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class GenerateRemoteCrontabCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('stats:crontab:remote')
+            ->setDescription('Generate Remote Stats Crontab File')
+            ->setHelp('El comando genera/actualiza el archivo crontab de manera remota para realizar consultas a dispositivos')
+            ->setDefinition(array(
+                new InputOption('file-crontab', false, InputOption::VALUE_OPTIONAL, "File Crontab","/etc/cron.d/fd3_stats"),
+                new InputOption('path-app', false, InputOption::VALUE_OPTIONAL, "Path App"),
+                new InputOption('url', false, InputOption::VALUE_OPTIONAL, "Crontab webservice url"),
+                new InputOption('amqp', false, InputOption::VALUE_OPTIONAL, "Execute the commands via amqp", true),
+                new InputOption('routing_key', false, InputOption::VALUE_OPTIONAL, "AMQP Routing key"),
+            ))
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $this->output = $output;
+        $fileCrontab = $input->getOption('file-crontab');
+        $pathApp = $input->getOption('path-app');
+        $url = $input->getOption('url');
+        if (is_null($url)) {
+            if ($this->getContainer()->hasParameter('url_crontab')) {
+                $url = $this->getContainer()->getParameter('url_crontab');
+            } else {
+                $output->writeln('<error>ERROR:</error> Debe definir una url para generar el crontab.');
+                $output->writeln($this->getSynopsis());
+
+                return;
+            }
+        }
+
+        $times = array();
+        $now = date("d-m-Y H:i:s");
+        $content = PHP_EOL."# NO EDITAR este archivo, se autogenera con el comando stats:crontab:remote. Generado {$now}.".PHP_EOL;
+
+        $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
+        
+        if(is_null($pathApp)) {
+            $pathApp = $this->getContainer()->getParameter('app_path');
+        }
+        $pathConsole = "root $(wich php) {$pathApp}/bin/console"; 
+        
+        $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
+        
+        $oltMarks = array('FiberHome','FiberLink','Huawei');
+        $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1');
+
+        $amqp = $input->getOption('amqp');
+        $amqpRemote = '';
+        $routing_key = '';
+        if ($amqp) {
+            $amqpRemote = 'amqp:remote';
+            
+            // Verifico la routing key si se pasa como parametro o variable de entorno
+            // default routing_key = stats
+            $routing_key = $input->getOption('routing_key');
+            if (!$routing_key) {
+                $routing_key = getenv('AMQP_KEY') !== false ? getenv('AMQP_KEY') : 'stats';
+            }
+            $routing_key = "--route={$routing_key}";
+        }
+        
+        $content .= "*/5 * * * * {$pathConsole} {$amqpRemote} {$routing_key} generate:crontab".PHP_EOL;
+
+        foreach($serverDevices as $server) {
+
+            $content .= PHP_EOL.PHP_EOL."# SERVER {$server->getName()} / {$server->getUrl()}".PHP_EOL;
+        
+            // APARTADO OLT
+            $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT','deviceServer' => $server));
+
+            $serverId = $server->getId();
+
+            foreach($oltDevices as $device) {
+                $commands = array();
+                $data = $device->jsonExtraData();
+                
+                if($data['executeSnmp'] == 0) continue;
+
+                if(!in_array($data['mark'],$oltMarks)) continue;
+                
+                if(!in_array($data['library'],$oltLibraries)) continue;
+
+                $oltName = $data['name'];
+                $mark = strtolower($data['mark']);
+                $library = $data['library'];
+                $snmpCommunity = $data['snmpCommunity'];
+                $deviceIp = $device->getIp();
+                $deviceId = $device->getDeviceId();
+
+                $timeScan = $data['timeScan'];
+                $timeOnuStats = $data['timeOnuStats'];
+                $timePonStats = $data['timePonStats'];
+                $timeOltOctets = $data['timeOltOctets'];
+
+                $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId}";
+                if ($amqp) {
+                    $params = "--args=--olt-ip:{$deviceIp} --args=--olt-community:{$snmpCommunity} --args=--olt-snmp-library:{$library} --args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}";
+                }
+
+                $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:scan {$params}";
+                $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:scan {$params}";
+                
+                $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:stats {$params}";
+                $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:stats {$params}";
+                $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:octets {$params}";
+                
+                $params = '--olt-device-id={$deviceId} --olt-server-id={$serverId}';
+                if ($amqp) {
+                    $params = '--args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}';
+                }
+                $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu {$params}";
+                $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport {$params}";
+                
+                $content .= PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL;
+                
+                $content .= implode(PHP_EOL, $commands);
+            }
+            
+            if($this->getContainer()->getParameter('geoserver_service')) {
+                $content .= PHP_EOL.PHP_EOL."# MAPAS".PHP_EOL;
+                
+                $params = '--olt-server-id={$serverId}';
+                if ($amqp) {
+                    $params = '--args=--olt-server-id:{$serverId}';
+                }
+                
+                $commands = array();
+                $commands[] = "*/5 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu:geo {$params}";
+                $commands[] = "*/10 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport:geo {$params}";
+                
+                $content .= implode(PHP_EOL, $commands);
+                $content .= PHP_EOL;
+            }
+        }
+        $this->soap($url, $fileCrontab, $content);
+    }
+
+    /**
+    * @param string $url
+    * @param string $filename
+    * @param string $content
+    */
+    protected function soap($url, $filename, $content)
+    {
+        $client = new \SoapClient($url);
+        $result = $client->__soapCall('crontab', compact('filename', 'content'));
+        $this->output->writeln($result); 
+    }
+
+}