GenerateCrontabCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $now = date("d-m-Y H:i:s");
  29. $content = PHP_EOL . "# NO EDITAR este archivo, se autogenera con el comando stats:crontab:remote. Generado {$now}." . PHP_EOL;
  30. $fileCrontab = $input->getOption('file-crontab');
  31. $pathApp = $input->getOption('path-app');
  32. $file = $this->getContainer()->get('kernel')->getRootDir()."/Resources/stats_crontab";
  33. $handle = fopen($file, "w");
  34. $times = array();
  35. $now = date("d-m-Y H:i:s");
  36. fwrite($handle, PHP_EOL."# NO EDITAR este archivo, se autogenera con el comando generate:crontab. Generado {$now}.".PHP_EOL);
  37. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  38. if(is_null($pathApp)) {
  39. $pathApp = $this->getContainer()->getParameter('app_path');
  40. }
  41. $pathConsole = "root $(which php) {$pathApp}/bin/console";
  42. $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
  43. $oltMarks = array('FiberHome','FiberLink','Huawei','ZTE','Calix');
  44. $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1','OIDSFiberLinkV1','OIDSZTEV1','OIDSCalixV1');
  45. fwrite($handle, "*/5 * * * * {$pathConsole} generate:crontab".PHP_EOL);
  46. fwrite($handle, "*/30 * * * * {$pathConsole} stats:devices".PHP_EOL);
  47. foreach($serverDevices as $server) {
  48. fwrite($handle, PHP_EOL.PHP_EOL."# SERVER {$server->getName()} / {$server->getUrl()}".PHP_EOL);
  49. // APARTADO OLT
  50. $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT','deviceServer' => $server));
  51. $serverId = $server->getId();
  52. $saveHistoric = $server->getSaveHistoric();
  53. ($saveHistoric)? $saveHistoric = 1 : $saveHistoric = 0;
  54. foreach($oltDevices as $device) {
  55. $commands = array();
  56. $data = $device->jsonExtraData();
  57. if($data['executeSnmp'] == 0) continue;
  58. if(!in_array($data['mark'],$oltMarks)) continue;
  59. if(!in_array($data['library'],$oltLibraries)) continue;
  60. $oltName = $data['name'];
  61. $mark = strtolower($data['mark']);
  62. $library = $data['library'];
  63. $snmpCommunity = $data['snmpCommunity'];
  64. $deviceIp = $device->getIp();
  65. $deviceId = $device->getDeviceId();
  66. $timeScan = $data['timeScan'];
  67. $timeOltScan = $data['timeOltScan'];
  68. $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId} --save-historic={$saveHistoric}";
  69. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:onu:scan {$params}";
  70. $commands[] = "*/{$timeOltScan} * * * * {$pathConsole} {$mark}:olt:scan {$params}";
  71. fwrite($handle, PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL);
  72. fwrite($handle, implode(PHP_EOL,$commands).PHP_EOL.PHP_EOL);
  73. }
  74. // APARTADO NAS
  75. $nasDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'RadiusBundle\Entity\NAS', 'deviceServer' => $server));
  76. foreach ($nasDevices as $device) {
  77. $commands = array();
  78. $data = $device->jsonExtraData();
  79. $deviceId = $device->getDeviceId();
  80. if($data['executeSnmp'] == 0) continue;
  81. if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
  82. continue;
  83. }
  84. if (!in_array($data['library'], array('OIDSBase'))){
  85. continue;
  86. }
  87. if (!$saveHistoric){
  88. continue;
  89. }
  90. $library = $data['library'];
  91. $snmpCommunity = $data['snmpCommunity'];
  92. $description = $data['description'];
  93. $deviceIp = $device->getIp();
  94. $params = "--nas-ip={$deviceIp} --nas-community={$snmpCommunity} --nas-snmp-library={$library} --nas-device-id={$deviceId} --nas-server-id={$serverId} --save-historic=1";
  95. $commands[] = "*/10 * * * * {$pathConsole} nas:onu:octets {$params}";
  96. fwrite($handle, PHP_EOL.PHP_EOL."# NAS {$description} ({$deviceIp})".PHP_EOL);
  97. fwrite($handle, PHP_EOL);
  98. fwrite($handle, implode(PHP_EOL,$commands).PHP_EOL.PHP_EOL);
  99. }
  100. }
  101. fwrite($handle, PHP_EOL . PHP_EOL);
  102. fclose($handle);
  103. @unlink($fileCrontab);
  104. if(symlink ($file , $fileCrontab)) print_r("Creado link al crontab".PHP_EOL);
  105. print_r(@shell_exec("/etc/init.d/cron reload"));
  106. }
  107. }