Rename NetSnmp to SnmpQuery (#13344)

* Rename NetSnmp to SnmpQuery
Always resolve a new instance.  It was not expected to be a singleton, set options persisted.
This means numeric() works properly now

* fix snmpfetch call and style

* Ability to set device from device array
This commit is contained in:
Tony Murray
2021-10-07 17:03:53 -05:00
committed by GitHub
parent 0d9cd86350
commit 4e94c1e0a1
7 changed files with 48 additions and 20 deletions

View File

@@ -46,7 +46,7 @@ class Device
*
* @param int $device_id
*/
public function setPrimary(int $device_id)
public function setPrimary(int $device_id): void
{
$this->primary = $device_id;
}
@@ -99,12 +99,20 @@ class Device
/**
* Flush the cache
*/
public function flush()
public function flush(): void
{
$this->devices = [];
}
private function load($value, $field = 'device_id')
/**
* Check if the device id is currently loaded into cache
*/
public function has(int $device_id): bool
{
return isset($this->devices[$device_id]);
}
private function load($value, $field = 'device_id'): \App\Models\Device
{
$device = \App\Models\Device::query()->where($field, $value)->first();

View File

@@ -114,6 +114,23 @@ class SnmpQuery
return $this;
}
/**
* Specify a device by a device array.
* The device will be fetched from the cache if it is loaded, otherwise, it will fill the array into a new Device
*/
public function deviceArray(array $device): SnmpQuery
{
if (isset($device['device_id']) && DeviceCache::has($device['device_id'])) {
$this->device = DeviceCache::get($device['device_id']);
return $this;
}
$this->device = new 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
@@ -139,15 +156,9 @@ class SnmpQuery
/**
* Output all OIDs numerically
*/
public function numeric(bool $enabled = true): SnmpQuery
public function numeric(): SnmpQuery
{
if ($enabled) {
$this->options = array_merge($this->options, ['-On']);
return $this;
}
$this->options = array_diff($this->options, ['-On']);
$this->options = array_merge($this->options, ['-On']);
return $this;
}

View File

@@ -32,7 +32,7 @@ use LibreNMS\Data\Source\Fping;
use LibreNMS\Data\Source\FpingResponse;
use LibreNMS\RRD\RrdDefinition;
use Log;
use NetSnmp;
use SnmpQuery;
use Symfony\Component\Process\Process;
class ConnectivityHelper
@@ -133,7 +133,7 @@ class ConnectivityHelper
public function isSNMPable(): bool
{
$response = NetSnmp::device($this->device)->get('SNMPv2-MIB::sysObjectID.0');
$response = SnmpQuery::device($this->device)->get('SNMPv2-MIB::sysObjectID.0');
return $response->getExitCode() === 0 || $response->isValid();
}

View File

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Console\LnmsCommand;
use App\Models\Device;
use Illuminate\Validation\Rule;
use SnmpQuery;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
@@ -54,9 +55,13 @@ class SnmpFetch extends LnmsCommand
$output = $this->option('output')
?: ($type == 'walk' ? 'table' : 'value');
$query = SnmpQuery::make();
if ($this->option('numeric')) {
$query->numeric();
}
/** @var \LibreNMS\Data\Source\SnmpResponse $res */
$res = \NetSnmp::numeric($this->option('numeric'))
->$type($this->argument('oid'));
$res = $query->$type($this->argument('oid'));
if (! $res->isValid()) {
$this->alert(trans('commands.snmp:fetch.failed'));

View File

@@ -26,11 +26,15 @@
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use LibreNMS\Data\Source\SnmpQuery;
class FacadeAccessorSnmp extends Facade
{
protected static function getFacadeAccessor()
{
return \LibreNMS\Data\Source\SnmpQuery::class;
// always resolve a new instance
self::clearResolvedInstance(SnmpQuery::class);
return SnmpQuery::class;
}
}

View File

@@ -246,7 +246,7 @@ return [
'Permissions' => \App\Facades\Permissions::class,
'DeviceCache' => \App\Facades\DeviceCache::class,
'Rrd' => \App\Facades\Rrd::class,
'NetSnmp' => \App\Facades\FacadeAccessorSnmp::class,
'SnmpQuery' => \App\Facades\FacadeAccessorSnmp::class,
],
'charset' => env('CHARSET', ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8'),

View File

@@ -10,7 +10,7 @@ use LibreNMS\Data\Source\SnmpResponse;
use LibreNMS\Polling\ConnectivityHelper;
use LibreNMS\Tests\TestCase;
use Mockery;
use NetSnmp;
use SnmpQuery;
class ConnectivityHelperTest extends TestCase
{
@@ -40,7 +40,7 @@ class ConnectivityHelperTest extends TestCase
// not called when snmp is disabled or ping up
$up = new SnmpResponse('SNMPv2-MIB::sysObjectID.0 = .1');
$down = new SnmpResponse('', '', 1);
NetSnmp::partialMock()->shouldReceive('get')
SnmpQuery::partialMock()->shouldReceive('get')
->times(6)
->andReturn(
$up,
@@ -153,7 +153,7 @@ class ConnectivityHelperTest extends TestCase
public function testIsSNMPable(): void
{
NetSnmp::partialMock()->shouldReceive('get')
SnmpQuery::partialMock()->shouldReceive('get')
->times(4)
->andReturn(
new SnmpResponse('SNMPv2-MIB::sysObjectID.0 = .1', '', 0),