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