mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Allow SnmpQuery to optionally abort walks if one fails (#14255)
* Allow SnmpQuery to optionally abort walks if one fails * style * remove baseline
This commit is contained in:
@@ -87,6 +87,10 @@ class NetSnmpQuery implements SnmpQueryInterface
|
||||
* @var \App\Models\Device
|
||||
*/
|
||||
private $device;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $abort = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -159,6 +163,17 @@ class NetSnmpQuery implements SnmpQueryInterface
|
||||
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.
|
||||
*/
|
||||
public function abortOnFailure(): SnmpQueryInterface
|
||||
{
|
||||
$this->abort = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not error on out of order indexes.
|
||||
* Use with caution as we could get stuck in an infinite loop.
|
||||
@@ -330,17 +345,20 @@ class NetSnmpQuery implements SnmpQueryInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function execMultiple(string $command, array $oids): ?SnmpResponse
|
||||
private function execMultiple(string $command, array $oids): SnmpResponse
|
||||
{
|
||||
$combined = null;
|
||||
$response = new SnmpResponse('');
|
||||
|
||||
foreach ($oids as $oid) {
|
||||
$response = $this->exec($command, [$oid]);
|
||||
$response = $response->append($this->exec($command, [$oid]));
|
||||
|
||||
$combined = $combined ? $combined->append($response) : $response;
|
||||
// if abort on failure is set, return after first failure
|
||||
if ($this->abort && ! $response->isValid()) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
return $combined;
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function exec(string $command, array $oids): SnmpResponse
|
||||
|
@@ -58,6 +58,12 @@ interface SnmpQueryInterface
|
||||
*/
|
||||
public function mibDir(?string $dir): 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.
|
||||
*/
|
||||
public function abortOnFailure(): SnmpQueryInterface;
|
||||
|
||||
/**
|
||||
* Do not error on out of order indexes.
|
||||
* Use with caution as we could get stuck in an infinite loop.
|
||||
|
Reference in New Issue
Block a user