GenerateCrontabCommand.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. foreach($serverDevices as $server) {
  47. fwrite($handle, PHP_EOL.PHP_EOL."# SERVER {$server->getName()} / {$server->getUrl()}".PHP_EOL);
  48. // APARTADO OLT
  49. $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT','deviceServer' => $server));
  50. $serverId = $server->getId();
  51. $saveHistoric = $server->getSaveHistoric();
  52. ($saveHistoric)? $saveHistoric = 1 : $saveHistoric = 0;
  53. foreach($oltDevices as $device) {
  54. $commands = array();
  55. $data = $device->jsonExtraData();
  56. if($data['executeSnmp'] == 0) continue;
  57. if(!in_array($data['mark'],$oltMarks)) continue;
  58. if(!in_array($data['library'],$oltLibraries)) continue;
  59. $oltName = $data['name'];
  60. $mark = strtolower($data['mark']);
  61. $library = $data['library'];
  62. $snmpCommunity = $data['snmpCommunity'];
  63. $deviceIp = $device->getIp();
  64. $deviceId = $device->getDeviceId();
  65. $timeScan = $data['timeScan'];
  66. $timeOnuStats = $data['timeOnuStats'];
  67. $timePonStats = $data['timePonStats'];
  68. $timeOltOctets = $data['timeOltOctets'];
  69. $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  70. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:pon:scan {$params}";
  71. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:onu:scan {$params}";
  72. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$mark}:olt:scan {$params}";
  73. $params .= " --save-historic={$saveHistoric}";
  74. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$mark}:onu:stats {$params}";
  75. $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$mark}:pon:stats {$params}";
  76. if($mark == "huawei") {
  77. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:onu:octets {$params}";
  78. }
  79. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$mark}:pon:octets {$params}";
  80. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:onu --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  81. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} stats:ponport --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  82. fwrite($handle, PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL);
  83. fwrite($handle, implode(PHP_EOL,$commands));
  84. }
  85. if($this->getContainer()->getParameter('geoserver_service')) {
  86. fwrite($handle, PHP_EOL.PHP_EOL."# MAPAS".PHP_EOL);
  87. $commands = array();
  88. $commands[] = "*/5 * * * * {$pathConsole} stats:onu:geo --olt-server-id={$serverId}";
  89. $commands[] = "*/10 * * * * {$pathConsole} stats:ponport:geo --olt-server-id={$serverId}";
  90. fwrite($handle, implode(PHP_EOL,$commands));
  91. fwrite($handle, PHP_EOL);
  92. }
  93. // APARTADO NAS
  94. $nasDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'RadiusBundle\Entity\NAS', 'deviceServer' => $server));
  95. foreach ($nasDevices as $device) {
  96. $commands = array();
  97. $data = $device->jsonExtraData();
  98. $deviceId = $device->getDeviceId();
  99. if (is_null($data['radiusPassword']) || empty($data['radiusPassword'])){
  100. continue;
  101. }
  102. if (!in_array($data['library'], array('OIDSBase'))){
  103. continue;
  104. }
  105. if (!$saveHistoric){
  106. continue;
  107. }
  108. $library = $data['library'];
  109. $snmpCommunity = $data['radiusPassword'];
  110. $description = $data['description'];
  111. $deviceIp = $device->getIp();
  112. $params = "--nas-ip={$deviceIp} --nas-community={$snmpCommunity} --nas-snmp-library={$library} --nas-device-id={$deviceId} --nas-server-id={$serverId} --save-historic=1";
  113. $commands[] = "*/10 * * * * {$pathConsole} nas:onu:octets {$params}";
  114. fwrite($handle, PHP_EOL.PHP_EOL."# NAS {$description} ({$deviceIp})".PHP_EOL);
  115. fwrite($handle, PHP_EOL);
  116. fwrite($handle, implode(PHP_EOL,$commands));
  117. }
  118. // APARTADO CMTS
  119. $cmtsDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'CablemodemBundle\Entity\CMTS', 'deviceServer' => $server));
  120. foreach ($cmtsDevices as $device) {
  121. $commands = array();
  122. $data = $device->jsonExtraData();
  123. $deviceId = $device->getDeviceId();
  124. if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
  125. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, snmpCommunity is null or empty" . PHP_EOL;
  126. continue;
  127. }
  128. if (!in_array($data['library'], array('OIDSBase'))){
  129. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, library not exist" . PHP_EOL;
  130. continue;
  131. }
  132. if (!$data['executeSnmp']){
  133. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, because executeSnmp is 0". PHP_EOL;
  134. continue;
  135. }
  136. $library = $data['library'];
  137. $snmpCommunity = $data['snmpCommunity'];
  138. $description = $data['name'];
  139. $deviceIp = $device->getIp();
  140. $timeScan = $data['timeScan'];
  141. $params = "--cmts-ip={$deviceIp} --cmts-community={$snmpCommunity} --cmts-snmp-library={$library} --cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
  142. $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:cm:scan {$params}";
  143. $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:scan {$params}";
  144. $params .= " --save-historic={$saveHistoric}";
  145. $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:stats {$params}";
  146. $commands[] = "*/{$timeScan} * * * * {$pathConsole} cmts:interface:description {$params}";
  147. $params = "--cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
  148. $commands[] = "*/{$timeScan} * * * * {$pathConsole} stats:interface {$params}";
  149. fwrite($handle, PHP_EOL.PHP_EOL."# CMTS {$description} ({$deviceIp})".PHP_EOL);
  150. fwrite($handle, PHP_EOL);
  151. fwrite($handle, implode(PHP_EOL,$commands));
  152. }
  153. }
  154. fclose($handle);
  155. @unlink($fileCrontab);
  156. if(symlink ($file , $fileCrontab)) print_r("Creado link al crontab".PHP_EOL);
  157. print_r(@shell_exec("/etc/init.d/cron reload"));
  158. }
  159. }