Files
librenms-librenms/app/Console/Commands/SnmpTranslate.php
Jellyfrog 2b3575a5e9 Laravel 10.x Shift (#14995)
* Apply code style

* Remove explicit call to register policies

* Shift core files

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them and merged your true customizations -
where ENV variables may not be used.

* Bump Laravel dependencies

* Add type hints for Laravel 10

* Shift cleanup

* wip

* wip

* sync translation

* Sync back config

* Public Path Binding

* QueryException

* monolog

* db::raw

* monolog

* db::raw

* fix larastan collections

* fix phpstan bug looping forever

* larastan errors

* larastan: fix column type

* styleci

* initialize array

* fixes

* fixes

---------

Co-authored-by: Shift <shift@laravelshift.com>
2023-05-24 22:21:54 +02:00

53 lines
1.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Device;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use LibreNMS\Data\Source\SnmpResponse;
use SnmpQuery;
class SnmpTranslate extends SnmpFetch
{
protected $name = 'snmp:translate';
protected array $oids;
protected function getDevices(): Collection
{
if (empty($this->oids)) {
$this->oids = [$this->deviceSpec];
return new Collection([new Device]); // no device needed, supply dummy
}
$devices = parent::getDevices();
if ($devices->isEmpty()) {
$this->oids = [$this->deviceSpec, ...$this->oids];
return new Collection([new Device]); // no device needed, supply dummy
}
return $devices;
}
protected function fetchData(): SnmpResponse
{
$res = new SnmpResponse('');
// translate does not support multiple oids (should it?)
foreach ($this->oids as $oid) {
$translated = SnmpQuery::numeric($this->numeric)->translate($oid);
// if we got the same back (ignoring . prefix) swap numeric
if (empty($translated) || Str::start($oid, '.') == Str::start($translated, '.')) {
$translated = SnmpQuery::numeric(! $this->numeric)->translate($oid);
}
$res->append(new SnmpResponse($translated . PHP_EOL));
}
return $res;
}
}