123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace FiberhomeBundle\Command;
- use BaseStatsBundle\Command\BaseCommand;
- 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 FiberhomeBundle\SNMP\SNMP as SNMP;
- //use RedisBundle\Services\RedisService;
- class FiberhomeOnuStatsCommand extends BaseCommand
- {
- protected function configure()
- {
- $this
- ->setName('fiberhome:onu:stats')
- ->setDescription('Stats de ONUs de OLT')
- ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
- ->setDefinition(array(
- new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT",1),
- new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT",1),
- new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
- new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
- new InputOption('olt-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP"),
- 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_olt_scan = "olt_scan_{$this->d_s}";
- $saveHistoric = (int) $input->getOption('save-historic');
- $inicio = microtime(true);
- $SNMP = new SNMP($this->oltIp, $this->oltCommunity);
- $library = "use".$this->oltSnmpLibrary;
- $this->apiSNMP = $SNMP->$library();
- $dataCached = $this->getData($key_olt_scan, true);
-
- if(empty($dataCached)) {
- $this->output->writeln("Se requiere {$key_olt_scan}.");
- $this->removeLock($this->flag);
- return true;
- }
- $txPower = $this->getSNMP("onuPonTxOpticalPower","onuTxPower");
- $rxPower = $this->getSNMP("onuPonRxOpticalPower","onuRxPower");
- $temperature = $this->getSNMP("onuPonOpticalTemperature","onuTemperature");
- $voltage = $this->getSNMP("onuPonOpticalVltage","onuVoltage");
- $status = $this->getSNMP("onuStatus","onuStatus");
- $stats = $sendData = array();
- $metrics = array("txPower" => "onu_tx_", "rxPower" => "onu_rx_", "temperature" => "onu_temperature_", "voltage" => "onu_voltage_", "status" => "onu_status_");
- foreach($metrics as $m => $metric) {
- $stats[$metric] = array();
- }
- foreach($dataCached as $index => $onu) {
- $sn = strtolower($onu['serialNumber']);
-
- foreach($metrics as $data => $metric) {
- if(isset($$data[$index]) && !empty($$data[$index])) {
- $m = "{$metric}{$sn}";
- if($data != "status") {
- $v = $$data[$index] * 0.01;
- } else {
- ($$data[$index] != 1)? $v = 0 : $v = 1;
- }
- $sendData[$m] = "{$v}|g";
- $stats[$metric][$sn] = $v;
- }
- }
- }
- foreach($stats as $metric => $data) {
- $key_onu_stats = "{$metric}{$this->d_s}";
- $this->setData($key_onu_stats, $data, true);
- }
-
- 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");
-
- }
- }
|