CmtsCmStatsCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $this->cmIp = $ip;
  56. $this->cmMac = $mac;
  57. $metrics = $this->metrics($key_cm_stats, $ip, $mac);
  58. $count++;
  59. if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
  60. }
  61. } else {
  62. $metrics = $this->metrics($key_cm_stats, $this->cmIp, $this->cmMac);
  63. $count++;
  64. if($metrics && $saveHistoric) $this->sendData($metrics, $statsdService);
  65. }
  66. /* End Of Blockout */
  67. $this->removeLock($this->flag);
  68. $fin = microtime(true);
  69. $time = $fin - $inicio;
  70. $this->output->writeln("Cantidad CM: {$count} / Tiempo: {$time} segundos / Date: ".date("Y-m-d H:i:s"));
  71. exit();
  72. }
  73. private function sendData($data, $service)
  74. {
  75. $t_start_script = microtime(true);
  76. $service->send($data);
  77. $t_end_script = microtime(true);
  78. $time = $t_end_script - $t_start_script;
  79. $this->output->writeln("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($data));
  80. }
  81. private function sla($ip)
  82. {
  83. $kernel = $this->getContainer()->get('kernel');
  84. $application = new Application($kernel);
  85. $application->setAutoExit(false);
  86. $args = ['','ping',$ip];
  87. $input = new ArgvInput($args);
  88. $output = new BufferedOutput();
  89. $application->run($input, $output);
  90. return $output->fetch();
  91. }
  92. private function metrics($key, $ip, $mac)
  93. {
  94. $inicio = microtime(true);
  95. $SNMP = new SNMP($ip, $this->cmCommunity);
  96. $SNMP->setTimeout(2500000); // 2.5 seconds of timeout
  97. $SNMP->setRetry(1); // 1 query per metric
  98. $library = "use".$this->cmSnmpLibrary;
  99. $this->apiSNMP = $SNMP->$library();
  100. $uptime = $this->getSNMP("sysUpTimeInstance","cmUptime");
  101. if(!$uptime) $SNMP->setTimeout(1000000); // 1 seconds of timeout
  102. $txPower = $this->getSNMP("docsIfCmStatusTxPower","cmTxPower");
  103. if($txPower) $SNMP->setTimeout(2500000);
  104. $rxPower = $this->getSNMP("docsIfDownChannelPower","cmRxPower");
  105. if(!$txPower && !$rxPower) {
  106. $this->removeLock($this->flag);
  107. $fin = microtime(true);
  108. $time = $fin - $inicio;
  109. $this->output->writeln("CM no responde uptime, tx y rx => cortamos! Tiempo: $time segundos");
  110. return false;
  111. }
  112. $signal = $this->getSNMP("docsIfSigQSignalNoise","cmSignal");
  113. $microreflection = $this->getSNMP("docsIfSigQMicroreflections","cmMicroreflection");
  114. $unerroreds = $this->getSNMP("docsIfSigQUnerroreds","cmUnerroreds");
  115. $correcteds = $this->getSNMP("docsIfSigQCorrecteds","cmCorrecteds");
  116. $uncorrectables = $this->getSNMP("docsIfSigQUncorrectables","cmUncorrectables");
  117. $slaJson = $this->sla($ip);
  118. try{
  119. $sla = json_decode($slaJson,true);
  120. } catch(\Exception $e) {
  121. $sla = array();
  122. }
  123. $metrics = array('txPower' => 'cm_tx_', 'rxPower' => 'cm_rx_', 'signal' => 'cm_signal_', 'microreflection' => 'cm_microreflection_', 'unerroreds' => 'cm_unerroreds_', 'correcteds' => 'cm_correcteds_', 'uncorrectables' => 'cm_uncorrectables_');
  124. $sendData = $data = array();
  125. foreach($metrics as $var => $m) {
  126. $data[$var] = array();
  127. if(!is_array($$var)) continue;
  128. $multiplicator = 1;
  129. if(in_array($var, array('txPower','rxPower','signal'))) {
  130. $multiplicator = 0.1;
  131. }
  132. foreach($$var as $channel => $value) {
  133. $v = $value * $multiplicator;
  134. $_m = "{$m}{$mac}.{$channel}";
  135. $data[$var][$channel] = $v;
  136. $sendData[$_m] = "{$v}|g";
  137. }
  138. }
  139. if($uptime) {
  140. foreach($uptime as $oid => $value) {
  141. $data['uptime'] = $this->convertSysUpTimeInstance($value);
  142. }
  143. }
  144. if(isset($sla['latency'])) {
  145. $data['latency'] = $sla['latency'];
  146. $sendData["cm_latency_{$mac}"] = "{$sla['latency']}|g";
  147. }
  148. if(isset($sla['jitter'])) {
  149. $data['jitter'] = $sla['jitter'];
  150. $sendData["cm_jitter_{$mac}"] = "{$sla['jitter']}|g";
  151. }
  152. if(isset($sla['loss'])) {
  153. $data['loss'] = $sla['loss'];
  154. $sendData["cm_loss_{$mac}"] = "{$sla['loss']}|g";
  155. }
  156. $this->hset($key, $mac, $data, true);
  157. return $sendData;
  158. }
  159. }