CmtsOctetsCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. $inBand = $outBand = 0;
  80. $flowsCached = array();
  81. if(isset($cmOctetsCached[$mac])) {
  82. $flowsCached = $cmOctetsCached[$mac]['flows'];
  83. $inAcc = $cmOctetsCached[$mac]['inAcc'];
  84. $outAcc = $cmOctetsCached[$mac]['outAcc'];
  85. $t0 = $cmOctetsCached[$mac]['t'];
  86. } else {
  87. $flowsCached = array();
  88. $outAcc = $inAcc = 0; $t0 = $time;
  89. }
  90. foreach($_flows as $flow) {
  91. if(!isset($flows[$flow]) || !isset($octets[$flow])) continue;
  92. $d = $flows[$flow]; $o1 = $octets[$flow]; $t1 = $time; $acc = 0;
  93. if(isset($flowsCached[$flow])) {
  94. $o0 = $flowsCached[$flow]['oct'];
  95. if($d == 1) {
  96. $band = "inBand";
  97. $acc = "inAcc";
  98. } else {
  99. $band = "outBand";
  100. $acc = "outAcc";
  101. }
  102. if(($o1 >= $o0) && ($t1 > $t0)) {
  103. $diff = $o1 - $o0;
  104. $$acc += $diff;
  105. $$band += ($diff / ($t1 - $t0)) * 8;
  106. }
  107. }
  108. $new['flows'][$flow] = array('d' => $d, 'oct' => $o1);
  109. }
  110. $new['inBand'] = $inBand;
  111. $new['outBand'] = $outBand;
  112. $new['inAcc'] = $inAcc;
  113. $new['outAcc'] = $outAcc;
  114. $new['t'] = $time;
  115. $data[$mac] = $new;
  116. $sendData["inbandwidth_cm_{$mac}"] = "{$inBand}|g";
  117. $sendData["outbandwidth_cm_{$mac}"] = "{$outBand}|g";
  118. $div = 1073741824; //bytes => giga
  119. $consIn = number_format(($inAcc / $div),3);
  120. $consOut = number_format(($outAcc / $div),3);
  121. $sendData["inconsumption_cm_{$mac}"] = "{$consIn}|g";
  122. $sendData["outconsumption_cm_{$mac}"] = "{$consOut}|g";
  123. $inBandTotal += $inBand;
  124. $outBandTotal += $outBand;
  125. $inAccTotal += $inAcc;
  126. $outAccTotal += $outAcc;
  127. }
  128. if($data) $this->setData($key_cm_octets, $data, true);
  129. $sendData["{$this->d_s}_inbandwidth_cmts_x_cm"] = "{$inBandTotal}|g";
  130. $sendData["{$this->d_s}_outbandwidth_cmts_x_cm"] = "{$outBandTotal}|g";
  131. $div = 1073741824; //bytes => giga
  132. $consIn = number_format(($inAccTotal / $div),3);
  133. $consOut = number_format(($outAccTotal / $div),3);
  134. $sendData["{$this->d_s}_inconsumption_cmts_x_cm"] = "{$consIn}|g";
  135. $sendData["{$this->d_s}_outconsumption_cmts_x_cm"] = "{$consOut}|g";
  136. if($sendData && $saveHistoric) {
  137. $t_start_script = microtime(true);
  138. $statsdService = $this->getContainer()->get('statsd');
  139. $statsdService->send($sendData);
  140. $t_end_script = microtime(true);
  141. $time = $t_end_script - $t_start_script;
  142. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  143. }
  144. /* Fin de bloqueo */
  145. $this->removeLock($this->flag);
  146. $fin = microtime(true);
  147. $time = $fin - $inicio;
  148. $this->output->writeln("Tiempo: $time segundos");
  149. }
  150. private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
  151. $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
  152. $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
  153. $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
  154. $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
  155. if(empty($bandwidthCached)) $bandwidthCached = array();
  156. $t1 = time();
  157. $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
  158. $sendData = array(); $subId = $this->d_s;
  159. foreach($dataCached as $index => $data) {
  160. (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
  161. (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
  162. if(isset($ifStatsCached[$index])) {
  163. $ifStatsCached[$index]['inOctets'] = 0;
  164. $ifStatsCached[$index]['outOctets'] = 0;
  165. }
  166. if(isset($bandwidthCached[$index])) {
  167. $t0 = $bandwidthCached[$index]['t'];
  168. $in0 = $bandwidthCached[$index]['inOct'];
  169. $out0 = $bandwidthCached[$index]['outOct'];
  170. $inAcc = $bandwidthCached[$index]['inAcc'];
  171. $outAcc = $bandwidthCached[$index]['outAcc'];
  172. $inBandwidth = $outBandwidth = 0;
  173. if(($in1 >= $in0) && ($t1 > $t0)) {
  174. $diff = $in1 - $in0;
  175. $inAcc += $diff;
  176. $inBandwidth = ($diff / ($t1 - $t0)) * 8;
  177. }
  178. if(($out1 >= $out0) && ($t1 > $t0)) {
  179. $diff = $out1 - $out0;
  180. $outAcc += $diff;
  181. $outBandwidth = ($diff / ($t1 - $t0)) * 8;
  182. }
  183. $totalIn += $inBandwidth;
  184. $totalOut += $outBandwidth;
  185. $sendData["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
  186. $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$outBandwidth}|g";
  187. if(date("d",$t0) != date("d",$t1)) {
  188. $inAcc = $outAcc = 0;
  189. }
  190. $totalConsOut += $outAcc;
  191. $totalConsIn += $inAcc;
  192. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
  193. if(isset($ifStatsCached[$index])) {
  194. $ifStatsCached[$index]['inOctets'] = $inBandwidth;
  195. $ifStatsCached[$index]['outOctets'] = $outBandwidth;
  196. }
  197. } else {
  198. $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
  199. }
  200. }
  201. $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
  202. $cmtsBandwidth = array();
  203. $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
  204. $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
  205. $div = 1073741824; //bytes => giga
  206. $consIn = number_format(($totalConsIn / $div),3);
  207. $consOut = number_format(($totalConsOut / $div),3);
  208. $cmtsConsumption = array();
  209. $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
  210. $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
  211. $statsdService = $this->getContainer()->get('statsd');
  212. $statsdService->send($cmtsBandwidth);
  213. $statsdService->send($cmtsConsumption);
  214. if($sendData && $saveHistoric) {
  215. $t_start_script = microtime(true);
  216. $statsdService->send($sendData);
  217. $t_end_script = microtime(true);
  218. $time = $t_end_script - $t_start_script;
  219. print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  220. }
  221. }
  222. }