From dcc6c96ec4c646feade95eac7f91d402c4dd312b Mon Sep 17 00:00:00 2001 From: Frank Mogaddedi Date: Mon, 24 Apr 2017 15:06:22 -0400 Subject: [PATCH] feature Location map regex replace pattern only (#6485) * Update core.inc.php * Update rewrites.php * Update rewrites.php * Update rewrites.php * Update Configuration.md Adding "location_map_regex_sub" documentation * Update Configuration.md Better(?) example/explanation for location_map_regex_sub. --- doc/Support/Configuration.md | 6 ++++++ includes/polling/core.inc.php | 2 +- includes/rewrites.php | 11 ++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/Support/Configuration.md b/doc/Support/Configuration.md index f7a2a99d76..ae62ece30f 100644 --- a/doc/Support/Configuration.md +++ b/doc/Support/Configuration.md @@ -394,6 +394,12 @@ Regex Matching: ```php $config['location_map_regex']['/Sink/'] = "Under The Sink, The Office, London, UK"; ``` +Regex Match Substitution: +```php +$config['location_map_regex_sub']['/Sink/'] = "Under The Sink, The Office, London, UK [lat, long]"; +``` +If you have an SNMP SysLocation of "Rack10,Rm-314,Sink", Regex Match Substition yields "Rack10,Rm-314,Under The Sink, The Office, London, UK [lat, long]". This allows you to keep the SysLocation string short and keeps Rack/Room/Building information intact after the substitution. + The above are examples, these will rewrite device snmp locations so you don't need to configure full location within snmp. ### Interfaces to be ignored diff --git a/includes/polling/core.inc.php b/includes/polling/core.inc.php index 5f9f93fde6..1e1154883f 100644 --- a/includes/polling/core.inc.php +++ b/includes/polling/core.inc.php @@ -72,7 +72,7 @@ $poll_device['sysLocation'] = str_replace('"', '', $poll_device['sysLocation']); $poll_device['sysLocation'] = trim($poll_device['sysLocation'], '\\'); // Rewrite sysLocation if there is a mapping array (database too?) -if (!empty($poll_device['sysLocation']) && (is_array($config['location_map']) || is_array($config['location_map_regex']))) { +if (!empty($poll_device['sysLocation']) && (is_array($config['location_map']) || is_array($config['location_map_regex']) || is_array($config['location_map_regex_sub']))) { $poll_device['sysLocation'] = rewrite_location($poll_device['sysLocation']); } diff --git a/includes/rewrites.php b/includes/rewrites.php index cd5ea76fae..db8043b352 100644 --- a/includes/rewrites.php +++ b/includes/rewrites.php @@ -14,7 +14,16 @@ function rewrite_location($location) } } } - + + if (is_array($config['location_map_regex_sub'])) { + foreach ($config['location_map_regex_sub'] as $reg => $val) { + if (preg_match($reg, $location)) { + $location = preg_replace($reg, $val, $location); + break; + } + } + } + if (isset($config['location_map'][$location])) { $location = $config['location_map'][$location]; }