CmtsCmStatsCommand.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, "List of CM: --cm=ip1,mac1 --cm=ip2,mac2"),
  29. new InputOption('cm-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS",'public'),
  30. new InputOption('cm-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP",'OIDSBase'),
  31. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  32. ))
  33. ;
  34. }
  35. /**
  36. * @param InputInterface $input
  37. * @param OutputInterface $output
  38. */
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. parent::execute($input, $output);
  42. $statsdService = $this->getContainer()->get('statsd');
  43. $key_cm_stats = "cm_stats_{$this->d_s}"; // group by cmts
  44. $saveHistoric = (int) $input->getOption('save-historic');
  45. $inicio = microtime(true);
  46. $count = 0;
  47. if(is_null($this->cmIp)) {
  48. $cablemodems = $input->getOption('cm');
  49. if(is_null($cablemodems) || empty($cablemodems)) {
  50. $this->removeLock($this->flag);
  51. exit();
  52. }
  53. foreach($cablemodems as $dupla) {
  54. list($ip, $mac) = explode(",",$dupla);
  55. $metrics = $this->metrics($key_cm_stats, $ip, $mac);
  56. $count++;
  57. if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
  58. }
  59. } else {
  60. $metrics = $this->metrics($key_cm_stats, $this->cmIp, $this->cmMac);
  61. $count++;
  62. if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
  63. }
  64. /* End Of Blockout */
  65. $this->removeLock($this->flag);
  66. $fin = microtime(true);
  67. $time = $fin - $inicio;
  68. $this->output->writeln("Cantidad CM: {$count} / Tiempo: {$time} segundos / Date: ".date("Y-m-d H:i:s"));
  69. exit();
  70. }
  71. private function sendData($data, $service)
  72. {
  73. $t_start_script = microtime(true);
  74. $service->send($data);
  75. $t_end_script = microtime(true);
  76. $time = $t_end_script - $t_start_script;
  77. $this->output->writeln("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($data));
  78. }
  79. private function sla($ip)
  80. {
  81. $kernel = $this->getContainer()->get('kernel');
  82. $application = new Application($kernel);
  83. $application->setAutoExit(false);
  84. $args = ['','ping',$ip];
  85. $input = new ArgvInput($args);
  86. $output = new BufferedOutput();
  87. $application->run($input, $output);
  88. return $output->fetch();
  89. }
  90. private function metrics($key, $ip, $mac)
  91. {
  92. $inicio = microtime(true);
  93. $SNMP = new SNMP($ip, $this->cmCommunity);
  94. $SNMP->setTimeout(2500000); // 2.5 seconds of timeout
  95. $SNMP->setRetry(1); // 1 query per metric
  96. $library = "use".$this->cmSnmpLibrary;
  97. $this->apiSNMP = $SNMP->$library();
  98. $uptime = $this->getSNMP("sysUpTimeInstance","cmUptime");
  99. if(!$uptime) $SNMP->setTimeout(1000000); // 1 seconds of timeout
  100. $txPower = $this->getSNMP("docsIfCmStatusTxPower","cmTxPower");
  101. if($txPower) $SNMP->setTimeout(2500000);
  102. $rxPower = $this->getSNMP("docsIfDownChannelPower","cmRxPower");
  103. if(!$txPower && !$rxPower) {
  104. $this->removeLock($this->flag);
  105. $fin = microtime(true);
  106. $time = $fin - $inicio;
  107. $this->output->writeln("CM no responde uptime, tx y rx => cortamos! Tiempo: $time segundos");
  108. return false;
  109. }
  110. $signal = $this->getSNMP("docsIfSigQSignalNoise","cmSignal");
  111. $microreflection = $this->getSNMP("docsIfSigQMicroreflections","cmMicroreflection");
  112. $unerroreds = $this->getSNMP("docsIfSigQUnerroreds","cmUnerroreds");
  113. $correcteds = $this->getSNMP("docsIfSigQCorrecteds","cmCorrecteds");
  114. $uncorrectables = $this->getSNMP("docsIfSigQUncorrectables","cmUncorrectables");
  115. $slaJson = $this->sla($ip);
  116. try{
  117. $sla = json_decode($slaJson,true);
  118. } catch(\Exception $e) {
  119. $sla = array();
  120. }
  121. $metrics = array('txPower' => 'cm_tx_', 'rxPower' => 'cm_rx_', 'signal' => 'cm_signal_', 'microreflection' => 'cm_microreflection_', 'unerroreds' => 'cm_unerroreds_', 'correcteds' => 'cm_correcteds_', 'uncorrectables' => 'cm_uncorrectables_');
  122. $sendData = $data = array();
  123. foreach($metrics as $var => $m) {
  124. $data[$var] = array();
  125. if(!is_array($$var)) continue;
  126. $multiplicator = 1;
  127. if(in_array($var, array('txPower','rxPower','signal'))) {
  128. $multiplicator = 0.1;
  129. }
  130. foreach($$var as $channel => $value) {
  131. $v = $value * $multiplicator;
  132. $_m = "{$m}{$mac}.{$channel}";
  133. $data[$var][$channel] = $v;
  134. $sendData[$_m] = "{$v}|g";
  135. }
  136. }
  137. if($uptime) {
  138. foreach($uptime as $oid => $value) {
  139. $data['uptime'] = $this->convertSysUpTimeInstance($value);
  140. }
  141. }
  142. if(isset($sla['latency'])) {
  143. $data['latency'] = $sla['latency'];
  144. $sendData["cm_latency_{$mac}"] = "{$sla['latency']}|g";
  145. }
  146. if(isset($sla['jitter'])) {
  147. $data['jitter'] = $sla['jitter'];
  148. $sendData["cm_jitter_{$mac}"] = "{$sla['jitter']}|g";
  149. }
  150. if(isset($sla['loss'])) {
  151. $data['loss'] = $sla['loss'];
  152. $sendData["cm_loss_{$mac}"] = "{$sla['loss']}|g";
  153. }
  154. $this->hset($key, $mac, $data, true);
  155. return $sendData;
  156. }
  157. }