123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?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", "/opt/stats"),
- 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 = "SHELL=/bin/bash".PHP_EOL."BASH_ENV=/container.env".PHP_EOL;
- $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 = "{$pathApp}/bin/console";
- $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
- $oltMarks = array('FiberHome','FiberLink','Huawei','ZTE','Calix');
- $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1','OIDSFiberLinkV1','OIDSZTEV1','OIDSCalixV1');
- $amqp = $input->getOption('amqp');
- $amqpRemote = 'stats:amqp';
- $content .= "*/5 * * * * {$pathConsole} stats:crontab:remote >> /var/log/cron.log 2>&1" . PHP_EOL;
- $content .= "*/20 * * * * {$pathConsole} stats:devices >> /var/log/cron.log 2>&1" . 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();
- $saveHistoric = $server->getSaveHistoric();
- ($saveHistoric)? $saveHistoric = 1 : $saveHistoric = 0;
- foreach ($oltDevices as $device) {
- $commands = array();
- $data = $device->jsonExtraData();
- $deviceId = $device->getDeviceId();
- if(isset($data['timeDiscovery']) && $data['timeDiscovery'] > 0) {
- $timeDiscovery = (int) $data['timeDiscovery'];
- $params = "--args=--action:Discovery --args=--entity:FTTHBundle\\\\\Entity\\\\\OLT --args=--id:{$deviceId}";
- $content .= PHP_EOL . PHP_EOL . "# Autodiscovery {$deviceId} is OK " . PHP_EOL;
- $content .= "*/{$timeDiscovery} * * * * {$pathConsole} amqp:remote --route=ftth workflow:run:action {$params} >> /var/log/cron.log 2>&1" . PHP_EOL;
- } else {
- $content .= PHP_EOL . PHP_EOL . "# Autodiscovery {$deviceId} skiped, timeDiscovery is 0 or null or undefined" . PHP_EOL;
- }
- if ($data['executeSnmp'] == 0){
- $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, executeSnmp is 0" . PHP_EOL;
- continue;
- }
- if (!in_array($data['mark'], $oltMarks)){
- $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, mark is not one of " . implode(", ", $oltMarks). PHP_EOL;
- continue;
- }
- if (!in_array($data['library'], $oltLibraries)){
- $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, library is not one of " . implode(", ", $oltLibraries). PHP_EOL;
- continue;
- }
- $oltName = $data['name'];
- $mark = strtolower($data['mark']);
- $library = $data['library'];
- $snmpCommunity = $data['snmpCommunity'];
- $deviceIp = $device->getIp();
- $timeScan = $data['timeScan'];
- $timeOltScan = $data['timeOltScan'];
- $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId} --save-historic={$saveHistoric}";
- 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} --args=--save-historic:{$saveHistoric}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$mark}:onu:scan {$params} >> /var/log/cron.log 2>&1";
- $commands[] = "*/{$timeOltScan} * * * * {$pathConsole} {$amqpRemote} {$mark}:olt:scan {$params} >> /var/log/cron.log 2>&1";
-
- $content .= PHP_EOL . "# OLT {$oltName} ({$deviceIp})" . PHP_EOL;
- $content .= implode(PHP_EOL, $commands);
- $content .= PHP_EOL . PHP_EOL;
- }
- // APARTADO NAS
- $nasDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'RadiusBundle\Entity\NAS', 'deviceServer' => $server));
- foreach ($nasDevices as $device) {
- $commands = array();
- $data = $device->jsonExtraData();
- $deviceId = $device->getDeviceId();
- if ($data['executeSnmp'] == 0){
- $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, executeSnmp is 0" . PHP_EOL;
- continue;
- }
- if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
- $content .= PHP_EOL . PHP_EOL . "# Device(NAS) {$deviceId} skiped, snmpCommunity is null or empty" . PHP_EOL;
- continue;
- }
- if (!in_array($data['library'], array('OIDSBase'))){
- $content .= PHP_EOL . PHP_EOL . "# Device(NAS) {$deviceId} skiped, library not exist" . PHP_EOL;
- continue;
- }
-
- if (!$saveHistoric){
- $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, because saveHistoric is 0, set Server options in Stats". PHP_EOL;
- continue;
- }
-
- $library = $data['library'];
- $snmpCommunity = $data['snmpCommunity'];
- $description = $data['description'];
- $deviceIp = $device->getIp();
- $params = "--nas-ip={$deviceIp} --nas-community={$snmpCommunity} --nas-snmp-library={$library} --nas-device-id={$deviceId} --nas-server-id={$serverId} --save-historic=1";
- if ($amqp)
- $params = "--args=--nas-ip:{$deviceIp} --args=--nas-community:{$snmpCommunity} --args=--nas-snmp-library:{$library} --args=--nas-device-id:{$deviceId} --args=--nas-server-id:{$serverId} --args=--save-historic:1";
- $commands[] = "*/10 * * * * {$pathConsole} {$amqpRemote} nas:onu:octets {$params} >> /var/log/cron.log 2>&1";
- $content .= PHP_EOL . "# NAS {$description} ({$deviceIp})" . PHP_EOL;
- $content .= implode(PHP_EOL, $commands);
- $content .= PHP_EOL . PHP_EOL;
- }
- }
- $content .= PHP_EOL."#END ".PHP_EOL.PHP_EOL;
- file_put_contents($fileCrontab, $content);
- sleep(5);
- print_r(shell_exec("/usr/bin/crontab /etc/cron.d/fd3_stats"));
- //print_r(shell_exec("/usr/local/bin/supervisorctl -uiksop -pqueRini6 restart cron"));
- }
- }
|