Module: check_mk
Branch: master
Commit: 737c0e8432d825c9aeaaa393c6153d878eb29b78
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=737c0e8432d825…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Wed Jun 29 10:32:11 2016 +0200
3687 Hostname translation for piggybacked host: now it's possible to enter any number
of regular expressions within this ruleset
Please note: You can add any number of expressions which are executed succesively until
the first match.
---
.werks/3687 | 9 ++++++
ChangeLog | 1 +
modules/check_mk_base.py | 11 ++++++--
web/htdocs/wato.py | 68 ++++++++++++++++++++++++++++------------------
4 files changed, 60 insertions(+), 29 deletions(-)
diff --git a/.werks/3687 b/.werks/3687
new file mode 100644
index 0000000..aa68fd2
--- /dev/null
+++ b/.werks/3687
@@ -0,0 +1,9 @@
+Title: Hostname translation for piggybacked host: now it's possible to enter any
number of regular expressions within this ruleset
+Level: 1
+Component: wato
+Compatible: compat
+Version: 1.4.0i1
+Date: 1467188546
+Class: feature
+
+Please note: You can add any number of expressions which are executed succesively until
the first match.
diff --git a/ChangeLog b/ChangeLog
index 97169e1..7c85fff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -383,6 +383,7 @@
* 3538 Now hiding the activate changes button as long as another activation process
is running
* 3618 Better layout for role & permission matrix, also allow sorting and
searching
* 3544 The edit host and diagnostic page are now able to handle SNMPv3
credentials...
+ * 3687 Hostname translation for piggybacked host: now it's possible to enter any
number of regular expressions within this ruleset...
* 3657 Custom host attributes can now be configured...
* 3060 FIX: Folder properties: Fixed exception when a user has no alias set...
* 3062 FIX: Git integration: Fixed not adding files in WATO folders to git control
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 180767d..a7ba470 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -643,9 +643,13 @@ def do_hostname_translation(translation, hostname):
# We assume the incoming name is correctly encoded in UTF-8
hostname = decode_incoming_string(hostname)
- # 3. Regular expression conversion
- if "regex" in translation:
- expr, subst = translation.get("regex")
+ # 3. Multiple regular expression conversion
+ if type(translation.get("regex")) == tuple:
+ translations = [translation.get("regex")]
+ else:
+ translations = translation.get("regex", [])
+
+ for expr, subst in translations:
if not expr.endswith('$'):
expr += '$'
rcomp = regex(expr)
@@ -655,6 +659,7 @@ def do_hostname_translation(translation, hostname):
hostname = subst
for nr, text in enumerate(mo.groups("")):
hostname = hostname.replace("\\%d" % (nr+1), text)
+ break
# 4. Explicity mapping
for from_host, to_host in translation.get("mapping", []):
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 2059d88..8ca319c 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -13719,29 +13719,40 @@ def HostnameTranslation(**kwargs):
totext = _("Drop domain part
(<tt>host123.foobar.de</tt> → <tt>host123</tt>)"),
)),
( "regex",
- Tuple(
- title = _("Regular expression substitution"),
- help = _("Please specify a regular expression in the first field.
This expression should at "
- "least contain one subexpression exclosed in brackets -
for example <tt>vm_(.*)_prod</tt>. "
- "In the second field you specify the translated host name
and can refer to the first matched "
- "group with <tt>\\1</tt>, the second with
<tt>\\2</tt> and so on, for example
<tt>\\1.example.org</tt>"),
- elements = [
- RegExpUnicode(
- title = _("Regular expression"),
- help = _("Must contain at least one subgroup
<tt>(...)</tt>"),
- mingroups = 0,
- maxgroups = 9,
- size = 30,
- allow_empty = False,
- ),
- TextUnicode(
- title = _("Replacement"),
- help = _("Use <tt>\\1</tt>,
<tt>\\2</tt> etc. to replace matched subgroups"),
- size = 30,
- allow_empty = False,
- )
- ]
- )),
+ Transform(
+ ListOf(
+ Tuple(
+ orientation = "horizontal",
+ elements = [
+ RegExpUnicode(
+ title = _("Regular expression"),
+ help = _("Must contain at least one
subgroup <tt>(...)</tt>"),
+ mingroups = 0,
+ maxgroups = 9,
+ size = 30,
+ allow_empty = False,
+ ),
+ TextUnicode(
+ title = _("Replacement"),
+ help = _("Use <tt>\\1</tt>,
<tt>\\2</tt> etc. to replace matched subgroups"),
+ size = 30,
+ allow_empty = False,
+ )
+ ],
+ ),
+ title = _("Multiple regular expressions"),
+ help = _("You can add any number of expressions here
which are executed succesively until the first match. "
+ "Please specify a regular expression in the
first field. This expression should at "
+ "least contain one subexpression exclosed in
brackets - for example <tt>vm_(.*)_prod</tt>. "
+ "In the second field you specify the
translated host name and can refer to the first matched "
+ "group with <tt>\\1</tt>, the
second with <tt>\\2</tt> and so on, for example
<tt>\\1.example.org</tt>. "
+ ""),
+ add_label = _("Add expression"),
+ movable = False,
+ ),
+ forth = lambda x: type(x) == tuple and [x] or x,
+ )
+ ),
( "mapping",
ListOf(
Tuple(
@@ -13788,9 +13799,13 @@ def do_hostname_translation(translation, hostname):
if translation.get("drop_domain") and not hostname[0].isdigit():
hostname = hostname.split(".", 1)[0]
- # 3. Regular expression conversion
- if "regex" in translation:
- expr, subst = translation.get("regex")
+ # 3. Multiple regular expression conversion
+ if type(translation.get("regex")) == tuple:
+ translations = [translation.get("regex")]
+ else:
+ translations = translation.get("regex", [])
+
+ for expr, subst in translations:
if not expr.endswith('$'):
expr += '$'
rcomp = regex(expr)
@@ -13800,6 +13815,7 @@ def do_hostname_translation(translation, hostname):
hostname = subst
for nr, text in enumerate(mo.groups("")):
hostname = hostname.replace("\\%d" % (nr+1), text)
+ break
# 4. Explicit mapping
for from_host, to_host in translation.get("mapping", []):