CmtsInterfaceStatsCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. $cmtsStatus = array(0 => 0, 1 => 0);
  66. if(empty($cmCached)) {
  67. $this->output->writeln("Se requiere {$key_cm_scan} para obtener cantidad de CM por Interface.");
  68. } else {
  69. foreach($cmCached as $index => $value) {
  70. $up = $value['upInterface'];
  71. $down = $value['downInterface'];
  72. $state = $value['status'];
  73. if(!isset($states[$up])) $states[$up] = array(0 => 0, 1 => 0);
  74. if(!isset($states[$down])) $states[$down] = array(0 => 0, 1 => 0);
  75. $states[$up][$state]++;
  76. $states[$down][$state]++;
  77. $cmtsStatus[$state]++;
  78. $mac = strtolower($value['mac']);
  79. $sendData["cm_status_{$mac}"] = "{$state}|g";
  80. }
  81. }
  82. $subId = $this->d_s;
  83. $metrics = array("utilization" => "{$subId}_if_utilization_", "microreflection" => "{$subId}_if_microreflection_", "signal" => "{$subId}_if_signal_", "states" => "{$subId}_if_state_");
  84. $countIfs = 0;
  85. foreach($dataCached as $index => $interface) {
  86. $countIfs++;
  87. $stats = array();
  88. foreach($metrics as $data => $metric) {
  89. if(!isset($$data[$index])) continue;
  90. $m = "{$metric}{$index}";
  91. if($data == "signal") {
  92. $v = $$data[$index] * 0.1;
  93. $sendData[$m] = "{$v}|g";
  94. } elseif($data == "utilization") {
  95. foreach($$data[$index] as $aux => $v) {
  96. $sendData["{$m}.{$aux}"] = "{$v}|g";
  97. }
  98. $v = $$data[$index];
  99. } elseif($data == "states") {
  100. $v = $$data[$index];
  101. $on = "{$subId}_if_state_on_{$index}";
  102. $off = "{$subId}_if_state_off_{$index}";
  103. $sendData[$on] = "{$v[1]}|g";
  104. $sendData[$off] = "{$v[0]}|g";
  105. } else {
  106. $v = $$data[$index];
  107. }
  108. $stats[$data] = $v;
  109. }
  110. $ifStatsCached[$index] = $stats;
  111. }
  112. $this->octets($ifStatsCached, $dataCached, $saveHistoric);
  113. if($ifStatsCached) {
  114. $key_ifs_stats = "cmts_stats_ifs_{$this->d_s}";
  115. $this->setData($key_ifs_stats, $ifStatsCached, true);
  116. }
  117. $sendData["{$subId}_state_on"] = "{$cmtsStatus[1]}|g";
  118. $sendData["{$subId}_state_off"] = "{$cmtsStatus[0]}|g";
  119. if($sendData && $saveHistoric) {
  120. $t_start_script = microtime(true);
  121. $statsdService = $this->getContainer()->get('statsd');
  122. $statsdService->send($sendData);
  123. $t_end_script = microtime(true);
  124. $time = $t_end_script - $t_start_script;
  125. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  126. }
  127. /* Fin de bloqueo */
  128. $this->removeLock($this->flag);
  129. $fin = microtime(true);
  130. $time = $fin - $inicio;
  131. $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
  132. }
  133. private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
  134. $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
  135. $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
  136. $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
  137. $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
  138. if(empty($bandwidthCached)) $bandwidthCached = array();
  139. $t1 = time();
  140. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  141. $sendData = array(); $subId = $this->d_s;
  142. foreach($dataCached as $index => $data) {
  143. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  144. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  145. if(isset($ifStatsCached[$index])) {
  146. $ifStatsCached[$index]['inOctets'] = 0;
  147. $ifStatsCached[$index]['outOctets'] = 0;
  148. }
  149. if(isset($bandwidthCached[$index])) {
  150. $t0 = $bandwidthCached[$index]['t'];
  151. $in0 = $bandwidthCached[$index]['inOct'];
  152. $out0 = $bandwidthCached[$index]['outOct'];
  153. $inAcc = $bandwidthCached[$index]['inAcc'];
  154. $outAcc = $bandwidthCached[$index]['outAcc'];
  155. $inBandwidth = $outBandwidth = 0;
  156. if(($in1 >= $in0) && ($t1 > $t0)) {
  157. $diff = $in1 - $in0;
  158. $inAcc += $diff;
  159. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  160. }
  161. if(($out1 >= $out0) && ($t1 > $t0)) {
  162. $diff = $out1 - $out0;
  163. $outAcc += $diff;
  164. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  165. }
  166. $totalIn += $inBandwidth;
  167. $totalOut += $outBandwidth;
  168. $sendData["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
  169. $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$outBandwidth}|g";
  170. // Consumo en giga
  171. $div = 1073741824; //bytes => giga
  172. $_consIn = number_format(($inAcc / $div),3,'.','');
  173. $_consOut = number_format(($outAcc / $div),3,'.','');
  174. $sendData["{$subId}_inconsumption_if_{$index}"] = "{$_consIn}|g";
  175. $sendData["{$subId}_outconsumption_if_{$index}"] = "{$_consOut}|g";
  176. // Reinicio mensual
  177. if(date("n",$t0) != date("n",$t1)) {
  178. $inAcc = $outAcc = 0;
  179. }
  180. $totalConsOut += $outAcc;
  181. $totalConsIn += $inAcc;
  182. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  183. if(isset($ifStatsCached[$index])) {
  184. $ifStatsCached[$index]['inOctets'] = $inBandwidth;
  185. $ifStatsCached[$index]['outOctets'] = $outBandwidth;
  186. }
  187. } else {
  188. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  189. }
  190. }
  191. $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
  192. $cmtsBandwidth = array();
  193. $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
  194. $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
  195. $div = 1073741824; //bytes => giga
  196. $consIn = number_format(($totalConsIn / $div),3,'.','');
  197. $consOut = number_format(($totalConsOut / $div),3,'.','');
  198. $cmtsConsumption = array();
  199. $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
  200. $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
  201. $statsdService = $this->getContainer()->get('statsd');
  202. $statsdService->send($cmtsBandwidth);
  203. $statsdService->send($cmtsConsumption);
  204. if($sendData && $saveHistoric) {
  205. $t_start_script = microtime(true);
  206. $statsdService->send($sendData);
  207. $t_end_script = microtime(true);
  208. $time = $t_end_script - $t_start_script;
  209. print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  210. }
  211. }
  212. }