123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?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)
- {
- $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;
-
- $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 $(which php) {$pathApp}/bin/console";
-
- $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
-
- $oltMarks = array('FiberHome','FiberLink','Huawei','ZTE','Calix');
- $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1','OIDSFiberLinkV1','OIDSZTEV1','OIDSCalixV1');
- fwrite($handle, "*/5 * * * * {$pathConsole} generate:crontab".PHP_EOL);
- foreach($serverDevices as $server) {
- fwrite($handle, 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();
- 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}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:pon:scan {$params}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:onu:scan {$params}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:olt:scan {$params}";
-
- $params .= " --save-historic={$saveHistoric}";
- $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$mark}:onu:stats {$params}";
- $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$mark}:pon:stats {$params}";
- if($mark == "huawei") {
- $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:onu:octets {$params}";
- }
- $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:pon:octets {$params}";
-
- $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:onu --olt-device-id={$deviceId} --olt-server-id={$serverId}";
- $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:ponport --olt-device-id={$deviceId} --olt-server-id={$serverId}";
-
- fwrite($handle, PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL);
-
- fwrite($handle, implode(PHP_EOL,$commands));
- }
-
- if($this->getContainer()->getParameter('geoserver_service')) {
- fwrite($handle, PHP_EOL.PHP_EOL."# MAPAS".PHP_EOL);
-
- $commands = array();
- $commands[] = "*/5 * * * * {$pathConsole} stats:onu:geo --olt-server-id={$serverId}";
- $commands[] = "*/10 * * * * {$pathConsole} stats:ponport:geo --olt-server-id={$serverId}";
-
- fwrite($handle, implode(PHP_EOL,$commands));
-
- fwrite($handle, 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 (is_null($data['radiusPassword']) || empty($data['radiusPassword'])){
- continue;
- }
- if (!in_array($data['library'], array('OIDSBase'))){
- continue;
- }
-
- if (!$saveHistoric){
- continue;
- }
-
- $library = $data['library'];
- $snmpCommunity = $data['radiusPassword'];
- $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";
- $commands[] = "*/10 * * * * {$pathConsole} nas:onu:octets {$params}";
-
- fwrite($handle, PHP_EOL.PHP_EOL."# NAS {$description} ({$deviceIp})".PHP_EOL);
- fwrite($handle, PHP_EOL);
- fwrite($handle, implode(PHP_EOL,$commands));
-
- }
- // APARTADO CMTS
- $cmtsDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'CablemodemBundle\Entity\CMTS', 'deviceServer' => $server));
- foreach ($cmtsDevices as $device) {
- $commands = array();
- $data = $device->jsonExtraData();
- $deviceId = $device->getDeviceId();
- if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
- $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, snmpCommunity is null or empty" . PHP_EOL;
- continue;
- }
- if (!in_array($data['library'], array('OIDSBase'))){
- $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, library not exist" . PHP_EOL;
- continue;
- }
-
- if (!$data['executeSnmp']){
- $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, because executeSnmp is 0". PHP_EOL;
- continue;
- }
-
- $library = $data['library'];
- $snmpCommunity = $data['snmpCommunity'];
- $description = $data['name'];
- $deviceIp = $device->getIp();
- $timeScan = $data['timeScan'];
- $params = "--cmts-ip={$deviceIp} --cmts-community={$snmpCommunity} --cmts-snmp-library={$library} --cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
-
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:cm:scan {$params}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:scan {$params}";
-
- $params .= " --save-historic={$saveHistoric}";
-
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:stats {$params}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:description {$params}";
-
- $params = "--cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
- $commands[] = "*/{$timeScan} * * * * {$pathConsole} stats:interface {$params}";
-
- fwrite($handle, PHP_EOL.PHP_EOL."# CMTS {$description} ({$deviceIp})".PHP_EOL);
- fwrite($handle, PHP_EOL);
- fwrite($handle, implode(PHP_EOL,$commands));
- }
- }
- 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"));
- }
- }
|