CmtsInterfaceStatsCommand.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace CmtsBundle\Command;
  3. use BaseStatsBundle\Command\BaseCmtsCommand;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use CmtsBundle\SNMP\SNMP as SNMP;
  9. use Symfony\Component\Yaml\Parser;
  10. class CmtsInterfaceStatsCommand extends BaseCmtsCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('cmts:interface:stats')
  16. ->setDescription('Obtener Stats de Interfaces')
  17. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  18. ->setDefinition(array(
  19. new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
  20. new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
  21. new InputOption('cmts-ip', false, InputOption::VALUE_OPTIONAL, "IP del CMTS"),
  22. new InputOption('cmts-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS"),
  23. new InputOption('cmts-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
  24. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  25. ))
  26. ;
  27. }
  28. /**
  29. * @param InputInterface $input
  30. * @param OutputInterface $output
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. parent::execute($input, $output);
  35. $key_cmts_scan = "cmts_scan_ifs_{$this->d_s}";
  36. $key_cm_scan = "cmts_scan_{$this->d_s}";
  37. $saveHistoric = (int) $input->getOption('save-historic');
  38. $inicio = microtime(true);
  39. $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
  40. $library = "use".$this->cmtsSnmpLibrary;
  41. $this->apiSNMP = $SNMP->$library();
  42. $dataCached = $this->getData($key_cmts_scan, true);
  43. if(empty($dataCached)) {
  44. $this->output->writeln("Se requiere {$key_cmts_scan}.");
  45. $this->removeLock($this->flag);
  46. return true;
  47. }
  48. $_utilization = $this->getSNMP("docsIfCmtsChannelUtilization","cmtsIfUtilization");
  49. $ifStatsCached = $sendData = $utilization = array();
  50. foreach($_utilization as $compIndex => $value) {
  51. $params = explode(".",$compIndex);
  52. if($params > 1) {
  53. $index = $params[0];
  54. $aux = implode(".", array_slice($params,1));
  55. } else {
  56. $index = $compIndex;
  57. $aux = 0;
  58. }
  59. $utilization[$index] = array($aux => $value);
  60. }
  61. $microreflection = $this->getSNMP("docsIfSigQMicroreflections","cmtsIfMicroreflection");
  62. $signal = $this->getSNMP("docsIfSigQSignalNoise","cmtsIfSignal");
  63. $cmCached = $this->getData($key_cm_scan, true);
  64. $states = array();
  65. if(empty($cmCached)) {
  66. $this->output->writeln("Se requiere {$key_cm_scan} para obtener cantidad de CM por Interface.");
  67. } else {
  68. foreach($cmCached as $index => $value) {
  69. $up = $value['upInterface'];
  70. $down = $value['downInterface'];
  71. $state = $value['status'];
  72. if(!isset($states[$up])) $states[$up] = array(0 => 0, 1 => 0);
  73. if(!isset($states[$down])) $states[$down] = array(0 => 0, 1 => 0);
  74. $states[$up][$state]++;
  75. $states[$down][$state]++;
  76. $mac = strtolower($value['mac']);
  77. $sendData["cm_status_{$mac}"] = "{$state}|g";
  78. }
  79. }
  80. $subId = $this->d_s;
  81. $metrics = array("utilization" => "{$subId}_if_utilization_", "microreflection" => "{$subId}_if_microreflection_", "signal" => "{$subId}_if_signal_", "states" => "{$subId}_if_state_");
  82. $countIfs = 0;
  83. foreach($dataCached as $index => $interface) {
  84. $countIfs++;
  85. $stats = array();
  86. foreach($metrics as $data => $metric) {
  87. if(!isset($$data[$index])) continue;
  88. $m = "{$metric}{$index}";
  89. if($data == "signal") {
  90. $v = $$data[$index] * 0.1;
  91. $sendData[$m] = "{$v}|g";
  92. } elseif($data == "utilization") {
  93. foreach($$data[$index] as $aux => $v) {
  94. $sendData["{$m}.{$aux}"] = "{$v}|g";
  95. }
  96. $v = $$data[$index];
  97. } elseif($data == "states") {
  98. $v = $$data[$index];
  99. $on = "{$subId}_if_state_on_{$index}";
  100. $off = "{$subId}_if_state_off_{$index}";
  101. $sendData[$on] = "{$v[1]}|g";
  102. $sendData[$off] = "{$v[0]}|g";
  103. } else {
  104. $v = $$data[$index];
  105. }
  106. $stats[$data] = $v;
  107. }
  108. $ifStatsCached[$index] = $stats;
  109. }
  110. $this->octets($ifStatsCached, $dataCached, $saveHistoric);
  111. if($ifStatsCached) {
  112. $key_ifs_stats = "cmts_stats_ifs_{$this->d_s}";
  113. $this->setData($key_ifs_stats, $ifStatsCached, true);
  114. }
  115. if($sendData && $saveHistoric) {
  116. $t_start_script = microtime(true);
  117. $statsdService = $this->getContainer()->get('statsd');
  118. $statsdService->send($sendData);
  119. $t_end_script = microtime(true);
  120. $time = $t_end_script - $t_start_script;
  121. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  122. }
  123. /* Fin de bloqueo */
  124. $this->removeLock($this->flag);
  125. $fin = microtime(true);
  126. $time = $fin - $inicio;
  127. $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
  128. }
  129. private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
  130. $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
  131. $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
  132. $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
  133. $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
  134. if(empty($bandwidthCached)) $bandwidthCached = array();
  135. $t1 = time();
  136. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  137. $sendData = array(); $subId = $this->d_s;
  138. foreach($dataCached as $index => $data) {
  139. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  140. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  141. if(isset($ifStatsCached[$index])) {
  142. $ifStatsCached[$index]['inOctets'] = 0;
  143. $ifStatsCached[$index]['outOctets'] = 0;
  144. }
  145. if(isset($bandwidthCached[$index])) {
  146. $t0 = $bandwidthCached[$index]['t'];
  147. $in0 = $bandwidthCached[$index]['inOct'];
  148. $out0 = $bandwidthCached[$index]['outOct'];
  149. $inAcc = $bandwidthCached[$index]['inAcc'];
  150. $outAcc = $bandwidthCached[$index]['outAcc'];
  151. $inBandwidth = $outBandwidth = 0;
  152. if(($in1 >= $in0) && ($t1 > $t0)) {
  153. $diff = $in1 - $in0;
  154. $inAcc += $diff;
  155. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  156. }
  157. if(($out1 >= $out0) && ($t1 > $t0)) {
  158. $diff = $out1 - $out0;
  159. $outAcc += $diff;
  160. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  161. }
  162. $totalIn += $inBandwidth;
  163. $totalOut += $outBandwidth;
  164. $sendData["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
  165. $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$outBandwidth}|g";
  166. if(date("d",$t0) != date("d",$t1)) {
  167. $inAcc = $outAcc = 0;
  168. }
  169. $totalConsOut += $outAcc;
  170. $totalConsIn += $inAcc;
  171. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  172. if(isset($ifStatsCached[$index])) {
  173. $ifStatsCached[$index]['inOctets'] = $inBandwidth;
  174. $ifStatsCached[$index]['outOctets'] = $outBandwidth;
  175. }
  176. } else {
  177. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  178. }
  179. }
  180. $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
  181. $cmtsBandwidth = array();
  182. $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
  183. $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
  184. $div = 1073741824; //bytes => giga
  185. $consIn = number_format(($totalConsIn / $div),3);
  186. $consOut = number_format(($totalConsOut / $div),3);
  187. $cmtsConsumption = array();
  188. $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
  189. $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
  190. $statsdService = $this->getContainer()->get('statsd');
  191. $statsdService->send($cmtsBandwidth);
  192. $statsdService->send($cmtsConsumption);
  193. if($sendData && $saveHistoric) {
  194. $t_start_script = microtime(true);
  195. $statsdService->send($sendData);
  196. $t_end_script = microtime(true);
  197. $time = $t_end_script - $t_start_script;
  198. print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  199. }
  200. }
  201. }