GenerateCrontabCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace StatsBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use StatsBundle\Services\DeviceManager;
  8. class GenerateCrontabCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('generate:crontab')
  14. ->setDescription('Stats Crontab File')
  15. ->setHelp('El comando genera/actualiza el archivo crontab para realizar consultas a dispositivos')
  16. ->setDefinition(array(
  17. new InputOption('file-crontab', false, InputOption::VALUE_OPTIONAL, "File Crontab","/etc/cron.d/fd3_stats"),
  18. new InputOption('path-app', false, InputOption::VALUE_OPTIONAL, "Path App")
  19. ))
  20. ;
  21. }
  22. /**
  23. * @param InputInterface $input
  24. * @param OutputInterface $output
  25. */
  26. protected function execute(InputInterface $input, OutputInterface $output)
  27. {
  28. $fileCrontab = $input->getOption('file-crontab');
  29. $pathApp = $input->getOption('path-app');
  30. $file = $this->getContainer()->get('kernel')->getRootDir()."/Resources/stats_crontab";
  31. $handle = fopen($file, "w");
  32. $times = array();
  33. $now = date("d-m-Y H:i:s");
  34. fwrite($handle, PHP_EOL."# NO EDITAR este archivo, se autogenera con el comando generate:crontab. Generado {$now}.".PHP_EOL);
  35. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  36. if(is_null($pathApp)) {
  37. $pathApp = $this->getContainer()->getParameter('app_path');
  38. }
  39. $pathConsole = "root $(wich php) {$pathApp}/bin/console";
  40. $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
  41. $oltMarks = array('FiberHome','FiberLink','Huawei','ZTE','Calix');
  42. $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1','OIDSFiberLinkV1','OIDSZTEV1','OIDSCalixV1');
  43. fwrite($handle, "*/5 * * * * {$pathConsole} generate:crontab".PHP_EOL);
  44. foreach($serverDevices as $server) {
  45. fwrite($handle, PHP_EOL.PHP_EOL."# SERVER {$server->getName()} / {$server->getUrl()}".PHP_EOL);
  46. // APARTADO OLT
  47. $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT','deviceServer' => $server));
  48. $serverId = $server->getId();
  49. $saveHistoric = $server->getSaveHistoric();
  50. ($saveHistoric)? $saveHistoric = 1 : $saveHistoric = 0;
  51. foreach($oltDevices as $device) {
  52. $commands = array();
  53. $data = $device->jsonExtraData();
  54. if($data['executeSnmp'] == 0) continue;
  55. if(!in_array($data['mark'],$oltMarks)) continue;
  56. if(!in_array($data['library'],$oltLibraries)) continue;
  57. $oltName = $data['name'];
  58. $mark = strtolower($data['mark']);
  59. $library = $data['library'];
  60. $snmpCommunity = $data['snmpCommunity'];
  61. $deviceIp = $device->getIp();
  62. $deviceId = $device->getDeviceId();
  63. $timeScan = $data['timeScan'];
  64. $timeOnuStats = $data['timeOnuStats'];
  65. $timePonStats = $data['timePonStats'];
  66. $timeOltOctets = $data['timeOltOctets'];
  67. $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  68. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:pon:scan {$params}";
  69. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:onu:scan {$params}";
  70. $params .= " --save-historic={$saveHistoric}";
  71. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$mark}:onu:stats {$params}";
  72. $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$mark}:pon:stats {$params}";
  73. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:pon:octets {$params}";
  74. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:onu --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  75. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:ponport --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  76. fwrite($handle, PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL);
  77. fwrite($handle, implode(PHP_EOL."#",$commands));
  78. }
  79. if($this->getContainer()->getParameter('geoserver_service')) {
  80. fwrite($handle, PHP_EOL.PHP_EOL."# MAPAS".PHP_EOL);
  81. $commands = array();
  82. $commands[] = "*/5 * * * * {$pathConsole} stats:onu:geo --olt-server-id={$serverId}";
  83. $commands[] = "*/10 * * * * {$pathConsole} stats:ponport:geo --olt-server-id={$serverId}";
  84. fwrite($handle, implode(PHP_EOL,$commands));
  85. fwrite($handle, PHP_EOL);
  86. }
  87. }
  88. fclose($handle);
  89. @unlink($fileCrontab);
  90. if(symlink ($file , $fileCrontab)) print_r("Creado link al crontab".PHP_EOL);
  91. print_r(@shell_exec("/etc/init.d/cron reload"));
  92. }
  93. }