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: 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); } }