Files
librenms-librenms/app/Console/Commands/SnmpTranslate.php
Tony Murray 1baf8f4a1f lnms snmp:translate always show textual and numeric translations (#16187)
* lnms snmp:translate always show textual and numeric translations
accept os name as device spec for translate
values output tidy up for translate and one result

* ingore phpstan
2024-07-09 12:04:18 -05:00

66 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Device;
use Illuminate\Support\Collection;
use LibreNMS\Config;
use LibreNMS\Data\Source\SnmpResponse;
use SnmpQuery;
class SnmpTranslate extends SnmpFetch
{
protected $name = 'snmp:translate';
protected array $oids;
public function __construct()
{
parent::__construct();
// remove numeric option as this shows both
$options = $this->getDefinition()->getOptions();
unset($options['numeric']);
$this->getDefinition()->setOptions($options);
}
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->isNotEmpty()) {
return $devices;
}
// check if the "device" is an valid os, if it is, use that for the dummy device
\LibreNMS\Util\OS::loadDefinition($this->deviceSpec);
if (Config::has('os.' . $this->deviceSpec)) {
return new Collection([new Device(['os' => $this->deviceSpec])]);
}
$this->oids = [$this->deviceSpec, ...$this->oids];
// no device needed, supply dummy
return new Collection([new Device]);
}
protected function fetchData($device): SnmpResponse
{
$res = new SnmpResponse('');
// translate does not support multiple oids (should it?)
foreach ($this->oids as $oid) {
$textual = SnmpQuery::numeric(false)->device($device)->mibs(['ALL'])->translate($oid);
$numeric = SnmpQuery::numeric(true)->device($device)->mibs(['ALL'])->translate($oid);
$response = new SnmpResponse("$textual = $numeric\n");
$res = $res->append($response);
}
return $res;
}
}