CmtsCmStatsCommand.php 5.2 KB

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