mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
lnms snmp:fetch command and new SNMP code (#13303)
* SNMP WIP * cleanup, more types * Include my snmp:fetch command * Fix Facade name conflict * Command WIP remove mib * Ignore exit code cleanups * Doc blocks and style fixes * forgot to use parseOid * Hopefully final fixes * missed on (: * small changes deviates from existing code, hopefully doesn't re-add too many corner cases. * add some simple tests, will make it easier to add more in the future when we find corner cases. * test numeric * API refinements, try to avoid setting textual net-snmp options directly * change numeric to a toggle makes for nicer usage * make ci happy * Some errors happen only in stderr, pass that to SnmpResponse for parsing. Add error message access * More consistent naming
This commit is contained in:
395
LibreNMS/Data/Source/SnmpQuery.php
Normal file
395
LibreNMS/Data/Source/SnmpQuery.php
Normal file
@@ -0,0 +1,395 @@
|
||||
<?php
|
||||
/*
|
||||
* SNMP.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2021 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Data\Source;
|
||||
|
||||
use App\Models\Device;
|
||||
use DeviceCache;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Enum\Alert;
|
||||
use LibreNMS\Util\Debug;
|
||||
use LibreNMS\Util\Rewrite;
|
||||
use Log;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class SnmpQuery
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $cleanup = [
|
||||
'command' => [
|
||||
[
|
||||
'/-c\' \'[\S]+\'/',
|
||||
'/-u\' \'[\S]+\'/',
|
||||
'/-U\' \'[\S]+\'/',
|
||||
'/-A\' \'[\S]+\'/',
|
||||
'/-X\' \'[\S]+\'/',
|
||||
'/-P\' \'[\S]+\'/',
|
||||
'/-H\' \'[\S]+\'/',
|
||||
'/(udp|udp6|tcp|tcp6):([^:]+):([\d]+)/',
|
||||
], [
|
||||
'-c\' \'COMMUNITY\'',
|
||||
'-u\' \'USER\'',
|
||||
'-U\' \'USER\'',
|
||||
'-A\' \'PASSWORD\'',
|
||||
'-X\' \'PASSWORD\'',
|
||||
'-P\' \'PASSWORD\'',
|
||||
'-H\' \'HOSTNAME\'',
|
||||
'\1:HOSTNAME:\3',
|
||||
],
|
||||
],
|
||||
'output' => [
|
||||
'/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/',
|
||||
'*',
|
||||
],
|
||||
];
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $context = '';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mibDir;
|
||||
/**
|
||||
* @var array|string
|
||||
*/
|
||||
private $options = [];
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $defaultOptions = ['-OQXUte'];
|
||||
/**
|
||||
* @var \App\Models\Device
|
||||
*/
|
||||
private $device;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->device = DeviceCache::getPrimary();
|
||||
}
|
||||
|
||||
/**
|
||||
* Easy way to start a new instance
|
||||
*/
|
||||
public static function make(): SnmpQuery
|
||||
{
|
||||
return new static;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a device to make the snmp query against.
|
||||
* By default the query will use the primary device.
|
||||
*/
|
||||
public function device(Device $device): SnmpQuery
|
||||
{
|
||||
$this->device = $device;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a context for the snmp query
|
||||
* This is most commonly used to fetch alternate sets of data, such as different VRFs
|
||||
*/
|
||||
public function context(string $context): SnmpQuery
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an additional MIB directory to search for MIBs.
|
||||
* You do not need to specify the base and os directories, they are already included.
|
||||
*/
|
||||
public function mibDir(string $dir): SnmpQuery
|
||||
{
|
||||
$this->mibDir = $dir;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output all OIDs numerically
|
||||
*/
|
||||
public function numeric(bool $enabled = true): SnmpQuery
|
||||
{
|
||||
if ($enabled) {
|
||||
$this->options = array_merge($this->options, ['-On']);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->options = array_diff($this->options, ['-On']);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set option(s) for net-snmp command line.
|
||||
* Some options may break parsing, but you can manually parse the raw output if needed.
|
||||
* This will override other options set such as setting numeric. Call with no options to reset to default.
|
||||
* Try to avoid setting options this way to keep the API generic.
|
||||
*
|
||||
* @param array|string $options
|
||||
* @return $this
|
||||
*/
|
||||
public function options($options = []): SnmpQuery
|
||||
{
|
||||
$this->options = Arr::wrap($options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* snmpget an OID
|
||||
* Commonly used to fetch a single or multiple explicit values.
|
||||
*
|
||||
* @param array|string $oid
|
||||
* @return \LibreNMS\Data\Source\SnmpResponse
|
||||
*/
|
||||
public function get($oid): SnmpResponse
|
||||
{
|
||||
return $this->exec('snmpget', $this->parseOid($oid));
|
||||
}
|
||||
|
||||
/**
|
||||
* snmpwalk an OID
|
||||
* Fetches all OIDs under a given OID, commonly used with tables.
|
||||
*
|
||||
* @param array|string $oid
|
||||
* @return \LibreNMS\Data\Source\SnmpResponse
|
||||
*/
|
||||
public function walk($oid): SnmpResponse
|
||||
{
|
||||
return $this->exec('snmpwalk', $this->parseOid($oid));
|
||||
}
|
||||
|
||||
/**
|
||||
* snmpnext for the given oid
|
||||
* snmpnext retrieves the first oid after the given oid.
|
||||
*
|
||||
* @param array|string $oid
|
||||
* @return \LibreNMS\Data\Source\SnmpResponse
|
||||
*/
|
||||
public function next($oid): SnmpResponse
|
||||
{
|
||||
return $this->exec('snmpgetnext', $this->parseOid($oid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate an OID.
|
||||
* Specify -On option to output numeric OID.
|
||||
*
|
||||
* @param array|string $oid
|
||||
* @return \LibreNMS\Data\Source\SnmpResponse
|
||||
*/
|
||||
public function translate($oid): SnmpResponse
|
||||
{
|
||||
return $this->exec('snmptranslate', $this->parseOid($oid));
|
||||
}
|
||||
|
||||
private function recordStatistic(string $type, float $start_time): void
|
||||
{
|
||||
global $snmp_stats;
|
||||
|
||||
$runtime = microtime(true) - $start_time;
|
||||
$snmp_stats['ops'][$type] = isset($snmp_stats['ops'][$type]) ? $snmp_stats['ops'][$type] + 1 : 0;
|
||||
$snmp_stats['time'][$type] = isset($snmp_stats['time'][$type]) ? $snmp_stats['time'][$type] + $runtime : $runtime;
|
||||
}
|
||||
|
||||
private function buildCli(string $command, array $oids): array
|
||||
{
|
||||
$cmd = $this->initCommand($command);
|
||||
|
||||
array_push($cmd, '-M', $this->mibDirectories());
|
||||
|
||||
if ($command === 'snmptranslate') {
|
||||
return array_merge($cmd, $this->options, $oids);
|
||||
}
|
||||
|
||||
// authentication
|
||||
$this->buildAuth($cmd);
|
||||
|
||||
$cmd = array_merge($cmd, $this->defaultOptions, $this->options);
|
||||
|
||||
$timeout = $this->device->timeout ?? Config::get('snmp.timeout');
|
||||
if ($timeout && $timeout !== 1) {
|
||||
array_push($cmd, '-t', $timeout);
|
||||
}
|
||||
|
||||
$retries = $this->device->retries ?? Config::get('snmp.retries');
|
||||
if ($retries && $retries !== 5) {
|
||||
array_push($cmd, '-r', $retries);
|
||||
}
|
||||
|
||||
$hostname = Rewrite::addIpv6Brackets((string) ($this->device->overwrite_ip ?: $this->device->hostname));
|
||||
$cmd[] = ($this->device->transport ?? 'udp') . ':' . $hostname . ':' . $this->device->port;
|
||||
|
||||
return array_merge($cmd, $oids);
|
||||
}
|
||||
|
||||
private function buildAuth(array &$cmd): void
|
||||
{
|
||||
if ($this->device->snmpver === 'v3') {
|
||||
array_push($cmd, '-v3', '-l', $this->device->authlevel);
|
||||
array_push($cmd, '-n', $this->context);
|
||||
|
||||
switch (strtolower($this->device->authlevel)) {
|
||||
case 'authpriv':
|
||||
array_push($cmd, '-x', $this->device->cryptoalgo);
|
||||
array_push($cmd, '-X', $this->device->cryptopass);
|
||||
// fallthrough
|
||||
case 'authnopriv':
|
||||
array_push($cmd, '-a', $this->device->authalgo);
|
||||
array_push($cmd, '-A', $this->device->authpass);
|
||||
// fallthrough
|
||||
case 'noauthnopriv':
|
||||
array_push($cmd, '-u', $this->device->authname ?: 'root');
|
||||
break;
|
||||
default:
|
||||
Log::debug("Unsupported SNMPv3 AuthLevel: {$this->device->snmpver}");
|
||||
}
|
||||
} elseif ($this->device->snmpver === 'v2c' || $this->device->snmpver === 'v1') {
|
||||
array_push($cmd, '-' . $this->device->snmpver, '-c', $this->context ? "{$this->device->community}@$this->context" : $this->device->community);
|
||||
} else {
|
||||
Log::debug("Unsupported SNMP Version: {$this->device->snmpver}");
|
||||
}
|
||||
}
|
||||
|
||||
private function exec(string $command, array $oids): SnmpResponse
|
||||
{
|
||||
$time_start = microtime(true);
|
||||
|
||||
$proc = new Process($this->buildCli($command, $oids));
|
||||
$proc->setTimeout(Config::get('snmp.exec_timeout', 1200));
|
||||
|
||||
$this->logCommand($proc->getCommandLine());
|
||||
|
||||
$proc->run();
|
||||
$exitCode = $proc->getExitCode();
|
||||
$output = $proc->getOutput();
|
||||
$stderr = $proc->getErrorOutput();
|
||||
|
||||
// check exit code and log possible bad auth
|
||||
$this->checkExitCode($exitCode, $stderr);
|
||||
$this->logOutput($output, $stderr);
|
||||
|
||||
$this->recordStatistic($command, $time_start);
|
||||
|
||||
return new SnmpResponse($output, $stderr, $exitCode);
|
||||
}
|
||||
|
||||
private function initCommand(string $binary): array
|
||||
{
|
||||
if ($binary == 'snmpwalk' && $this->device->snmpver !== 'v1' && ! Config::getOsSetting($this->device->os, 'snmp_bulk', true)) {
|
||||
$snmpcmd = [Config::get('snmpbulkwalk', 'snmpbulkwalk')];
|
||||
|
||||
$max_repeaters = $this->device->getAttrib('snmp_max_repeaters') ?: Config::getOsSetting($this->device->os, 'snmp.max_repeaters', Config::get('snmp.max_repeaters', false));
|
||||
if ($max_repeaters > 0) {
|
||||
$snmpcmd[] = "-Cr$max_repeaters";
|
||||
}
|
||||
|
||||
return $snmpcmd;
|
||||
}
|
||||
|
||||
return [Config::get($binary, $binary)];
|
||||
}
|
||||
|
||||
private function mibDirectories(): string
|
||||
{
|
||||
$base = Config::get('mib_dir');
|
||||
$dirs = [$base];
|
||||
|
||||
// os directory
|
||||
if ($os_mibdir = Config::get("os.{$this->device->os}.mib_dir")) {
|
||||
$dirs[] = "$base/$os_mibdir";
|
||||
} elseif (file_exists($base . '/' . $this->device->os)) {
|
||||
$dirs[] = $base . '/' . $this->device->os;
|
||||
}
|
||||
|
||||
// os group
|
||||
if ($os_group = Config::get("os.{$this->device->os}.os_group")) {
|
||||
if (file_exists("$base/$os_group")) {
|
||||
$dirs[] = "$base/$os_group";
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->mibDir) {
|
||||
$dirs[] = "$base/$this->mibDir";
|
||||
}
|
||||
|
||||
// remove trailing /, remove empty dirs, and remove duplicates
|
||||
$dirs = array_unique(array_filter(array_map(function ($dir) {
|
||||
return rtrim($dir, '/');
|
||||
}, $dirs)));
|
||||
|
||||
return implode(':', $dirs);
|
||||
}
|
||||
|
||||
private function checkExitCode(int $code, string $error): void
|
||||
{
|
||||
if ($code) {
|
||||
if (Str::startsWith($error, 'Invalid authentication protocol specified')) {
|
||||
Log::event('Unsupported SNMP authentication algorithm - ' . $code, $this->device, 'poller', Alert::ERROR);
|
||||
} elseif (Str::startsWith($error, 'Invalid privacy protocol specified')) {
|
||||
Log::event('Unsupported SNMP privacy algorithm - ' . $code, $this->device, 'poller', Alert::ERROR);
|
||||
}
|
||||
Log::debug('Exitcode: ' . $code, [$error]);
|
||||
}
|
||||
}
|
||||
|
||||
private function logCommand(string $command): void
|
||||
{
|
||||
if (Debug::isEnabled() && ! Debug::isVerbose()) {
|
||||
$debug_command = preg_replace($this->cleanup['command'][0], $this->cleanup['command'][1], $command);
|
||||
Log::debug('SNMP[%c' . $debug_command . '%n]', ['color' => true]);
|
||||
} elseif (Debug::isVerbose()) {
|
||||
Log::debug('SNMP[%c' . $command . '%n]', ['color' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
private function logOutput(string $output, string $error): void
|
||||
{
|
||||
if (Debug::isEnabled() && ! Debug::isVerbose()) {
|
||||
Log::debug(preg_replace($this->cleanup['output'][0], $this->cleanup['output'][1], $output));
|
||||
} elseif (Debug::isVerbose()) {
|
||||
Log::debug($output);
|
||||
}
|
||||
Log::debug($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $oid
|
||||
*/
|
||||
private function parseOid($oid): array
|
||||
{
|
||||
return is_string($oid) ? explode(' ', $oid) : $oid;
|
||||
}
|
||||
}
|
160
LibreNMS/Data/Source/SnmpResponse.php
Normal file
160
LibreNMS/Data/Source/SnmpResponse.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/*
|
||||
* SnmpResponse.php
|
||||
*
|
||||
* Responsible for parsing net-snmp output into usable PHP data structures.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2021 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Data\Source;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Log;
|
||||
|
||||
class SnmpResponse
|
||||
{
|
||||
const DELIMITER = ' = ';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $raw;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $exitCode;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $errorMessage;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $stderr;
|
||||
|
||||
/**
|
||||
* Create a new response object filling with output from the net-snmp command.
|
||||
*
|
||||
* @param string $output
|
||||
* @param string $errorOutput
|
||||
* @param int $exitCode
|
||||
*/
|
||||
public function __construct(string $output, string $errorOutput = '', int $exitCode = 0)
|
||||
{
|
||||
$this->exitCode = $exitCode;
|
||||
$this->raw = preg_replace('/Wrong Type \(should be .*\): /', '', $output);
|
||||
$this->stderr = $errorOutput;
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
$this->errorMessage = '';
|
||||
// not checking exitCode because I think it may lead to false negatives
|
||||
$invalid = preg_match('/(Timeout: No Response from .*|Unknown user name|Authentication failure)/', $this->stderr, $errors)
|
||||
|| empty($this->raw)
|
||||
|| preg_match('/(No Such Instance|No Such Object|No more variables left).*/', $this->raw, $errors);
|
||||
|
||||
if ($invalid) {
|
||||
$this->errorMessage = $errors[0] ?? 'Empty Output';
|
||||
Log::debug(sprintf('SNMP query failed. Exit Code: %s Empty: %s Bad String: %s', $this->exitCode, var_export(empty($this->raw), true), $errors[0] ?? 'not found'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error message if any
|
||||
*/
|
||||
public function getErrorMessage(): string
|
||||
{
|
||||
if (empty($this->errorMessage)) {
|
||||
$this->isValid(); // if no error message, double check.
|
||||
}
|
||||
|
||||
return $this->errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw output returned by net-snmp
|
||||
*/
|
||||
public function raw(): string
|
||||
{
|
||||
return $this->raw;
|
||||
}
|
||||
|
||||
public function value(): string
|
||||
{
|
||||
return Arr::first($this->values(), null, '');
|
||||
}
|
||||
|
||||
public function values(): array
|
||||
{
|
||||
$values = [];
|
||||
$line = strtok($this->raw, PHP_EOL);
|
||||
while ($line !== false) {
|
||||
if (Str::contains($line, ['at this OID', 'this MIB View'])) {
|
||||
// these occur when we seek past the end of data, usually the end of the response, but grab the next line and continue
|
||||
$line = strtok(PHP_EOL);
|
||||
continue;
|
||||
}
|
||||
|
||||
[$oid, $value] = explode(self::DELIMITER, $line, 2);
|
||||
|
||||
$line = strtok(PHP_EOL); // get the next line and concatenate multi-line values
|
||||
while ($line !== false && ! Str::contains($line, self::DELIMITER)) {
|
||||
$value .= PHP_EOL . $line;
|
||||
$line = strtok(PHP_EOL);
|
||||
}
|
||||
|
||||
$values[$oid] = $value;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function table(int $group = 0, array &$array = []): array
|
||||
{
|
||||
foreach ($this->values() as $key => $value) {
|
||||
preg_match_all('/([^[\]]+)/', $key, $parts);
|
||||
$parts = $parts[1];
|
||||
array_splice($parts, $group, 0, array_shift($parts)); // move the oid name to the correct depth
|
||||
|
||||
// merge the parts into an array, creating keys if they don't exist
|
||||
$tmp = &$array;
|
||||
foreach ($parts as $part) {
|
||||
$key = trim($part, '"');
|
||||
$tmp = &$tmp[$key];
|
||||
}
|
||||
$tmp = $value; // assign the value as the leaf
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getExitCode(): int
|
||||
{
|
||||
return $this->exitCode;
|
||||
}
|
||||
}
|
93
app/Console/Commands/SnmpFetch.php
Normal file
93
app/Console/Commands/SnmpFetch.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Console\LnmsCommand;
|
||||
use App\Models\Device;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class SnmpFetch extends LnmsCommand
|
||||
{
|
||||
protected $name = 'snmp:fetch';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->addArgument('device spec', InputArgument::REQUIRED);
|
||||
$this->addArgument('oid', InputArgument::REQUIRED);
|
||||
$this->addOption('type', 't', InputOption::VALUE_REQUIRED, trans('commands.snmp:fetch.options.type', ['types' => '[get, walk, next, translate]']), 'get');
|
||||
$this->addOption('output', 'o', InputOption::VALUE_REQUIRED, trans('commands.snmp:fetch.options.output', ['formats' => '[value, values, table]']));
|
||||
$this->addOption('depth', 'd', InputOption::VALUE_REQUIRED, null, 1);
|
||||
$this->addOption('numeric', 'i', InputOption::VALUE_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->validate([
|
||||
'type' => Rule::in(['walk', 'get', 'next', 'translate']),
|
||||
'output' => ['nullable', Rule::in(['value', 'values', 'table'])],
|
||||
]);
|
||||
|
||||
$spec = $this->argument('device spec');
|
||||
$device_id = Device::where('device_id', $spec)->orWhere('hostname', $spec)->value('device_id');
|
||||
if ($device_id == null) {
|
||||
$this->error(trans('commands.snmp:fetch.not_found'));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
\DeviceCache::setPrimary($device_id);
|
||||
|
||||
$type = $this->option('type');
|
||||
$output = $this->option('output')
|
||||
?: ($type == 'walk' ? 'table' : 'value');
|
||||
|
||||
/** @var \LibreNMS\Data\Source\SnmpResponse $res */
|
||||
$res = \NetSnmp::numeric($this->option('numeric'))
|
||||
->$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;
|
||||
}
|
||||
}
|
36
app/Facades/FacadeAccessorSnmp.php
Normal file
36
app/Facades/FacadeAccessorSnmp.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/*
|
||||
* SNMP.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2021 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace App\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class FacadeAccessorSnmp extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return \LibreNMS\Data\Source\SnmpQuery::class;
|
||||
}
|
||||
}
|
@@ -244,7 +244,8 @@ return [
|
||||
// LibreNMS
|
||||
'Permissions' => \App\Facades\Permissions::class,
|
||||
'DeviceCache' => \App\Facades\DeviceCache::class,
|
||||
'Rrd' => App\Facades\Rrd::class,
|
||||
'Rrd' => \App\Facades\Rrd::class,
|
||||
'NetSnmp' => \App\Facades\FacadeAccessorSnmp::class,
|
||||
],
|
||||
|
||||
'charset' => env('CHARSET', ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8'),
|
||||
|
@@ -102,6 +102,22 @@ return [
|
||||
'compat' => '[deprecated] Mimic the behaviour of gen_smokeping.php',
|
||||
],
|
||||
],
|
||||
'snmp:fetch' => [
|
||||
'description' => 'Run snmp query against a device',
|
||||
'arguments' => [
|
||||
'device spec' => 'Device to query: device_id or hostname/ip',
|
||||
'oid' => 'SNMP OID to fetch. Should be either MIB::oid or a numeric oid',
|
||||
],
|
||||
'failed' => 'SNMP command failed!',
|
||||
'oid' => 'OID',
|
||||
'options' => [
|
||||
'type' => 'The type of snmp query to perform :types',
|
||||
'output' => 'Specify the output format :formats',
|
||||
'numeric' => 'Numeric OIDs',
|
||||
],
|
||||
'not_found' => 'Device not found',
|
||||
'value' => 'Value',
|
||||
],
|
||||
'translation:generate' => [
|
||||
'description' => 'Generate updated json language files for use in the web frontend',
|
||||
],
|
||||
|
102
tests/Unit/SnmpResponseTest.php
Normal file
102
tests/Unit/SnmpResponseTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/*
|
||||
* SnmpResponseTest.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2021 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace LibreNMS\Tests\Unit;
|
||||
|
||||
use LibreNMS\Data\Source\SnmpResponse;
|
||||
use LibreNMS\Tests\TestCase;
|
||||
|
||||
class SnmpResponseTest extends TestCase
|
||||
{
|
||||
public function testSimple(): void
|
||||
{
|
||||
$response = new SnmpResponse("IF-MIB::ifDescr[1] = lo\nIF-MIB::ifDescr[2] = enp4s0\n");
|
||||
|
||||
$this->assertTrue($response->isValid());
|
||||
$this->assertEquals(['IF-MIB::ifDescr[1]' => 'lo', 'IF-MIB::ifDescr[2]' => 'enp4s0'], $response->values());
|
||||
$this->assertEquals('lo', $response->value());
|
||||
$this->assertEquals(['IF-MIB::ifDescr' => [1 => 'lo', 2 => 'enp4s0']], $response->table());
|
||||
$this->assertEquals([1 => ['IF-MIB::ifDescr' => 'lo'], 2 => ['IF-MIB::ifDescr' => 'enp4s0']], $response->table(1));
|
||||
}
|
||||
|
||||
public function testMultiLine(): void
|
||||
{
|
||||
$response = new SnmpResponse("SNMPv2-MIB::sysDescr.0 = \"something\n on two lines\"\n");
|
||||
|
||||
$this->assertTrue($response->isValid());
|
||||
$this->assertEquals("\"something\n on two lines\"", $response->value());
|
||||
$this->assertEquals(['SNMPv2-MIB::sysDescr.0' => "\"something\n on two lines\""], $response->values());
|
||||
}
|
||||
|
||||
public function numericTest(): void
|
||||
{
|
||||
$response = new SnmpResponse(".1.3.6.1.2.1.2.2.1.10.1 = 495813425\n.1.3.6.1.2.1.2.2.1.10.2 = 3495809228\n");
|
||||
|
||||
$this->assertTrue($response->isValid());
|
||||
$this->assertEquals('496255256', $response->value());
|
||||
$this->assertEquals(['.1.3.6.1.2.1.2.2.1.10.1' => '496255256', '.1.3.6.1.2.1.2.2.1.10.2' => '3495809228'], $response->values());
|
||||
$this->assertEquals(['.1.3.6.1.2.1.2.2.1.10.1' => '496255256', '.1.3.6.1.2.1.2.2.1.10.2' => '3495809228'], $response->table());
|
||||
$this->assertEquals(['.1.3.6.1.2.1.2.2.1.10.1' => '496255256', '.1.3.6.1.2.1.2.2.1.10.2' => '3495809228'], $response->table(3));
|
||||
}
|
||||
|
||||
public function testErrorHandling(): void
|
||||
{
|
||||
// no response
|
||||
$response = new SnmpResponse('', "Timeout: No Response from udp:127.1.6.1:1161.\n", 1);
|
||||
$this->assertFalse($response->isValid());
|
||||
$this->assertEquals('Timeout: No Response from udp:127.1.6.1:1161.', $response->getErrorMessage());
|
||||
|
||||
// correct handling of empty output
|
||||
$this->assertEmpty($response->value());
|
||||
$this->assertEmpty($response->values());
|
||||
$this->assertEmpty($response->table());
|
||||
|
||||
// invalid type (should ignore)
|
||||
$response = new SnmpResponse("SNMPv2-MIB::sysObjectID.0 = Wrong Type (should be OBJECT IDENTIFIER): wrong thing\n");
|
||||
$this->assertTrue($response->isValid());
|
||||
$this->assertEquals('', $response->getErrorMessage());
|
||||
$this->assertEquals(['SNMPv2-MIB::sysObjectID.0' => 'wrong thing'], $response->values());
|
||||
|
||||
// No more variables left in this MIB View
|
||||
$response = new SnmpResponse("iso.9 = No more variables left in this MIB View (It is past the end of the MIB tree)\n");
|
||||
$this->assertFalse($response->isValid());
|
||||
$this->assertEquals('No more variables left in this MIB View (It is past the end of the MIB tree)', $response->getErrorMessage());
|
||||
|
||||
// No Such Instance currently exists at this OID.
|
||||
$response = new SnmpResponse("SNMPv2-SMI::enterprises.9.9.661.1.3.2.1.1 = No Such Instance currently exists at this OID.\n");
|
||||
$this->assertFalse($response->isValid());
|
||||
$this->assertEquals('No Such Instance currently exists at this OID.', $response->getErrorMessage());
|
||||
|
||||
// Unknown user name
|
||||
$response = new SnmpResponse('', "snmpget: Unknown user name (Sub-id not found: (top) -> sysDescr)\n", 1);
|
||||
$this->assertFalse($response->isValid());
|
||||
$this->assertEquals('Unknown user name', $response->getErrorMessage());
|
||||
|
||||
// Authentication failure
|
||||
$response = new SnmpResponse('', "snmpget: Authentication failure (incorrect password, community or key) (Sub-id not found: (top) -> sysDescr)\n", 1);
|
||||
$this->assertFalse($response->isValid());
|
||||
$this->assertEquals('Authentication failure', $response->getErrorMessage());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user