CmtsOctetsCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 CmtsOctetsCommand extends BaseCmtsCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('cmts:octets')
  16. ->setDescription('Obtener Octets de CMTS')
  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('cmts-docs', false, InputOption::VALUE_OPTIONAL, "Versión DOCS-QOS del CMTS.", 1),
  25. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  26. ))
  27. ;
  28. }
  29. /**
  30. * @param InputInterface $input
  31. * @param OutputInterface $output
  32. */
  33. protected function execute(InputInterface $input, OutputInterface $output)
  34. {
  35. parent::execute($input, $output);
  36. $key_cm_octets = "cm_octets_{$this->d_s}";
  37. $key_cmts_octets = "cmts_octets_{$this->d_s}";
  38. $saveHistoric = (int) $input->getOption('save-historic');
  39. $cmtsDocs = (int) $input->getOption('cmts-docs');
  40. $inicio = microtime(true);
  41. $time = time();
  42. $date = date("Y-m-d");
  43. $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
  44. $library = "use".$this->cmtsSnmpLibrary;
  45. $this->apiSNMP = $SNMP->$library();
  46. $cmOctetsCached = $this->getData($key_cm_octets, true);
  47. $cmtsOctetsCached = $this->getData($key_cmts_octets, true);
  48. $inBandTotal = $outBandTotal = $inAccTotal = $outAccTotal = 0;
  49. if(empty($cmOctetsCached)) {
  50. $this->output->writeln("Se inicializa {$key_cm_octets}.");
  51. $cmOctetsCached = array();
  52. }
  53. if(empty($cmtsOctetsCached)) {
  54. $this->output->writeln("Se inicializa {$key_cmts_octets}.");
  55. $cmtsOctetsCached = array();
  56. }
  57. switch($cmtsDocs) {
  58. case 1:
  59. $index = $this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs");
  60. $flows = $this->getSNMP("docsQosServiceFlowDirection","cmtsFlows");
  61. $octets = $this->getSNMP("docsQosServiceFlowOctets","cmtsOctets");
  62. break;
  63. case 2:
  64. $index = array_merge($this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs"),$this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs"));
  65. $flows = array_merge($this->getSNMP("docsQosServiceFlowDirection","cmtsFlows"),$this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows"));
  66. $octets = array_merge($this->getSNMP("docsQosServiceFlowOctets","cmtsOctets"),$this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets"));
  67. break;
  68. case 3:
  69. $index = $this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs");
  70. $flows = $this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows");
  71. $octets = $this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets");
  72. break;
  73. }
  74. //Flow Direction: 1 = download(in) / 2 = upload(out)
  75. $data = array();
  76. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  77. foreach($index as $mac => $_flows) {
  78. $new = array('flows' => array());
  79. $down = $up = 0;
  80. $flowsCached = array();
  81. if(isset($cmOctetsCached[$mac])) {
  82. $flowsCached = $cmOctetsCached[$mac]['flows'];
  83. $inBand = $cmOctetsCached[$mac]['inBand'];
  84. $outBand = $cmOctetsCached[$mac]['outBand'];
  85. $inAcc = $cmOctetsCached[$mac]['inAcc'];
  86. $outAcc = $cmOctetsCached[$mac]['outAcc'];
  87. $t0 = $cmOctetsCached[$mac]['t'];
  88. } else {
  89. $flowsCached = array();
  90. $outAcc = $inAcc = $outBand = $inBand = 0; $t0 = $time;
  91. }
  92. $inBandwidth = $outBandwidth = 0;
  93. foreach($_flows as $flow) {
  94. if(!isset($flows[$flow]) || !isset($octets[$flow])) continue;
  95. $d = $flows[$flow]; $o1 = $octets[$flow]; $t1 = $time; $acc = 0;
  96. if(isset($flowsCached[$flow])) {
  97. $o0 = $flowsCached[$flow]['oct'];
  98. if($d == 1) {
  99. $band = "inBand";
  100. $acc = "inAcc";
  101. } else {
  102. $band = "outBand";
  103. $acc = "outAcc";
  104. }
  105. if(($o1 >= $o0) && ($t1 > $t0)) {
  106. $diff = $o1 - $o0;
  107. $$acc += $diff;
  108. $$band += ($diff / ($t1 - $t0)) * 8;
  109. }
  110. }
  111. $new['flows'][$flow] = array('d' => $d, 'oct' => $o1);
  112. }
  113. $new['inBand'] = $inBand;
  114. $new['outBand'] = $outBand;
  115. $new['inAcc'] = $inAcc;
  116. $new['outAcc'] = $outAcc;
  117. $new['t'] = $time;
  118. $data[$mac] = $new;
  119. $sendData["inbandwidth_cm_{$mac}"] = "{$inBand}|g";
  120. $sendData["outbandwidth_cm_{$mac}"] = "{$outBand}|g";
  121. $div = 1073741824; //bytes => giga
  122. $consIn = number_format(($inAcc / $div),3);
  123. $consOut = number_format(($outAcc / $div),3);
  124. $sendData["inconsumption_cm_{$mac}"] = "{$consIn}|g";
  125. $sendData["outconsumption_cm_{$mac}"] = "{$consOut}|g";
  126. $inBandTotal += $inBand;
  127. $outBandTotal += $outBand;
  128. $inAccTotal += $inAcc;
  129. $outAccTotal += $outAcc;
  130. }
  131. if($data) $this->setData($key_cm_octets, $data, true);
  132. $sendData["{$this->d_s}_inbandwidth_cmts_x_cm"] = "{$inBandTotal}|g";
  133. $sendData["{$this->d_s}_outbandwidth_cmts_x_cm"] = "{$outBandTotal}|g";
  134. $div = 1073741824; //bytes => giga
  135. $consIn = number_format(($inAccTotal / $div),3);
  136. $consOut = number_format(($outAccTotal / $div),3);
  137. $sendData["{$this->d_s}_inconsumption_cmts_x_cm"] = $consIn;
  138. $sendData["{$this->d_s}_outconsumption_cmts_x_cm"] = $consOut;
  139. if($sendData && $saveHistoric) {
  140. $t_start_script = microtime(true);
  141. $statsdService = $this->getContainer()->get('statsd');
  142. $statsdService->send($sendData);
  143. $t_end_script = microtime(true);
  144. $time = $t_end_script - $t_start_script;
  145. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  146. }
  147. /* Fin de bloqueo */
  148. $this->removeLock($this->flag);
  149. $fin = microtime(true);
  150. $time = $fin - $inicio;
  151. $this->output->writeln("Tiempo: $time segundos");
  152. }
  153. private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
  154. $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
  155. $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
  156. $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
  157. $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
  158. if(empty($bandwidthCached)) $bandwidthCached = array();
  159. $t1 = time();
  160. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  161. $sendData = array(); $subId = $this->d_s;
  162. foreach($dataCached as $index => $data) {
  163. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  164. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  165. if(isset($ifStatsCached[$index])) {
  166. $ifStatsCached[$index]['inOctets'] = 0;
  167. $ifStatsCached[$index]['outOctets'] = 0;
  168. }
  169. if(isset($bandwidthCached[$index])) {
  170. $t0 = $bandwidthCached[$index]['t'];
  171. $in0 = $bandwidthCached[$index]['inOct'];
  172. $out0 = $bandwidthCached[$index]['outOct'];
  173. $inAcc = $bandwidthCached[$index]['inAcc'];
  174. $outAcc = $bandwidthCached[$index]['outAcc'];
  175. $inBandwidth = $outBandwidth = 0;
  176. if(($in1 >= $in0) && ($t1 > $t0)) {
  177. $diff = $in1 - $in0;
  178. $inAcc += $diff;
  179. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  180. }
  181. if(($out1 >= $out0) && ($t1 > $t0)) {
  182. $diff = $out1 - $out0;
  183. $outAcc += $diff;
  184. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  185. }
  186. $totalIn += $inBandwidth;
  187. $totalOut += $outBandwidth;
  188. $sendData["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
  189. $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$outBandwidth}|g";
  190. if(date("d",$t0) != date("d",$t1)) {
  191. $inAcc = $outAcc = 0;
  192. }
  193. $totalConsOut += $outAcc;
  194. $totalConsIn += $inAcc;
  195. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  196. if(isset($ifStatsCached[$index])) {
  197. $ifStatsCached[$index]['inOctets'] = $inBandwidth;
  198. $ifStatsCached[$index]['outOctets'] = $outBandwidth;
  199. }
  200. } else {
  201. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  202. }
  203. }
  204. $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
  205. $cmtsBandwidth = array();
  206. $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
  207. $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
  208. $div = 1073741824; //bytes => giga
  209. $consIn = number_format(($totalConsIn / $div),3);
  210. $consOut = number_format(($totalConsOut / $div),3);
  211. $cmtsConsumption = array();
  212. $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
  213. $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
  214. $statsdService = $this->getContainer()->get('statsd');
  215. $statsdService->send($cmtsBandwidth);
  216. $statsdService->send($cmtsConsumption);
  217. if($sendData && $saveHistoric) {
  218. $t_start_script = microtime(true);
  219. $statsdService->send($sendData);
  220. $t_end_script = microtime(true);
  221. $time = $t_end_script - $t_start_script;
  222. print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  223. }
  224. }
  225. }