diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index 1908f4e6b98a..bb9579ad2cf4 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -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 @@ -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(); diff --git a/LibreNMS/Data/Source/SnmpResponse.php b/LibreNMS/Data/Source/SnmpResponse.php index f4f96c7d2ef1..ea1472eb0fbf 100644 --- a/LibreNMS/Data/Source/SnmpResponse.php +++ b/LibreNMS/Data/Source/SnmpResponse.php @@ -28,6 +28,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use LibreNMS\Config; use Log; class SnmpResponse @@ -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"); diff --git a/misc/config_definitions.json b/misc/config_definitions.json index 65257e6cbc0f..a2f2ca384879 100644 --- a/misc/config_definitions.json +++ b/misc/config_definitions.json @@ -5130,6 +5130,9 @@ "public" ] }, + "snmp.unescape": { + "type": "boolean" + }, "snmp.exec_timeout": { "default": 1200, "type": "integer", diff --git a/tests/Unit/SnmpResponseTest.php b/tests/Unit/SnmpResponseTest.php index 8e76848aad18..4b36d3ceaa80 100644 --- a/tests/Unit/SnmpResponseTest.php +++ b/tests/Unit/SnmpResponseTest.php @@ -25,6 +25,7 @@ namespace LibreNMS\Tests\Unit; +use LibreNMS\Config; use LibreNMS\Data\Source\SnmpResponse; use LibreNMS\Tests\TestCase; @@ -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 diff --git a/tests/data/arista-mos_metamux48.json b/tests/data/arista-mos_metamux48.json index 7f99a4c54730..26d3b21bc605 100644 --- a/tests/data/arista-mos_metamux48.json +++ b/tests/data/arista-mos_metamux48.json @@ -5,7 +5,7 @@ { "sysName": "", "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": "", "version": "0.32.0", "hardware": null,