[checkmk-commits] Check_MK Git: check_mk: refined new host regex functions

Bastian Kuhn bk at mathias-kettner.de
Thu Feb 27 17:37:51 CET 2014


Module: check_mk
Branch: master
Commit: aeb8ab49a281cd92b27b2ee8a921f4abf179238d
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=aeb8ab49a281cd92b27b2ee8a921f4abf179238d

Author: Bastian Kuhn <bk at mathias-kettner.de>
Date:   Thu Feb 27 17:37:25 2014 +0100

refined new host regex functions

---

 modules/check_mk.py |    2 +-
 web/htdocs/wato.py  |   41 ++++++++++++++++++++++++++++++++---------
 2 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/modules/check_mk.py b/modules/check_mk.py
index ecc44f1..aa6de5b 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -1871,7 +1871,7 @@ def in_extraconf_hostlist(hostlist, hostname):
             if not use_regex and hostname == hostentry:
                 return not negate
             # Handle Regex
-            elif re.match(regex(hostentry), hostname):
+            elif regex(hostentry).match(hostname):
                 return not negate
         except MKGeneralException:
             if opt_debug:
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index da25b56..5838983 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -12730,21 +12730,44 @@ def parse_rule(ruleset, orig_rule):
         raise MKGeneralException(_("Invalid rule <tt>%s</tt>") % (orig_rule,))
 
 
-
 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]
     hostname_match = False
+    negate = False
+    regex_match = False
+
+    for check_host in host_list:
+        if hostname == check_host:
+            hostname_match = True
+            break
+        else:
+            if check_host[0] == '!':
+                check_host = check_host[1:]
+                negate = True
+            if check_host[0] == '~':
+                check_host = check_host[1:]
+                regex_match = True
+
+            if not regex_match and hostname == check_host:
+                if negate:
+                    break
+                hostname_match = True
+                break
+            elif regex_match and regex(check_host).match(hostname):
+                if negate:
+                    break
+                hostname_match = True
+                break
+
+            # No Match until now, but negate, so thats a match
+            if negate:
+                hostname_match = True
+                break
 
-    if not ( 
-        (hostname in host_list)
-        or
-        (("!"+hostname) not in host_list
-         and len(host_list) > 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."))
+    if not hostname_match:
+        reasons.append(_("The host name does not match."))
 
     tags_match = True
     for tag in tag_specs:



More information about the checkmk-commits mailing list