Change how options are handled for SnmpQuery (#13488)

* Change how options are handled for SnmpQuery
Fix added backslashes when collecting snmp data.

* fix lint
This commit is contained in:
Tony Murray
2021-11-10 20:49:06 -06:00
committed by GitHub
parent 15632bb875
commit 06701870cd
5 changed files with 31 additions and 32 deletions
+16 -10
View File
@@ -30,6 +30,7 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LibreNMS\Component;
use LibreNMS\Config;
use LibreNMS\Data\Source\SnmpResponse;
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use Symfony\Component\Yaml\Yaml;
@@ -157,23 +158,22 @@ class ModuleTestHelper
$snmp_oids = $this->collectOids($device_id);
}
$device = device_by_id_cache($device_id, true);
DeviceCache::setPrimary($device_id);
$snmprec_data = [];
foreach ($snmp_oids as $oid_data) {
$this->qPrint(' ' . $oid_data['oid']);
$snmp_options = ['-OUneb', '-Ih'];
$snmp_options = ['-OUneb', '-Ih', '-m', '+' . $oid_data['mib']];
if ($oid_data['method'] == 'walk') {
$data = snmp_walk($device, $oid_data['oid'], $snmp_options, $oid_data['mib'], $oid_data['mibdir']);
$data = \SnmpQuery::options($snmp_options)->mibDir($oid_data['mibdir'])->walk($oid_data['oid']);
} elseif ($oid_data['method'] == 'get') {
$data = snmp_get($device, $oid_data['oid'], $snmp_options, $oid_data['mib'], $oid_data['mibdir']);
$data = \SnmpQuery::options($snmp_options)->mibDir($oid_data['mibdir'])->get($oid_data['oid']);
} elseif ($oid_data['method'] == 'getnext') {
$data = snmp_getnext($device, $oid_data['oid'], $snmp_options, $oid_data['mib'], $oid_data['mibdir']);
$data = \SnmpQuery::options($snmp_options)->mibDir($oid_data['mibdir'])->next($oid_data['oid']);
}
if (isset($data) && $data !== false) {
if (isset($data) && $data->isValid()) {
$snmprec_data[] = $this->convertSnmpToSnmprec($data);
}
}
@@ -368,10 +368,10 @@ class ModuleTestHelper
}
}
private function convertSnmpToSnmprec($snmp_data)
private function convertSnmpToSnmprec(SnmpResponse $snmp_data): array
{
$result = [];
foreach (explode(PHP_EOL, $snmp_data) as $line) {
foreach (explode(PHP_EOL, $snmp_data->raw()) as $line) {
if (empty($line)) {
continue;
}
@@ -381,7 +381,7 @@ class ModuleTestHelper
$oid = ltrim($oid, '.');
$raw_data = trim($raw_data);
if (empty($raw_data)) {
if (empty($raw_data) || $raw_data == '""') {
$result[] = "$oid|4|"; // empty data, we don't know type, put string
} else {
[$raw_type, $data] = explode(':', $raw_data, 2);
@@ -389,9 +389,15 @@ class ModuleTestHelper
// device returned the wrong type, save the wrong type to emulate the device behavior
[$raw_type, $data] = explode(':', ltrim($data), 2);
}
$data = ltrim($data, ' "');
$type = $this->getSnmprecType($raw_type);
$data = ltrim($data, ' ');
if (Str::startsWith($data, '"') && Str::endsWith($data, '"')) {
// raw string surrounded by quotes, strip extra escapes
$data = stripslashes(substr($data, 1, -1));
}
if ($type == '6') {
// remove leading . from oid data
$data = ltrim($data, '.');