فهرست منبع

Se agrega comando para generar archivo crontab para ejecución de
comandos. Chequear los de los paths.

Maximiliano Schvindt 7 سال پیش
والد
کامیت
7868abcdb7
4فایلهای تغییر یافته به همراه118 افزوده شده و 1 حذف شده
  1. 1 0
      .gitignore
  2. 2 1
      app/config/parameters.yml.dist
  3. 1 0
      app/config/parameters.yml.docker
  4. 114 0
      src/StatsBundle/Command/GenerateCrontabCommand.php

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 /app/config/parameters.yml
 /app/config/parameters.yml
+/app/Resources/stats_crontab
 /build/
 /build/
 /phpunit.xml
 /phpunit.xml
 /var/*
 /var/*

+ 2 - 1
app/config/parameters.yml.dist

@@ -27,4 +27,5 @@ parameters:
     db_session_port: null
     db_session_port: null
     db_session_name: fd_session
     db_session_name: fd_session
     db_session_user: root
     db_session_user: root
-    db_session_password: null
+    db_session_password: null
+    app_path: '/var/flowdat/stats'

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

@@ -37,3 +37,4 @@ parameters:
     db_session_name: fd_session
     db_session_name: fd_session
     db_session_user: root
     db_session_user: root
     db_session_password: null
     db_session_password: null
+    app_path: '/var/flowdat/stats'

+ 114 - 0
src/StatsBundle/Command/GenerateCrontabCommand.php

@@ -0,0 +1,114 @@
+<?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;
+use StatsBundle\Services\DeviceManager;
+
+class GenerateCrontabCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('generate:crontab')
+            ->setDescription('Stats Crontab File')
+            ->setHelp('El comando genera/actualiza el archivo crontab 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")
+            ))
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+
+        $fileCrontab = $input->getOption('file-crontab');
+        $pathApp = $input->getOption('path-app');
+
+        $file = $this->getContainer()->get('kernel')->getRootDir()."/Resources/stats_crontab";
+        $handle = fopen($file, "w");
+
+        $times = array();
+        $now = date("d-m-Y H:i:s");
+        fwrite($handle, PHP_EOL."# NO EDITAR este archivo, se autogenera con el comando generate:crontab. 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');
+        $oltLibraries = array('OIDSFiberHomeV1');
+
+        fwrite($handle, "*/5 * * * * {$pathConsole} generate:crontab".PHP_EOL);
+
+        foreach($serverDevices as $server) {
+
+            fwrite($handle, 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->getId();
+
+                $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}";
+                $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:pon:scan {$params}";
+                $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:onu:scan {$params}";
+                
+                $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$mark}:onu:stats {$params}";
+                $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$mark}:pon:stats {$params}";
+                $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:pon:octets {$params}";
+                
+                fwrite($handle, PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL);
+                
+                fwrite($handle, implode(PHP_EOL,$commands));
+                fwrite($handle, PHP_EOL);
+            }
+        }
+
+        fclose($handle);
+        
+        @unlink($fileCrontab);
+        
+        if(symlink ($file , $fileCrontab)) print_r("Creado link al crontab".PHP_EOL);
+        
+        print_r(@shell_exec("/etc/init.d/cron reload"));
+
+    }
+
+}