GenerateRemoteCrontabCommand.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. class GenerateRemoteCrontabCommand extends ContainerAwareCommand
  8. {
  9. protected function configure()
  10. {
  11. $this
  12. ->setName('stats:crontab:remote')
  13. ->setDescription('Generate Remote Stats Crontab File')
  14. ->setHelp('El comando genera/actualiza el archivo crontab de manera remota para realizar consultas a dispositivos')
  15. ->setDefinition(array(
  16. new InputOption('file-crontab', false, InputOption::VALUE_OPTIONAL, "File Crontab", "/etc/cron.d/fd3_stats"),
  17. new InputOption('path-app', false, InputOption::VALUE_OPTIONAL, "Path App", "/opt/stats"),
  18. new InputOption('url', false, InputOption::VALUE_OPTIONAL, "Crontab webservice url"),
  19. new InputOption('amqp', false, InputOption::VALUE_OPTIONAL, "Execute the commands via amqp", true),
  20. new InputOption('routing_key', false, InputOption::VALUE_OPTIONAL, "AMQP Routing key"),
  21. ))
  22. ;
  23. }
  24. /**
  25. * @param InputInterface $input
  26. * @param OutputInterface $output
  27. */
  28. protected function execute(InputInterface $input, OutputInterface $output)
  29. {
  30. $this->output = $output;
  31. $fileCrontab = $input->getOption('file-crontab');
  32. $pathApp = $input->getOption('path-app');
  33. $url = $input->getOption('url');
  34. if (is_null($url)) {
  35. if ($this->getContainer()->hasParameter('url_crontab')) {
  36. $url = $this->getContainer()->getParameter('url_crontab');
  37. } else {
  38. $output->writeln('<error>ERROR:</error> Debe definir una url para generar el crontab.');
  39. $output->writeln($this->getSynopsis());
  40. return;
  41. }
  42. }
  43. $times = array();
  44. $now = date("d-m-Y H:i:s");
  45. $content = "SHELL=/bin/bash".PHP_EOL."BASH_ENV=/container.env".PHP_EOL;
  46. $content .= PHP_EOL . "# NO EDITAR este archivo, se autogenera con el comando stats:crontab:remote. Generado {$now}." . PHP_EOL;
  47. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  48. if (is_null($pathApp)) {
  49. $pathApp = $this->getContainer()->getParameter('app_path');
  50. }
  51. $pathConsole = "{$pathApp}/bin/console";
  52. $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
  53. $oltMarks = array('FiberHome','FiberLink','Huawei','ZTE','Calix');
  54. $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1','OIDSFiberLinkV1','OIDSZTEV1','OIDSCalixV1');
  55. $amqp = $input->getOption('amqp');
  56. $amqpRemote = '';
  57. $routing_key = '';
  58. if ($amqp) {
  59. $amqpRemote = 'amqp:remote';
  60. // Verifico la routing key si se pasa como parametro o variable de entorno
  61. // default routing_key = stats
  62. $routing_key = $input->getOption('routing_key');
  63. if (!$routing_key) {
  64. $routing_key = getenv('AMQP_KEY') !== false ? getenv('AMQP_KEY') : 'stats';
  65. }
  66. $routing_key = "--route={$routing_key}";
  67. }
  68. $content .= "*/5 * * * * {$pathConsole} stats:crontab:remote >> /var/log/cron.log 2>&1" . PHP_EOL;
  69. foreach ($serverDevices as $server) {
  70. $content .= PHP_EOL . PHP_EOL . "# SERVER {$server->getName()} / {$server->getUrl()}" . PHP_EOL;
  71. // APARTADO OLT
  72. $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT', 'deviceServer' => $server));
  73. $serverId = $server->getId();
  74. $saveHistoric = $server->getSaveHistoric();
  75. ($saveHistoric)? $saveHistoric = 1 : $saveHistoric = 0;
  76. foreach ($oltDevices as $device) {
  77. $commands = array();
  78. $data = $device->jsonExtraData();
  79. $deviceId = $device->getDeviceId();
  80. if ($data['executeSnmp'] == 0){
  81. $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, executeSnmp is 0" . PHP_EOL;
  82. continue;
  83. }
  84. if (!in_array($data['mark'], $oltMarks)){
  85. $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, mark is not one of " . implode(", ", $oltMarks). PHP_EOL;
  86. continue;
  87. }
  88. if (!in_array($data['library'], $oltLibraries)){
  89. $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, library is not one of " . implode(", ", $oltLibraries). PHP_EOL;
  90. continue;
  91. }
  92. $oltName = $data['name'];
  93. $mark = strtolower($data['mark']);
  94. $library = $data['library'];
  95. $snmpCommunity = $data['snmpCommunity'];
  96. $deviceIp = $device->getIp();
  97. $timeScan = $data['timeScan'];
  98. $timeOnuStats = $data['timeOnuStats'];
  99. $timePonStats = $data['timePonStats'];
  100. $timeOltOctets = $data['timeOltOctets'];
  101. $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  102. if ($amqp)
  103. $params = "--args=--olt-ip:{$deviceIp} --args=--olt-community:{$snmpCommunity} --args=--olt-snmp-library:{$library} --args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}";
  104. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:scan {$params} >> /var/log/cron.log 2>&1";
  105. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:scan {$params} >> /var/log/cron.log 2>&1";
  106. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:olt:scan {$params} >> /var/log/cron.log 2>&1";
  107. if ($amqp) {
  108. $params .= " --args=--save-historic:{$saveHistoric}";
  109. } else {
  110. $params .= " --save-historic={$saveHistoric}";
  111. }
  112. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:stats {$params} >> /var/log/cron.log 2>&1";
  113. $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:stats {$params} >> /var/log/cron.log 2>&1";
  114. if($mark == "huawei")
  115. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:octets {$params} >> /var/log/cron.log 2>&1";
  116. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:octets {$params} >> /var/log/cron.log 2>&1";
  117. $params = "--olt-device-id={$deviceId} --olt-server-id={$serverId}";
  118. if ($amqp) $params = "--args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}";
  119. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu {$params} >> /var/log/cron.log 2>&1";
  120. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport {$params} >> /var/log/cron.log 2>&1";
  121. $content .= PHP_EOL . "# OLT {$oltName} ({$deviceIp})" . PHP_EOL;
  122. $content .= implode(PHP_EOL, $commands);
  123. $content .= PHP_EOL . PHP_EOL;
  124. }
  125. if ($this->getContainer()->getParameter('geoserver_service')) {
  126. $content .= PHP_EOL . PHP_EOL . "# MAPAS" . PHP_EOL;
  127. $params = "--olt-server-id={$serverId}";
  128. if ($amqp) $params = "--args=--olt-server-id:{$serverId}";
  129. $commands = array();
  130. $commands[] = "*/5 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu:geo {$params} >> /var/log/cron.log 2>&1";
  131. $commands[] = "*/10 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport:geo {$params} >> /var/log/cron.log 2>&1";
  132. $content .= implode(PHP_EOL, $commands);
  133. $content .= PHP_EOL . PHP_EOL;
  134. }
  135. // APARTADO NAS
  136. $nasDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'RadiusBundle\Entity\NAS', 'deviceServer' => $server));
  137. foreach ($nasDevices as $device) {
  138. $commands = array();
  139. $data = $device->jsonExtraData();
  140. $deviceId = $device->getDeviceId();
  141. if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
  142. $content .= PHP_EOL . PHP_EOL . "# Device(NAS) {$deviceId} skiped, snmpCommunity is null or empty" . PHP_EOL;
  143. continue;
  144. }
  145. if (!in_array($data['library'], array('OIDSBase'))){
  146. $content .= PHP_EOL . PHP_EOL . "# Device(NAS) {$deviceId} skiped, library not exist" . PHP_EOL;
  147. continue;
  148. }
  149. if (!$saveHistoric){
  150. $content .= PHP_EOL . PHP_EOL . "# Device {$deviceId} skiped, because saveHistoric is 0, set Server options in Stats". PHP_EOL;
  151. continue;
  152. }
  153. $library = $data['library'];
  154. $snmpCommunity = $data['snmpCommunity'];
  155. $description = $data['description'];
  156. $deviceIp = $device->getIp();
  157. $params = "--nas-ip={$deviceIp} --nas-community={$snmpCommunity} --nas-snmp-library={$library} --nas-device-id={$deviceId} --nas-server-id={$serverId} --save-historic=1";
  158. if ($amqp)
  159. $params = "--args=--nas-ip:{$deviceIp} --args=--nas-community:{$snmpCommunity} --args=--nas-snmp-library:{$library} --args=--nas-device-id:{$deviceId} --args=--nas-server-id:{$serverId} --args=--save-historic:1";
  160. $commands[] = "*/10 * * * * {$pathConsole} {$amqpRemote} {$routing_key} nas:onu:octets {$params} >> /var/log/cron.log 2>&1";
  161. $content .= PHP_EOL . "# NAS {$description} ({$deviceIp})" . PHP_EOL;
  162. $content .= implode(PHP_EOL, $commands);
  163. $content .= PHP_EOL . PHP_EOL;
  164. }
  165. // APARTADO CMTS
  166. $cmtsDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'CablemodemBundle\Entity\CMTS', 'deviceServer' => $server));
  167. foreach ($cmtsDevices as $device) {
  168. $commands = array();
  169. $data = $device->jsonExtraData();
  170. $deviceId = $device->getDeviceId();
  171. if (is_null($data['snmpCommunity']) || empty($data['snmpCommunity'])){
  172. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, snmpCommunity is null or empty" . PHP_EOL;
  173. continue;
  174. }
  175. if (!in_array($data['library'], array('OIDSBase'))){
  176. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, library not exist" . PHP_EOL;
  177. continue;
  178. }
  179. if (!$data['executeSnmp']){
  180. $content .= PHP_EOL . PHP_EOL . "# Device(CMTS) {$deviceId} skiped, because executeSnmp is 0". PHP_EOL;
  181. continue;
  182. }
  183. $library = $data['library'];
  184. $snmpCommunity = $data['snmpCommunity'];
  185. $docsVersion = $data['docsVersion'];
  186. $description = $data['name'];
  187. $deviceIp = $device->getIp();
  188. $timeScan = $data['timeScan'];
  189. $timeOctets = $data['timeCmtsOctets'];
  190. (isset($data['timeCmStats']))? $timeCmStats = $data['timeCmStats'] : $timeCmStats = 10;
  191. $params = "--cmts-ip={$deviceIp} --cmts-community={$snmpCommunity} --cmts-snmp-library={$library} --cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
  192. if ($amqp)
  193. $params = "--args=--cmts-ip:{$deviceIp} --args=--cmts-community:{$snmpCommunity} --args=--cmts-snmp-library:{$library} --args=--cmts-device-id:{$deviceId} --args=--cmts-server-id:{$serverId}";
  194. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} cmts:cm:scan {$params} >> /var/log/cron.log 2>&1";
  195. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} cmts:interface:scan {$params} >> /var/log/cron.log 2>&1";
  196. if ($amqp) {
  197. $params .= " --args=--save-historic:{$saveHistoric}";
  198. } else {
  199. $params .= " --save-historic={$saveHistoric}";
  200. }
  201. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} cmts:interface:stats {$params} >> /var/log/cron.log 2>&1";
  202. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} cmts:interface:description {$params} >> /var/log/cron.log 2>&1";
  203. if ($amqp) {
  204. $params .= " --args=--cmts-docs:{$docsVersion}";
  205. } else {
  206. $params .= " --cmts-docs={$docsVersion}";
  207. }
  208. $commands[] = "*/{$timeOctets} * * * * {$pathConsole} {$amqpRemote} {$routing_key} cmts:octets {$params} >> /var/log/cron.log 2>&1";
  209. if ($amqp) {
  210. $params = "--args=--cmts-device-id:{$deviceId} --args=--cmts-server-id:{$serverId}";
  211. } else {
  212. $params = "--cmts-device-id={$deviceId} --cmts-server-id={$serverId}";
  213. }
  214. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:interface {$params} >> /var/log/cron.log 2>&1";
  215. if ($amqp) {
  216. $params .= " --args=--generate-remote-commands:1";
  217. } else {
  218. $params .= " --generate-remote-commands=1";
  219. }
  220. $commands[] = "*/{$timeCmStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:cm {$params} >> /var/log/cron.log 2>&1";
  221. $content .= PHP_EOL . "# CMTS {$description} ({$deviceIp})" . PHP_EOL;
  222. $content .= implode(PHP_EOL, $commands);
  223. $content .= PHP_EOL . PHP_EOL;
  224. }
  225. }
  226. $content .= PHP_EOL."#END ".PHP_EOL.PHP_EOL;
  227. file_put_contents($fileCrontab, $content);
  228. sleep(5);
  229. print_r(shell_exec("/usr/bin/crontab /etc/cron.d/fd3_stats"));
  230. print_r(shell_exec("/usr/local/bin/supervisorctl -uiksop -pqueRini6 restart cron"));
  231. }
  232. }