Module: check_mk
Branch: master
Commit: 0bbf930a23f16bf9e751ea52a3aa669b5a131267
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0bbf930a23f16b…
Author: Bastian Kuhn <bk(a)mathias-kettner.de>
Date: Thu Feb 27 13:47:54 2014 +0100
Step1: Using Regex for Hostnames
---
modules/check_mk.py | 17 +++++++++++++----
web/htdocs/wato.py | 7 +++++--
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 12a5a98..22b2d31 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -1821,7 +1821,8 @@ def in_boolean_serviceconf_list(hostname, service_description,
ruleset):
# Entries in list are (tagged) hostnames that must equal the
# (untagged) hostname. Expressions beginning with ! are negated: if
-# they match, the item is excluded from the list. Also the three
+# they match, the item is excluded from the list. Expressions beginning
+# withy ~ are treated as Regular Expression. Also the three
# special tags '@all', '@clusters', '@physical' are allowed.
def in_extraconf_hostlist(hostlist, hostname):
@@ -1832,6 +1833,8 @@ def in_extraconf_hostlist(hostlist, hostname):
for hostentry in hostlist:
if len(hostentry) == 0:
raise MKGeneralException('Empty hostname in host list %r' %
hostlist)
+ negate = False
+ regex = False
if hostentry[0] == '@':
if hostentry == '@all':
return True
@@ -1845,11 +1848,17 @@ def in_extraconf_hostlist(hostlist, hostname):
elif hostentry[0] == '!':
hostentry = hostentry[1:]
negate = True
- else:
- negate = False
+ # Allow regex with prefix '~'
+ elif hostentry[0] == '~':
+ hostentry = hostentry[1:]
+ regex = True
- if hostname == strip_tags(hostentry):
+ hostentry = strip_tags(hostentry)
+ if not regex and hostname == hostentry:
return not negate
+ # Handle Regex
+ elif re.match(hostentry, hostname):
+ return True
return False
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 372ef61..b65ce1e 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -12725,12 +12725,15 @@ def rule_matches_host_and_item(rulespec, tag_specs, host_list,
item_list,
rule_folder, host_folder, hostname, item):
reasons = []
host = host_folder[".hosts"][hostname]
- if not (
+ hostname_match = False
+
+ if not (
(hostname in host_list)
or
(("!"+hostname) not in host_list
and len(host_list) > 0
- and host_list[-1] == ALL_HOSTS[0])):
+ and host_list[-1] == ALL_HOSTS[0])
+ or len([ x for x in host_list if x.startswith('~') and re.match(x[1:],
hostname) ]) > 0):
reasons.append(_("The host name does not match."))
tags_match = True