CmtsOctetsCommand.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }