Skip to content

Commit

Permalink
snmp.unescape setting (librenms#13590)
Browse files Browse the repository at this point in the history
* snmp.unescape setting
Works around issue with (I think net-snmp < 5.8.0) where it adds backslashes.

* Updated test data
  • Loading branch information
murrant committed Dec 1, 2021
1 parent d69674b commit 5ce7a5a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions LibreNMS/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use LibreNMS\Data\Store\Rrd;
use LibreNMS\DB\Eloquent;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Version;
use Log;

class Config
Expand Down Expand Up @@ -473,6 +474,9 @@ private static function processConfig()
if (! self::has('rrdtool_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();

Expand Down
6 changes: 6 additions & 0 deletions LibreNMS/Data/Source/SnmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use LibreNMS\Config;
use Log;

class SnmpResponse
Expand Down Expand Up @@ -130,6 +131,11 @@ public function values(): array
$line = strtok(PHP_EOL);
}

// remove extra escapes
if (Config::get('snmp.unescape')) {
$value = stripslashes($value);
}

if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
// unformatted string from net-snmp, remove extra escapes
$values[$oid] = trim(stripslashes($value), "\" \n\r");
Expand Down
3 changes: 3 additions & 0 deletions misc/config_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5130,6 +5130,9 @@
"public"
]
},
"snmp.unescape": {
"type": "boolean"
},
"snmp.exec_timeout": {
"default": 1200,
"type": "integer",
Expand Down
27 changes: 24 additions & 3 deletions tests/Unit/SnmpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace LibreNMS\Tests\Unit;

use LibreNMS\Config;
use LibreNMS\Data\Source\SnmpResponse;
use LibreNMS\Tests\TestCase;

Expand All @@ -49,11 +50,31 @@ public function testSimple(): void
$this->assertEquals(['IF-MIB::ifDescr'], $response->table());

// 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->assertEquals('default', $response->value());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName[1]' => 'default', 'Q-BRIDGE-MIB::dot1qVlanStaticName[9]' => '\\Surrounded\\'], $response->values());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName' => [1 => 'default', 9 => '\\Surrounded\\']], $response->table());
Config::set('snmp.unescape', false);
$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
Expand Down
2 changes: 1 addition & 1 deletion tests/data/arista-mos_metamux48.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"sysName": "<private>",
"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>",
"version": "0.32.0",
"hardware": null,
Expand Down

0 comments on commit 5ce7a5a

Please sign in to comment.