GenerateRemoteCrontabCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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"),
  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 = PHP_EOL."# NO EDITAR este archivo, se autogenera con el comando stats:crontab:remote. Generado {$now}.".PHP_EOL;
  46. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  47. if(is_null($pathApp)) {
  48. $pathApp = $this->getContainer()->getParameter('app_path');
  49. }
  50. $pathConsole = "root $(wich php) {$pathApp}/bin/console";
  51. $serverDevices = $doctrine->getRepository('\StatsBundle\Entity\DeviceServer')->findAll();
  52. $oltMarks = array('FiberHome','FiberLink','Huawei');
  53. $oltLibraries = array('OIDSFiberHomeV1','OIDSHuaweiV1');
  54. $amqp = $input->getOption('amqp');
  55. $amqpRemote = '';
  56. $routing_key = '';
  57. if ($amqp) {
  58. $amqpRemote = 'amqp:remote';
  59. // Verifico la routing key si se pasa como parametro o variable de entorno
  60. // default routing_key = stats
  61. $routing_key = $input->getOption('routing_key');
  62. if (!$routing_key) {
  63. $routing_key = getenv('AMQP_KEY') !== false ? getenv('AMQP_KEY') : 'stats';
  64. }
  65. $routing_key = "--route={$routing_key}";
  66. }
  67. $content .= "*/5 * * * * {$pathConsole} {$amqpRemote} {$routing_key} generate:crontab".PHP_EOL;
  68. foreach($serverDevices as $server) {
  69. $content .= PHP_EOL.PHP_EOL."# SERVER {$server->getName()} / {$server->getUrl()}".PHP_EOL;
  70. // APARTADO OLT
  71. $oltDevices = $doctrine->getRepository('\StatsBundle\Entity\Device')->findBy(array('deviceType' => 'FTTHBundle\Entity\OLT','deviceServer' => $server));
  72. $serverId = $server->getId();
  73. foreach($oltDevices as $device) {
  74. $commands = array();
  75. $data = $device->jsonExtraData();
  76. if($data['executeSnmp'] == 0) continue;
  77. if(!in_array($data['mark'],$oltMarks)) continue;
  78. if(!in_array($data['library'],$oltLibraries)) continue;
  79. $oltName = $data['name'];
  80. $mark = strtolower($data['mark']);
  81. $library = $data['library'];
  82. $snmpCommunity = $data['snmpCommunity'];
  83. $deviceIp = $device->getIp();
  84. $deviceId = $device->getDeviceId();
  85. $timeScan = $data['timeScan'];
  86. $timeOnuStats = $data['timeOnuStats'];
  87. $timePonStats = $data['timePonStats'];
  88. $timeOltOctets = $data['timeOltOctets'];
  89. $params = "--olt-ip={$deviceIp} --olt-community={$snmpCommunity} --olt-snmp-library={$library} --olt-device-id={$deviceId} --olt-server-id={$serverId}";
  90. if ($amqp) {
  91. $params = "--args=--olt-ip:{$deviceIp} --args=--olt-community:{$snmpCommunity} --args=--olt-snmp-library:{$library} --args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}";
  92. }
  93. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:scan {$params}";
  94. $commands[] = "*/{$timeScan} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:scan {$params}";
  95. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:onu:stats {$params}";
  96. $commands[] = "*/{$timePonStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:stats {$params}";
  97. $commands[] = "*/{$timeOltOctets} * * * * {$pathConsole} {$amqpRemote} {$routing_key} {$mark}:pon:octets {$params}";
  98. $params = '--olt-device-id={$deviceId} --olt-server-id={$serverId}';
  99. if ($amqp) {
  100. $params = '--args=--olt-device-id:{$deviceId} --args=--olt-server-id:{$serverId}';
  101. }
  102. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu {$params}";
  103. $commands[] = "*/{$timeOnuStats} * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport {$params}";
  104. $content .= PHP_EOL."# OLT {$oltName} ({$deviceIp})".PHP_EOL;
  105. $content .= implode(PHP_EOL, $commands);
  106. }
  107. if($this->getContainer()->getParameter('geoserver_service')) {
  108. $content .= PHP_EOL.PHP_EOL."# MAPAS".PHP_EOL;
  109. $params = '--olt-server-id={$serverId}';
  110. if ($amqp) {
  111. $params = '--args=--olt-server-id:{$serverId}';
  112. }
  113. $commands = array();
  114. $commands[] = "*/5 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:onu:geo {$params}";
  115. $commands[] = "*/10 * * * * {$pathConsole} {$amqpRemote} {$routing_key} stats:ponport:geo {$params}";
  116. $content .= implode(PHP_EOL, $commands);
  117. $content .= PHP_EOL;
  118. }
  119. }
  120. $this->soap($url, $fileCrontab, $content);
  121. }
  122. /**
  123. * @param string $url
  124. * @param string $filename
  125. * @param string $content
  126. */
  127. protected function soap($url, $filename, $content)
  128. {
  129. $client = new \SoapClient($url);
  130. $result = $client->__soapCall('crontab', compact('filename', 'content'));
  131. $this->output->writeln($result);
  132. }
  133. }