ForkCmtsCmStatsCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. # bin/console cmts:cm:stats --cmts-device-id=14 --cmts-server-id=1 --cm-ip=10.42.0.27 --cm-mac=00237406d524 --cm-community=public
  3. # http://200.50.168.115/Provisioning/admin/cablemodem/00237406d524
  4. # bin/console cmts:cm:stats --cmts-device-id=14 --cmts-server-id=1 --cm-ip=10.50.2.3 --cm-mac=001a6663dcde --cm-community=public
  5. namespace CmtsBundle\Command;
  6. use BaseStatsBundle\Command\BaseCmCommand;
  7. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use CmtsBundle\SNMP\SNMP as SNMP;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Input\ArgvInput;
  15. use Symfony\Component\Console\Output\BufferedOutput;
  16. use Symfony\Component\Process\Process;
  17. use Symfony\Component\Process\Exception\ProcessFailedException;
  18. use Graze\ParallelProcess\Pool;
  19. class ForkCmtsCmStatsCommand extends ContainerAwareCommand
  20. {
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('fork:cmts:cm:stats')
  25. ->setDescription('Obtener estadísticas de un CM')
  26. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  27. ->setDefinition(array(
  28. new InputOption('cm', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, "List of CM: --cm=ip1,mac1 --cm=ip2,mac2"),
  29. new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
  30. new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
  31. new InputOption('cm-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS",'public'),
  32. new InputOption('cm-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP",'OIDSBase'),
  33. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  34. ))
  35. ;
  36. }
  37. /**
  38. * @param InputInterface $input
  39. * @param OutputInterface $output
  40. */
  41. protected function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. $this->inicio = microtime(true);
  44. $pool = new Pool();
  45. $exception = function(Process $process, $duration) {throw new ProcessFailedException($process);};
  46. $pool->setOnFailure($exception);
  47. $cablemodems = $input->getOption('cm');
  48. $cmtsDeviceId = (int) $input->getOption('cmts-device-id');
  49. $cmtsServerId = (int) $input->getOption('cmts-server-id');
  50. $cmCommunity = $input->getOption('cm-community');
  51. $cmSnmpLibrary = $input->getOption('cm-snmp-library');
  52. $saveHistoric = (int) $input->getOption('save-historic');
  53. if(is_null($cablemodems) || empty($cablemodems)) exit();
  54. $path = "/opt/stats/bin/console";
  55. $cmd = "cmts:cm:stats";
  56. $args = "--cmts-device-id={$cmtsDeviceId} --cmts-server-id={$cmtsServerId} --cm-community={$cmCommunity} --cm-snmp-library={$cmSnmpLibrary} --save-historic={$saveHistoric}";
  57. $total = 0;
  58. foreach($cablemodems as $dupla) {
  59. list($ip, $mac) = explode(",",$dupla);
  60. if(is_null($ip) || is_null($mac)) continue;
  61. $process = "{$path} {$cmd} {$args} --cm-ip={$ip} --cm-mac={$mac}";
  62. $pool->add(new Process($process));
  63. $total++;
  64. }
  65. try {
  66. $pool->run(); // blocking that will run till it finishes
  67. } catch(\ProcessFailedException $e) {
  68. print_r($e);
  69. print_r(PHP_EOL);
  70. }
  71. $this->fin = microtime(true);
  72. print_r("Fin");
  73. print_r(PHP_EOL);
  74. exit(0);
  75. }
  76. }