snmp.unescape setting (#13590)

* snmp.unescape setting
Works around issue with (I think net-snmp < 5.8.0) where it adds backslashes.

* Updated test data
This commit is contained in:
Tony Murray
2021-11-30 21:33:18 -06:00
committed by GitHub
parent d69674b36e
commit 5ce7a5ad0e
5 changed files with 38 additions and 4 deletions

View File

@@ -33,6 +33,7 @@ use Illuminate\Support\Str;
use LibreNMS\Data\Store\Rrd; use LibreNMS\Data\Store\Rrd;
use LibreNMS\DB\Eloquent; use LibreNMS\DB\Eloquent;
use LibreNMS\Util\Debug; use LibreNMS\Util\Debug;
use LibreNMS\Util\Version;
use Log; use Log;
class Config class Config
@@ -473,6 +474,9 @@ class Config
if (! self::has('rrdtool_version')) { if (! self::has('rrdtool_version')) {
self::persist('rrdtool_version', Rrd::version()); self::persist('rrdtool_version', Rrd::version());
} }
if (! self::has('snmp.unescape')) {
self::persist('snmp.unescape', version_compare(Version::get()->netSnmp(), '5.8.0', '<'));
}
self::populateTime(); self::populateTime();

View File

@@ -28,6 +28,7 @@ namespace LibreNMS\Data\Source;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LibreNMS\Config;
use Log; use Log;
class SnmpResponse class SnmpResponse
@@ -130,6 +131,11 @@ class SnmpResponse
$line = strtok(PHP_EOL); $line = strtok(PHP_EOL);
} }
// remove extra escapes
if (Config::get('snmp.unescape')) {
$value = stripslashes($value);
}
if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) { if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
// unformatted string from net-snmp, remove extra escapes // unformatted string from net-snmp, remove extra escapes
$values[$oid] = trim(stripslashes($value), "\" \n\r"); $values[$oid] = trim(stripslashes($value), "\" \n\r");

View File

@@ -5130,6 +5130,9 @@
"public" "public"
] ]
}, },
"snmp.unescape": {
"type": "boolean"
},
"snmp.exec_timeout": { "snmp.exec_timeout": {
"default": 1200, "default": 1200,
"type": "integer", "type": "integer",

View File

@@ -25,6 +25,7 @@
namespace LibreNMS\Tests\Unit; namespace LibreNMS\Tests\Unit;
use LibreNMS\Config;
use LibreNMS\Data\Source\SnmpResponse; use LibreNMS\Data\Source\SnmpResponse;
use LibreNMS\Tests\TestCase; use LibreNMS\Tests\TestCase;
@@ -49,11 +50,31 @@ class SnmpResponseTest extends TestCase
$this->assertEquals(['IF-MIB::ifDescr'], $response->table()); $this->assertEquals(['IF-MIB::ifDescr'], $response->table());
// unescaped strings // unescaped strings
$response = new SnmpResponse("Q-BRIDGE-MIB::dot1qVlanStaticName[1] = \"default\"\nQ-BRIDGE-MIB::dot1qVlanStaticName[9] = \"\\\\Surrounded\\\\\""); $response = new SnmpResponse("Q-BRIDGE-MIB::dot1qVlanStaticName[1] = \"\\default\\\"\nQ-BRIDGE-MIB::dot1qVlanStaticName[6] = \\single\\\nQ-BRIDGE-MIB::dot1qVlanStaticName[9] = \\\\double\\\\\n");
$this->assertTrue($response->isValid()); $this->assertTrue($response->isValid());
$this->assertEquals('default', $response->value()); $this->assertEquals('default', $response->value());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName[1]' => 'default', 'Q-BRIDGE-MIB::dot1qVlanStaticName[9]' => '\\Surrounded\\'], $response->values()); Config::set('snmp.unescape', false);
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName' => [1 => 'default', 9 => '\\Surrounded\\']], $response->table()); $this->assertEquals([
'Q-BRIDGE-MIB::dot1qVlanStaticName[1]' => 'default',
'Q-BRIDGE-MIB::dot1qVlanStaticName[6]' => '\\single\\',
'Q-BRIDGE-MIB::dot1qVlanStaticName[9]' => '\\\\double\\\\',
], $response->values());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName' => [
1 => 'default',
6 => '\\single\\',
9 => '\\\\double\\\\',
]], $response->table());
Config::set('snmp.unescape', true); // for buggy versions of net-snmp
$this->assertEquals([
'Q-BRIDGE-MIB::dot1qVlanStaticName[1]' => 'default',
'Q-BRIDGE-MIB::dot1qVlanStaticName[6]' => 'single',
'Q-BRIDGE-MIB::dot1qVlanStaticName[9]' => '\\double\\',
], $response->values());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName' => [
1 => 'default',
6 => 'single',
9 => '\\double\\',
]], $response->table());
} }
public function testMultiLine(): void public function testMultiLine(): void

View File

@@ -5,7 +5,7 @@
{ {
"sysName": "<private>", "sysName": "<private>",
"sysObjectID": ".1.3.6.1.4.1.43191.1.6.7", "sysObjectID": ".1.3.6.1.4.1.43191.1.6.7",
"sysDescr": "Metamako MOS release 0.32.0 \\\\\\\\(build mos-0.32+22\\\\\\\\) running on a MetaMux 48 with L-Series", "sysDescr": "Metamako MOS release 0.32.0 \\\\(build mos-0.32+22\\\\) running on a MetaMux 48 with L-Series",
"sysContact": "<private>", "sysContact": "<private>",
"version": "0.32.0", "version": "0.32.0",
"hardware": null, "hardware": null,