Files
librenms-librenms/app/Console/Commands/SnmpTranslate.php
T
Tony Murray afba58cb29 Fix for lnms snmp:translate (#16159)
Wasn't handling things correctly
Disable numeric if the oid is already numeric and add ALL mibs
2024-06-27 13:04:06 -05:00

62 lines
1.7 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 LibreNMS\Util\Oid;
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) {
if (Oid::isNumeric($oid)) {
$translated = SnmpQuery::numeric(false)->mibs(['ALL'])->translate($oid);
$response = new SnmpResponse($translated . PHP_EOL);
$res = $res->append($response);
continue;
}
$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 = $res->append(new SnmpResponse($translated . PHP_EOL));
}
return $res;
}
}