CmtsCmStatsCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace CmtsBundle\Command;
  5. use BaseStatsBundle\Command\BaseCmCommand;
  6. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use CmtsBundle\SNMP\SNMP as SNMP;
  11. class CmtsCmStatsCommand extends BaseCmCommand
  12. {
  13. protected function configure()
  14. {
  15. $this
  16. ->setName('cmts:cm:stats')
  17. ->setDescription('Obtener estadísticas de un CM')
  18. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  19. ->setDefinition(array(
  20. new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
  21. new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
  22. new InputOption('cm-ip', false, InputOption::VALUE_OPTIONAL, "IP del CM"),
  23. new InputOption('cm-mac', false, InputOption::VALUE_OPTIONAL, "MAC del CM"),
  24. new InputOption('cm-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS",'public'),
  25. new InputOption('cm-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP",'OIDSBase'),
  26. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  27. ))
  28. ;
  29. }
  30. /**
  31. * @param InputInterface $input
  32. * @param OutputInterface $output
  33. */
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. parent::execute($input, $output);
  37. $key_cm_stats = "cm_stats_{$this->d_s}"; // group by cmts
  38. $saveHistoric = (int) $input->getOption('save-historic');
  39. $inicio = microtime(true);
  40. $SNMP = new SNMP($this->cmIp, $this->cmCommunity);
  41. $SNMP->setTimeout(5000000); // 5 seconds of timeout
  42. $SNMP->setRetry(1); // 1 query per metric
  43. $library = "use".$this->cmSnmpLibrary;
  44. $this->apiSNMP = $SNMP->$library();
  45. $txPower = $this->getSNMP("docsIfCmStatusTxPower","cmTxPower");
  46. $rxPower = $this->getSNMP("docsIfDownChannelPower","cmRxPower");
  47. $signal = $this->getSNMP("docsIfSigQSignalNoise","cmSignal");
  48. $microreflection = $this->getSNMP("docsIfSigQMicroreflections","cmMicroreflection");
  49. $unerroreds = $this->getSNMP("docsIfSigQUnerroreds","cmUnerroreds");
  50. $correcteds = $this->getSNMP("docsIfSigQCorrecteds","cmCorrecteds");
  51. $uncorrectables = $this->getSNMP("docsIfSigQUncorrectables","cmUncorrectables");
  52. $metrics = array('txPower' => 'cm_tx_', 'rxPower' => 'cm_rx_', 'signal' => 'cm_signal_', 'microreflection' => 'cm_microreflection_', 'unerroreds' => 'cm_unerroreds_', 'correcteds' => 'cm_correcteds_', 'uncorrectables' => 'cm_uncorrectables_');
  53. $sendData = $data = array();
  54. foreach($metrics as $var => $m) {
  55. $data[$var] = array();
  56. if(!is_array($$var)) continue;
  57. $multiplicator = 1;
  58. if(in_array($var, array('txPower','rxPower','signal'))) {
  59. $multiplicator = 0.1;
  60. }
  61. foreach($$var as $channel => $value) {
  62. $v = $value * $multiplicator;
  63. $_m = "{$m}{$this->cmMac}.{$channel}";
  64. $data[$var][$channel] = $v;
  65. $sendData[$_m] = "{$v}|g";
  66. }
  67. }
  68. $this->hset($key_cm_stats, $this->cmMac, $data, true);
  69. //$data = $this->collector->hgetall($key_cm_stats);
  70. if($sendData && $saveHistoric) {
  71. $t_start_script = microtime(true);
  72. $statsdService = $this->getContainer()->get('statsd');
  73. $statsdService->send($sendData);
  74. $t_end_script = microtime(true);
  75. $time = $t_end_script - $t_start_script;
  76. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  77. }
  78. /* End Of Blockout */
  79. $this->removeLock($this->flag);
  80. $fin = microtime(true);
  81. $time = $fin - $inicio;
  82. $this->output->writeln("Tiempo: $time segundos");
  83. }
  84. }