Module: check_mk
Branch: master
Commit: ae1d167da333523d04ecd5a74e815fcc1edc66c3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ae1d167da33352…
Author: Bastian Kuhn <bk(a)mathias-kettner.de>
Date: Thu Feb 27 15:36:58 2014 +0100
check_mk.py: Added regex cache
---
modules/check_mk.py | 30 +++++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 6df7f77..ecc44f1 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -652,6 +652,18 @@ def agent_target_version(hostname):
else:
return agent_target_versions[0]
+regex_cache = {}
+def regex(r):
+ rx = regex_cache.get(r)
+ if rx:
+ return rx
+ try:
+ rx = re.compile(r)
+ except Exception, e:
+ raise MKGeneralException("Invalid regular expression '%s': %s"
% (r, e))
+ regex_cache[r] = rx
+ return rx
+
#.
# .--SNMP----------------------------------------------------------------.
# | ____ _ _ __ __ ____ |
@@ -1834,7 +1846,7 @@ def in_extraconf_hostlist(hostlist, hostname):
if len(hostentry) == 0:
raise MKGeneralException('Empty hostname in host list %r' %
hostlist)
negate = False
- regex = False
+ use_regex = False
if hostentry[0] == '@':
if hostentry == '@all':
return True
@@ -1852,14 +1864,18 @@ def in_extraconf_hostlist(hostlist, hostname):
# Allow regex with prefix '~'
if hostentry[0] == '~':
hostentry = hostentry[1:]
- regex = True
+ use_regex = True
hostentry = strip_tags(hostentry)
- if not regex and hostname == hostentry:
- return not negate
- # Handle Regex
- elif re.match(hostentry, hostname):
- return not negate
+ try:
+ if not use_regex and hostname == hostentry:
+ return not negate
+ # Handle Regex
+ elif re.match(regex(hostentry), hostname):
+ return not negate
+ except MKGeneralException:
+ if opt_debug:
+ raise
return False