123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?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.", 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));
-
- }
- }
- }
|