Module: check_mk
Branch: master
Commit: f4cb42070af4a84e55d544410210e8d172997b00
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f4cb42070af4a8…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Jan 6 11:04:35 2015 +0100
Remove bogus duplicate get_regex() function
---
checks/if.include | 4 ++--
checks/logwatch | 5 +----
checks/multipath | 16 ++++++++--------
checks/ps.include | 5 +----
checks/sap | 15 +++------------
checks/services | 2 +-
modules/check_mk.py | 13 +------------
modules/check_mk_base.py | 19 ++++++++++++-------
8 files changed, 29 insertions(+), 50 deletions(-)
diff --git a/checks/if.include b/checks/if.include
index a33f8a7..e758013 100644
--- a/checks/if.include
+++ b/checks/if.include
@@ -121,8 +121,8 @@ def inventory_if_common(info, has_nodeinfo = False):
def alias_matches(alias):
if match_alias == None:
return True
- for regex in match_alias:
- if get_regex(regex).match(alias):
+ for r in match_alias:
+ if regex(r).match(alias):
return True
return False
diff --git a/checks/logwatch b/checks/logwatch
index 74afebd..d4554f9 100644
--- a/checks/logwatch
+++ b/checks/logwatch
@@ -180,10 +180,7 @@ def logwatch_precompile(hostname, item, _unused):
def logwatch_reclassify(counts, patterns, text):
for level, pattern in patterns:
- reg = compiled_regexes.get(pattern)
- if not reg:
- reg = re.compile(pattern)
- compiled_regexes[pattern] = reg
+ reg = regex(pattern)
if reg.search(text):
# If the level is not fixed like 'C' or 'W' but a pair like
(10, 20),
# then we count how many times this pattern has already matched and
diff --git a/checks/multipath b/checks/multipath
index 6025113..f3b0f95 100644
--- a/checks/multipath
+++ b/checks/multipath
@@ -160,16 +160,16 @@ def parse_multipath(info):
# 2: matched regex-group id of alias (optional)
# 3: matched regex-group id of dm-device (optional)
reg_headers = [
- (get_regex(r"^[0-9a-z]{33}$"), 0, None,
None), # 1. (should be included in 3.)
- (get_regex(r"^([^\s]+)\s\(([0-9A-Za-z_-]+)\)"), 2, 1,
None), # 2.
- (get_regex(r"^[a-zA-Z0-9_]+$"), 0, None,
None), # 3.
- (get_regex(r"^([0-9a-z]{33}|[0-9a-z]{49})\s?(dm.[0-9]+).*$"), 1, None,
2), # 4.
- (get_regex(r"^[a-zA-Z0-9_]+(dm-[0-9]+).*$"), 0, None,
1), # 5. Remove this line in 1.2.0
- (get_regex(r"^([-a-zA-Z0-9_ ]+)\s?(dm-[0-9]+).*$"), 1, None,
2), # 6. and 7.
+ (regex(r"^[0-9a-z]{33}$"), 0, None,
None), # 1. (should be included in 3.)
+ (regex(r"^([^\s]+)\s\(([0-9A-Za-z_-]+)\)"), 2, 1,
None), # 2.
+ (regex(r"^[a-zA-Z0-9_]+$"), 0, None,
None), # 3.
+ (regex(r"^([0-9a-z]{33}|[0-9a-z]{49})\s?(dm.[0-9]+).*$"), 1, None, 2),
# 4.
+ (regex(r"^[a-zA-Z0-9_]+(dm-[0-9]+).*$"), 0, None, 1),
# 5. Remove this line in 1.2.0
+ (regex(r"^([-a-zA-Z0-9_ ]+)\s?(dm-[0-9]+).*$"), 1, None, 2),
# 6. and 7.
]
- reg_prio = get_regex("[[ ]prio=")
- reg_lun = get_regex("[0-9]+:[0-9]+:[0-9]+:[0-9]+")
+ reg_prio = regex("[[ ]prio=")
+ reg_lun = regex("[0-9]+:[0-9]+:[0-9]+:[0-9]+")
uuid = None
alias = None
groups = {}
diff --git a/checks/ps.include b/checks/ps.include
index 2ccd451..bcb8de7 100644
--- a/checks/ps.include
+++ b/checks/ps.include
@@ -170,10 +170,7 @@ def process_matches(ps, procname, l_user):
return ()
else:
pattern = procname[1:]
- reg = compiled_regexes.get(pattern)
- if not reg:
- reg = re.compile(pattern)
- compiled_regexes[pattern] = reg
+ reg = regex(pattern)
matchobject = reg.match(" ".join(ps))
if matchobject:
return [ g and g or "" for g in matchobject.groups() ]
diff --git a/checks/sap b/checks/sap
index 060d71f..f265e3d 100644
--- a/checks/sap
+++ b/checks/sap
@@ -115,10 +115,7 @@ def sap_value_path_matches(path, pattern):
elif pattern[0] == '~':
# regex matching
pattern = pattern[1:]
- reg = compiled_regexes.get(pattern)
- if not reg:
- reg = re.compile(pattern)
- compiled_regexes[pattern] = reg
+ reg = regex(pattern)
matchobject = reg.match(path)
if matchobject:
return True
@@ -172,10 +169,7 @@ def sap_groups_of_value( value_name, patterns=False ):
inclusion, exclusion = pattern
# Exclude:
if exclusion:
- reg = compiled_regexes.get(exclusion)
- if not reg:
- reg = re.compile(exclusion)
- compiled_regexes[exclusion] = reg
+ reg = regex(exclusion)
if reg.match(value_name):
no_match = True
break
@@ -184,10 +178,7 @@ def sap_groups_of_value( value_name, patterns=False ):
break
# Include
- reg = compiled_regexes.get(inclusion)
- if not reg:
- reg = re.compile(inclusion)
- compiled_regexes[inclusion] = reg
+ reg = regex(inclusion)
if reg.match(value_name):
groups.append(group_name)
return groups
diff --git a/checks/services b/checks/services
index 6456bb9..4ffaaf7 100644
--- a/checks/services
+++ b/checks/services
@@ -84,7 +84,7 @@ def inventory_windows_services(info):
use_as_item = None
if svc:
if svc.startswith("~"):
- r = get_regex(svc[1:])
+ r = regex(svc[1:])
if not r.match(name) and not r.match(description):
return []
elif svc != name and svc != description:
diff --git a/modules/check_mk.py b/modules/check_mk.py
index fa19744..6b3f36f 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -566,17 +566,6 @@ def agent_target_version(hostname):
else:
return spec # return the whole spec in case of an "at least
version" config
-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----------------------------------------------------------------.
@@ -5442,7 +5431,7 @@ def copy_globals():
# Some global caches are allowed to change.
if varname not in [ "g_service_description",
"g_multihost_checks",
"g_check_table_cache",
"g_singlehost_checks",
- "g_nodesof_cache", "compiled_regexes",
"vars_before_config",
+ "g_nodesof_cache", "g_compiled_regexes",
"vars_before_config",
"g_initial_times",
"g_keepalive_initial_memusage",
"g_dns_cache", "g_ip_lookup_cache" ] \
and type(value).__name__ not in [ "function", "module",
"SRE_Pattern" ]:
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index bad296b..c9b3a48 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -108,9 +108,12 @@ g_single_oid_cache = {}
g_broken_snmp_hosts = set([])
g_broken_agent_hosts = set([])
g_timeout = None
+g_compiled_regexes = {}
-# variables set later by getopt
+# variables set later by getopt. These are defined here since in precompiled
+# mode the module check_mk.py is not present and we need all options to be
+# present.
opt_dont_submit = False
opt_showplain = False
opt_showperfdata = False
@@ -596,7 +599,7 @@ def translate_piggyback_host(sourcehost, backedhost):
regex, subst = translation.get("regex")
if not regex.endswith('$'):
regex += '$'
- rcomp = get_regex(regex)
+ rcomp = regex(regex)
mo = rcomp.match(backedhost)
if mo:
backedhost = subst
@@ -1841,12 +1844,14 @@ def within_range(value, minv, maxv):
# compile regex or look it up in already compiled regexes
# (compiling is a CPU consuming process. We cache compiled
# regexes).
-compiled_regexes = {}
-def get_regex(pattern):
- reg = compiled_regexes.get(pattern)
+def regex(pattern):
+ reg = g_compiled_regexes.get(pattern)
if not reg:
- reg = re.compile(pattern)
- compiled_regexes[pattern] = reg
+ try:
+ reg = re.compile(pattern)
+ except Exception, e:
+ raise MKGeneralException("Invalid regular expression '%s':
%s" % (pattern, e))
+ g_compiled_regexes[pattern] = reg
return reg
# Names of texts usually output by checks