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.", 2), 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}"; $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); $inBandTotal = $outBandTotal = $inAccTotal = $outAccTotal = 0; if(empty($cmOctetsCached)) { $this->output->writeln("Se inicializa {$key_cm_octets}."); $cmOctetsCached = 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) $consumptionData = $data = array(); $totalConsOut = $totalConsIn = $totalIn = $totalOut = 0; foreach($index as $mac => $_flows) { $new = array('flows' => array()); $inDiff = $outDiff = $inBand = $outBand = 0; $flowsCached = array(); if(isset($cmOctetsCached[$mac])) { $flowsCached = $cmOctetsCached[$mac]['flows']; $inAcc = $cmOctetsCached[$mac]['inAcc']; $outAcc = $cmOctetsCached[$mac]['outAcc']; $t0 = $cmOctetsCached[$mac]['t']; } else { $flowsCached = array(); $outAcc = $inAcc = 0; $t0 = $time; } 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"; $_d = "inDiff"; } else { $band = "outBand"; $acc = "outAcc"; $_d = "outDiff"; } if(($o1 >= $o0) && ($t1 > $t0)) { $diff = $o1 - $o0; $$_d += $diff; $$acc += $diff; $$band += ($diff / ($t1 - $t0)) * 8; } } $new['flows'][$flow] = array('d' => $d, 'oct' => $o1); } $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,'.',''); $div = 1073741824; //bytes => giga $inGB = number_format($inDiff / $div, 5,".",""); $outGB = number_format($outDiff / $div, 5,".",""); $server = $this->cmtsServerId; $fatherDevice = $this->cmtsDeviceId; $fatherDeviceType = 1; $device = $mac; $date = date("Y-m-d"); $consumptionData[] = "({$server},{$fatherDevice},{$fatherDeviceType},'{$device}','{$date}',{$inGB},{$outGB})"; $sendData["inconsumption_cm_{$mac}"] = "{$consIn}|g"; $sendData["outconsumption_cm_{$mac}"] = "{$consOut}|g"; $inBandTotal += $inBand; $outBandTotal += $outBand; $inAccTotal += $inAcc; $outAccTotal += $outAcc; if(date("n",$t0) != date("n",$time)) { $inAcc = $outAcc = 0; } $new['inBand'] = $inBand; $new['outBand'] = $outBand; $new['inAcc'] = $inAcc; $new['outAcc'] = $outAcc; $new['t'] = $time; $data[$mac] = $new; } 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}|g"; $sendData["{$this->d_s}_outconsumption_cmts_x_cm"] = "{$consOut}|g"; $this->saveConsumption($consumptionData); 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"); } /** * @param array $unique_data */ function saveConsumption($data) { if ($data) { $query = "INSERT LOW_PRIORITY INTO `device_consumption` (`server_id`, `father_device_id`, `father_device_type`, `device`, `date`,`cons_in`,`cons_out`) VALUES " . implode(",", $data) . " ON DUPLICATE KEY UPDATE cons_out = cons_out + VALUES(cons_out), cons_in = cons_in + VALUES(cons_in);"; $this->executeQueries(array($query)); } } }