123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- namespace CmtsBundle\Command;
- use BaseStatsBundle\Command\BaseCmtsCommand;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use CmtsBundle\SNMP\SNMP as SNMP;
- use Symfony\Component\Yaml\Parser;
- class CmtsOctetsCommand extends BaseCmtsCommand
- {
- protected function configure()
- {
- $this
- ->setName('cmts:octets')
- ->setDescription('Obtener Octets de CMTS')
- ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
- ->setDefinition(array(
- new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
- new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
- new InputOption('cmts-ip', false, InputOption::VALUE_OPTIONAL, "IP del CMTS"),
- new InputOption('cmts-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS"),
- new InputOption('cmts-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
- new InputOption('cmts-docs', false, InputOption::VALUE_OPTIONAL, "Versión DOCS-QOS del CMTS.", 1),
- new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
- ))
- ;
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- parent::execute($input, $output);
-
- $key_cm_octets = "cm_octets_{$this->d_s}";
- $key_cmts_octets = "cmts_octets_{$this->d_s}";
- $saveHistoric = (int) $input->getOption('save-historic');
- $cmtsDocs = (int) $input->getOption('cmts-docs');
- $inicio = microtime(true);
- $time = time();
- $date = date("Y-m-d");
-
- $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
- $library = "use".$this->cmtsSnmpLibrary;
- $this->apiSNMP = $SNMP->$library();
-
- $cmOctetsCached = $this->getData($key_cm_octets, true);
- $cmtsOctetsCached = $this->getData($key_cmts_octets, true);
- $inBandTotal = $outBandTotal = $inAccTotal = $outAccTotal = 0;
-
- if(empty($cmOctetsCached)) {
- $this->output->writeln("Se inicializa {$key_cm_octets}.");
- $cmOctetsCached = array();
- }
-
- if(empty($cmtsOctetsCached)) {
- $this->output->writeln("Se inicializa {$key_cmts_octets}.");
- $cmtsOctetsCached = array();
- }
- switch($cmtsDocs) {
- case 1:
- $index = $this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs");
- $flows = $this->getSNMP("docsQosServiceFlowDirection","cmtsFlows");
- $octets = $this->getSNMP("docsQosServiceFlowOctets","cmtsOctets");
- break;
-
- case 2:
- $index = array_merge($this->getSNMP("docsQosCmtsIfIndex","cmtsIndexs"),$this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs"));
- $flows = array_merge($this->getSNMP("docsQosServiceFlowDirection","cmtsFlows"),$this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows"));
- $octets = array_merge($this->getSNMP("docsQosServiceFlowOctets","cmtsOctets"),$this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets"));
- break;
-
- case 3:
- $index = $this->getSNMP("docsQos3CmtsIfIndex","cmtsIndexs");
- $flows = $this->getSNMP("docsQos3ServiceFlowDirection","cmtsFlows");
- $octets = $this->getSNMP("docsQos3ServiceFlowOctets","cmtsOctets");
- break;
- }
-
- //Flow Direction: 1 = download(in) / 2 = upload(out)
- $data = array();
- $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
-
- foreach($index as $mac => $_flows) {
- $new = array('flows' => array());
- $down = $up = 0;
- $flowsCached = array();
- if(isset($cmOctetsCached[$mac])) {
- $flowsCached = $cmOctetsCached[$mac]['flows'];
- $inBand = $cmOctetsCached[$mac]['inBand'];
- $outBand = $cmOctetsCached[$mac]['outBand'];
- $inAcc = $cmOctetsCached[$mac]['inAcc'];
- $outAcc = $cmOctetsCached[$mac]['outAcc'];
- $t0 = $cmOctetsCached[$mac]['t'];
- } else {
- $flowsCached = array();
- $outAcc = $inAcc = $outBand = $inBand = 0; $t0 = $time;
- }
-
- $inBandwidth = $outBandwidth = 0;
- foreach($_flows as $flow) {
-
- if(!isset($flows[$flow]) || !isset($octets[$flow])) continue;
- $d = $flows[$flow]; $o1 = $octets[$flow]; $t1 = $time; $acc = 0;
-
- if(isset($flowsCached[$flow])) {
- $o0 = $flowsCached[$flow]['oct'];
-
- if($d == 1) {
- $band = "inBand";
- $acc = "inAcc";
- } else {
- $band = "outBand";
- $acc = "outAcc";
- }
-
- if(($o1 >= $o0) && ($t1 > $t0)) {
- $diff = $o1 - $o0;
- $$acc += $diff;
- $$band += ($diff / ($t1 - $t0)) * 8;
- }
- }
-
- $new['flows'][$flow] = array('d' => $d, 'oct' => $o1);
- }
- $new['inBand'] = $inBand;
- $new['outBand'] = $outBand;
- $new['inAcc'] = $inAcc;
- $new['outAcc'] = $outAcc;
- $new['t'] = $time;
-
- $data[$mac] = $new;
- $sendData["inbandwidth_cm_{$mac}"] = "{$inBand}|g";
- $sendData["outbandwidth_cm_{$mac}"] = "{$outBand}|g";
- $div = 1073741824; //bytes => giga
- $consIn = number_format(($inAcc / $div),3);
- $consOut = number_format(($outAcc / $div),3);
-
- $sendData["inconsumption_cm_{$mac}"] = "{$consIn}|g";
- $sendData["outconsumption_cm_{$mac}"] = "{$consOut}|g";
- $inBandTotal += $inBand;
- $outBandTotal += $outBand;
- $inAccTotal += $inAcc;
- $outAccTotal += $outAcc;
- }
- if($data) $this->setData($key_cm_octets, $data, true);
- $sendData["{$this->d_s}_inbandwidth_cmts_x_cm"] = "{$inBandTotal}|g";
- $sendData["{$this->d_s}_outbandwidth_cmts_x_cm"] = "{$outBandTotal}|g";
- $div = 1073741824; //bytes => giga
- $consIn = number_format(($inAccTotal / $div),3);
- $consOut = number_format(($outAccTotal / $div),3);
- $sendData["{$this->d_s}_inconsumption_cmts_x_cm"] = $consIn;
- $sendData["{$this->d_s}_outconsumption_cmts_x_cm"] = $consOut;
- if($sendData && $saveHistoric) {
- $t_start_script = microtime(true);
- $statsdService = $this->getContainer()->get('statsd');
- $statsdService->send($sendData);
- $t_end_script = microtime(true);
- $time = $t_end_script - $t_start_script;
- print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
- }
-
- /* Fin de bloqueo */
- $this->removeLock($this->flag);
- $fin = microtime(true);
- $time = $fin - $inicio;
- $this->output->writeln("Tiempo: $time segundos");
- }
- private function octets(&$ifStatsCached, $dataCached, $saveHistoric) {
- $inOctets = $this->getSNMP("inOctets","cmtsIfInOctets");
- $outOctets = $this->getSNMP("outOctets","cmtsIfOutOctets");
- $key_cmts_ifs_bandwidth = "cmts_bandwidth_ifs_{$this->d_s}";
- $bandwidthCached = $this->getData($key_cmts_ifs_bandwidth, true);
- if(empty($bandwidthCached)) $bandwidthCached = array();
- $t1 = time();
- $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0;
- $sendData = array(); $subId = $this->d_s;
- foreach($dataCached as $index => $data) {
-
- (isset($inOctets[$index]))? $in1 = $inOctets[$index] : $in1 = 0;
- (isset($outOctets[$index]))? $out1 = $outOctets[$index] : $out1 = 0;
- if(isset($ifStatsCached[$index])) {
- $ifStatsCached[$index]['inOctets'] = 0;
- $ifStatsCached[$index]['outOctets'] = 0;
- }
- if(isset($bandwidthCached[$index])) {
- $t0 = $bandwidthCached[$index]['t'];
- $in0 = $bandwidthCached[$index]['inOct'];
- $out0 = $bandwidthCached[$index]['outOct'];
- $inAcc = $bandwidthCached[$index]['inAcc'];
- $outAcc = $bandwidthCached[$index]['outAcc'];
- $inBandwidth = $outBandwidth = 0;
-
- if(($in1 >= $in0) && ($t1 > $t0)) {
- $diff = $in1 - $in0;
- $inAcc += $diff;
- $inBandwidth = ($diff / ($t1 - $t0)) * 8;
- }
- if(($out1 >= $out0) && ($t1 > $t0)) {
- $diff = $out1 - $out0;
- $outAcc += $diff;
- $outBandwidth = ($diff / ($t1 - $t0)) * 8;
- }
- $totalIn += $inBandwidth;
- $totalOut += $outBandwidth;
- $sendData["{$subId}_inbandwidth_if_{$index}"] = "{$inBandwidth}|g";
- $sendData["{$subId}_outbandwidth_if_{$index}"] = "{$outBandwidth}|g";
- if(date("d",$t0) != date("d",$t1)) {
- $inAcc = $outAcc = 0;
- }
- $totalConsOut += $outAcc;
- $totalConsIn += $inAcc;
-
- $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => $inAcc, 'outAcc' => $outAcc, 'inBand' => $inBandwidth, 'outBand' => $outBandwidth);
- if(isset($ifStatsCached[$index])) {
- $ifStatsCached[$index]['inOctets'] = $inBandwidth;
- $ifStatsCached[$index]['outOctets'] = $outBandwidth;
- }
- } else {
- $bandwidthCached[$index] = array('t' => $t1, 'inOct' => $in1, 'outOct' => $out1, 'inAcc' => 0, 'outAcc' => 0, 'inBand' => 0, 'outBand' => 0);
- }
- }
- $this->setData($key_cmts_ifs_bandwidth, $bandwidthCached, true);
-
- $cmtsBandwidth = array();
- $cmtsBandwidth["{$subId}_inbandwidth_cmts"] = "{$totalIn}|g";
- $cmtsBandwidth["{$subId}_outbandwidth_cmts"] = "{$totalOut}|g";
-
- $div = 1073741824; //bytes => giga
- $consIn = number_format(($totalConsIn / $div),3);
- $consOut = number_format(($totalConsOut / $div),3);
- $cmtsConsumption = array();
- $cmtsConsumption["{$subId}_inconsumption_cmts"] = "{$consIn}|g";
- $cmtsConsumption["{$subId}_outconsumption_cmts"] = "{$consOut}|g";
- $statsdService = $this->getContainer()->get('statsd');
- $statsdService->send($cmtsBandwidth);
- $statsdService->send($cmtsConsumption);
-
- if($sendData && $saveHistoric) {
- $t_start_script = microtime(true);
- $statsdService->send($sendData);
- $t_end_script = microtime(true);
- $time = $t_end_script - $t_start_script;
- print_r("Tiempo de envío de Octets al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
- }
-
- }
- }
|