CmtsCmStatsCommand.php 6.4 KB

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