Module: check_mk
Branch: master
Commit: 8ef0d2849f2a6e2ef649c473039da5ed2712ab7c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8ef0d2849f2a6e…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Feb 20 13:45:04 2012 +0100
fix check includes, add node_info to check API
---
ChangeLog | 2 +
modules/check_mk.py | 8 ++++--
modules/check_mk_base.py | 48 +++++++++++++++++++++++++++++++++++++++------
3 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 308cc2c..2f0d46a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,8 @@
* FIX: fix handling of prefix-tag rules (+), needed for WATO
* FIX: handle buggy SNMP devices with non-consecutive OIDS
(such as BINTEC routers)
+ * Check API allows a check to get node information
+ * FIX: fix problem with check includes in subchecks
WATO:
* More checks are configurable via WATO now
diff --git a/modules/check_mk.py b/modules/check_mk.py
index e283474..73a27e9 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -2067,7 +2067,8 @@ def make_inventory(checkname, hostnamelist, check_only=False,
include_state=Fals
try:
inventory = inventory_function(checkname, info) # inventory is a
list of pairs (item, current_value)
except Exception, ee:
- raise e # raise original exception
+ # Let the first exception happen again
+ inventory_function(info)
if inventory == None: # tolerate if function does no explicit return
inventory = []
@@ -2422,8 +2423,9 @@ no_inventory_possible = None
# check table
filenames = []
for check_type in needed_check_types:
+ basename = check_type.split(".")[0]
# Add library files needed by check (also look in local)
- for lib in check_info[check_type].get("includes", []):
+ for lib in set(check_includes.get(basename, [])):
if local_checks_dir and os.path.exists(local_checks_dir + "/" +
lib):
to_add = local_checks_dir + "/" + lib
else:
@@ -4505,7 +4507,7 @@ if __name__ == "__main__":
missing.append(node)
if len(missing) == 0:
if opt_verbose:
- sys.stdout.write("All nodes of %s specified,
dropping checks of %s, too.\n" % (clust, clust))
+ sys.stdout.write("All nodes of %s specified,
dropping checks of %s, too.\n" % (clust, node))
remove_autochecks_of(clust, checknames)
else:
sys.stdout.write("Warning: %s is part of cluster %s, but
you didn't specify %s as well.\nChecks on %s will be kept.\n" %
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index ddd71a0..5856bb0 100755
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -249,8 +249,15 @@ def submit_check_mk_aggregation(hostname, status, output):
# and thus do no network activity at all...
def get_host_info(hostname, ipaddress, checkname):
- nodes = nodes_of(hostname)
+ # If the check want's the node info, we add an additional
+ # column (as the first column) with the name of the node
+ # or None (in case of non-clustered nodes). On problem arises,
+ # if we deal with subchecks. We assume that all subchecks
+ # have the same setting here. If not, let's raise an exception.
+ add_nodeinfo = check_info.get(checkname, {}).get("node_info", False)
+
+ nodes = nodes_of(hostname)
if nodes != None:
info = []
at_least_one_without_exception = False
@@ -263,7 +270,10 @@ def get_host_info(hostname, ipaddress, checkname):
# try the other node.
try:
ipaddress = lookup_ipaddress(node)
- info += get_realhost_info(node, ipaddress, checkname,
cluster_max_cachefile_age)
+ new_info = get_realhost_info(node, ipaddress, checkname,
cluster_max_cachefile_age)
+ if add_nodeinfo:
+ new_info = [ [node] + line for line in new_info ]
+ info += new_info
at_least_one_without_exception = True
except MKAgentError, e:
if str(e) != "": # only first error contains text
@@ -281,7 +291,11 @@ def get_host_info(hostname, ipaddress, checkname):
raise MKAgentError(", ".join(exception_texts))
return info
else:
- return get_realhost_info(hostname, ipaddress, checkname,
check_max_cachefile_age)
+ info = get_realhost_info(hostname, ipaddress, checkname,
check_max_cachefile_age)
+ if add_nodeinfo:
+ return [ [ None ] + line for line in info ]
+ else:
+ return info
# Gets info from a real host (not a cluster). There are three possible
# ways: TCP, SNMP and external command. This function raises
@@ -731,11 +745,13 @@ def check_unimplemented(checkname, params, info):
def convert_check_info():
for check_type, info in check_info.items():
+ basename = check_type.split(".")[0]
+
if type(info) != dict:
+ # Convert check declaration from old style to new API
check_function, service_description, has_perfdata, inventory_function = info
if inventory_function == no_inventory_possible:
inventory_function = None
- basename = check_type.split(".")[0]
check_info[check_type] = {
"check_function" : check_function,
@@ -750,17 +766,35 @@ def convert_check_info():
"snmp_scan_function" :
snmp_scan_functions.get(check_type,
snmp_scan_functions.get(basename)),
- "includes" : check_includes.get(check_type,
[]),
"default_levels_variable" :
check_default_levels.get(check_type),
+ "node_info" : False,
}
else:
- # Make sure that all keys are present
+ # Check does already use new API. Make sure that all keys are present,
+ # extra check-specific information into file-specific variables.
info.setdefault("inventory_function", None)
info.setdefault("group", None)
info.setdefault("snmp_info", None)
info.setdefault("snmp_scan_function", None)
- info.setdefault("includes", [])
info.setdefault("default_levels_variable", None)
+ info.setdefault("node_info", False)
+
+ # Include files are related to the check file (= the basename),
+ # not to the (sub-)check. So we keep them in check_includes.
+ check_includes.setdefault(basename, [])
+ check_includes[basename] += info.get("includes", [])
+
+ # Make sure that setting for node_info of check and subcheck matches
+ for check_type, info in check_info.iteritems():
+ if "." in check_type:
+ base_check = check_type.split(".")[0]
+ if base_check not in check_info:
+ if info["node_info"]:
+ raise MKGeneralException("Invalid check implementation:
node_info for %s is True, but base check %s not defined" %
+ (check_type, base_check))
+ elif check_info[base_check]["node_info"] !=
info["node_info"]:
+ raise MKGeneralException("Invalid check implementation: node_info for
%s and %s are different." % (
+ (base_check, check_type)))
# Now gather snmp_info and snmp_scan_function back to the
# original arrays. Note: these information is tied to a "agent section",