mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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:
@@ -33,6 +33,7 @@ use Illuminate\Support\Str;
|
||||
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 @@ class Config
|
||||
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();
|
||||
|
||||
|
@@ -28,6 +28,7 @@ namespace LibreNMS\Data\Source;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use LibreNMS\Config;
|
||||
use Log;
|
||||
|
||||
class SnmpResponse
|
||||
@@ -130,6 +131,11 @@ class SnmpResponse
|
||||
$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");
|
||||
|
@@ -5130,6 +5130,9 @@
|
||||
"public"
|
||||
]
|
||||
},
|
||||
"snmp.unescape": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"snmp.exec_timeout": {
|
||||
"default": 1200,
|
||||
"type": "integer",
|
||||
|
@@ -25,6 +25,7 @@
|
||||
|
||||
namespace LibreNMS\Tests\Unit;
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Data\Source\SnmpResponse;
|
||||
use LibreNMS\Tests\TestCase;
|
||||
|
||||
@@ -49,11 +50,31 @@ class SnmpResponseTest extends TestCase
|
||||
$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
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user