mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
lnms snmp:fetch query multiple devices (#13538)
* lnms snmp:fetch query multiple devices Allows regex and all device spec input * Fix lint * fix regexp
This commit is contained in:
@@ -4,6 +4,8 @@ namespace App\Console\Commands;
|
||||
|
||||
use App\Console\LnmsCommand;
|
||||
use App\Models\Device;
|
||||
use DeviceCache;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Validation\Rule;
|
||||
use SnmpQuery;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
@@ -42,57 +44,67 @@ class SnmpFetch extends LnmsCommand
|
||||
]);
|
||||
|
||||
$spec = $this->argument('device spec');
|
||||
$device_id = Device::where('device_id', $spec)->orWhere('hostname', $spec)->value('device_id');
|
||||
if ($device_id == null) {
|
||||
$device_ids = Device::query()->when($spec !== 'all', function (Builder $query) use ($spec) {
|
||||
return $query->where('device_id', $spec)
|
||||
->orWhere('hostname', 'regexp', $spec);
|
||||
})->pluck('device_id');
|
||||
|
||||
if ($device_ids->isEmpty()) {
|
||||
$this->error(trans('commands.snmp:fetch.not_found'));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
\DeviceCache::setPrimary($device_id);
|
||||
$return = 0;
|
||||
|
||||
$type = $this->option('type');
|
||||
$output = $this->option('output')
|
||||
?: ($type == 'walk' ? 'table' : 'value');
|
||||
foreach ($device_ids as $device_id) {
|
||||
DeviceCache::setPrimary($device_id);
|
||||
$this->info(DeviceCache::getPrimary()->displayName() . ':');
|
||||
|
||||
$query = SnmpQuery::make();
|
||||
if ($this->option('numeric')) {
|
||||
$query->numeric();
|
||||
$type = $this->option('type');
|
||||
$output = $this->option('output')
|
||||
?: ($type == 'walk' ? 'table' : 'value');
|
||||
|
||||
$query = SnmpQuery::make();
|
||||
if ($this->option('numeric')) {
|
||||
$query->numeric();
|
||||
}
|
||||
|
||||
/** @var \LibreNMS\Data\Source\SnmpResponse $res */
|
||||
$res = $query->$type($this->argument('oid'));
|
||||
|
||||
if (! $res->isValid()) {
|
||||
$this->warn(trans('commands.snmp:fetch.failed'));
|
||||
$this->line($res->getErrorMessage());
|
||||
$res->isValid();
|
||||
|
||||
$return = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($output) {
|
||||
case 'value':
|
||||
$this->line($res->value());
|
||||
|
||||
continue 2;
|
||||
case 'values':
|
||||
$values = [];
|
||||
foreach ($res->values() as $oid => $value) {
|
||||
$values[] = [$oid, $value];
|
||||
}
|
||||
$this->table(
|
||||
[trans('commands.snmp:fetch.oid'), trans('commands.snmp:fetch.value')],
|
||||
$values
|
||||
);
|
||||
|
||||
continue 2;
|
||||
case 'table':
|
||||
dump($res->table((int) $this->option('depth')));
|
||||
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
/** @var \LibreNMS\Data\Source\SnmpResponse $res */
|
||||
$res = $query->$type($this->argument('oid'));
|
||||
|
||||
if (! $res->isValid()) {
|
||||
$this->alert(trans('commands.snmp:fetch.failed'));
|
||||
$this->line($res->getErrorMessage());
|
||||
$res->isValid();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch ($output) {
|
||||
case 'value':
|
||||
$this->line($res->value());
|
||||
|
||||
return 0;
|
||||
case 'values':
|
||||
$values = [];
|
||||
foreach ($res->values() as $oid => $value) {
|
||||
$values[] = [$oid, $value];
|
||||
}
|
||||
$this->table(
|
||||
[trans('commands.snmp:fetch.oid'), trans('commands.snmp:fetch.value')],
|
||||
$values
|
||||
);
|
||||
|
||||
return 0;
|
||||
case 'table':
|
||||
dump($res->table((int) $this->option('depth')));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
@@ -125,7 +125,7 @@ return [
|
||||
'snmp:fetch' => [
|
||||
'description' => 'Run snmp query against a device',
|
||||
'arguments' => [
|
||||
'device spec' => 'Device to query: device_id or hostname/ip',
|
||||
'device spec' => 'Device to query: device_id, hostname/ip, hostname regex, or all',
|
||||
'oid' => 'SNMP OID to fetch. Should be either MIB::oid or a numeric oid',
|
||||
],
|
||||
'failed' => 'SNMP command failed!',
|
||||
|
Reference in New Issue
Block a user