Set mibs explicitly in SnmpQuery (#15199)

* Set mibs explicitly in SnmpQuery
Always override system mibs, so we have consistency
New method to control mibs

* Lint fixes
This commit is contained in:
Tony Murray
2023-08-05 07:45:15 -05:00
committed by GitHub
parent d66c09623f
commit 9382959c8c
7 changed files with 102 additions and 98 deletions

View File

@@ -73,26 +73,17 @@ class NetSnmpQuery implements SnmpQueryInterface
'*',
],
];
/**
* @var string
*/
private $context = '';
/**
* @var string[]
*/
private array $mibDirs = [];
/**
* @var array|string
*/
private $options = [self::DEFAULT_FLAGS];
/**
* @var \App\Models\Device
*/
private $device;
/**
* @var bool
*/
private $abort = false;
private string $context = '';
private array|string $options = [self::DEFAULT_FLAGS];
private Device $device;
private bool $abort = false;
// defaults for net-snmp https://net-snmp.sourceforge.io/docs/man/snmpcmd.html
private array $mibs = ['SNMPv2-TC', 'SNMPv2-MIB', 'IF-MIB', 'IP-MIB', 'TCP-MIB', 'UDP-MIB', 'SNMP-VACM-MIB'];
public function __construct()
{
@@ -165,6 +156,17 @@ class NetSnmpQuery implements SnmpQueryInterface
return $this;
}
/**
* Set MIBs to use for this query. Base mibs are included by default.
* They will be appended to existing mibs unless $append is set to false.
*/
public function mibs(array $mibs, bool $append = true): SnmpQueryInterface
{
$this->mibs = $append ? array_merge($this->mibs, $mibs) : $mibs;
return $this;
}
/**
* When walking multiple OIDs, stop if one fails. Used when the first OID indicates if the rest are supported.
* OIDs will be walked in order, so you may want to put your OIDs in a specific order.
@@ -281,7 +283,7 @@ class NetSnmpQuery implements SnmpQueryInterface
* Translate an OID.
* call numeric() on the query to output numeric OID
*/
public function translate(string $oid, ?string $mib = null): string
public function translate(string $oid): string
{
$this->options = array_diff($this->options, [self::DEFAULT_FLAGS]); // remove default options
@@ -299,10 +301,6 @@ class NetSnmpQuery implements SnmpQueryInterface
$this->options[] = '-IR'; // search for mib
}
if ($mib) {
array_push($this->options, '-m', $mib);
}
return $this->exec('snmptranslate', [$oid])->value();
}
@@ -311,6 +309,7 @@ class NetSnmpQuery implements SnmpQueryInterface
$cmd = $this->initCommand($command, $oids);
array_push($cmd, '-M', $this->mibDirectories());
array_push($cmd, '-m', implode(':', $this->mibs));
if ($command === 'snmptranslate') {
return array_merge($cmd, $this->options, $oids);

View File

@@ -58,6 +58,12 @@ interface SnmpQueryInterface
*/
public function mibDir(?string $dir): SnmpQueryInterface;
/**
* Set MIBs to use for this query. Base mibs are included by default.
* They will be appended to existing mibs unless $append is set to false.
*/
public function mibs(array $mibs, bool $append = true): SnmpQueryInterface;
/**
* When walking multiple OIDs, stop if one fails. Used when the first OID indicates if the rest are supported.
* OIDs will be walked in order, so you may want to put your OIDs in a specific order.
@@ -127,5 +133,5 @@ interface SnmpQueryInterface
* Translate an OID.
* Call numeric method prior output numeric OID.
*/
public function translate(string $oid, ?string $mib = null): string;
public function translate(string $oid): string;
}