Module: check_mk
Branch: master
Commit: edc088388cb84d8a56819f1f8a4dc68307bfc6c4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=edc088388cb84d…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Jul 22 08:53:39 2014 +0200
New additional API for checks using yield
---
checks/tcp_conn_stats | 38 +++++++++++---------------
modules/automation.py | 2 +-
modules/check_mk_base.py | 67 ++++++++++++++++++++++++++--------------------
3 files changed, 55 insertions(+), 52 deletions(-)
diff --git a/checks/tcp_conn_stats b/checks/tcp_conn_stats
index d85b48a..64ab3cc 100644
--- a/checks/tcp_conn_stats
+++ b/checks/tcp_conn_stats
@@ -47,31 +47,25 @@ def inventory_tcp_conn_stats(info):
def check_tcp_conn_stats(item, params, info):
stats = dict(info)
- worst_state = 0
- info = []
- perfdata = []
for state, hex in tcp_conn_stats_states:
num = int(stats.get(state, stats.get(hex, 0)))
- if num > 0:
+ if num > 0: # Only check positive counts
infotext = "%s: %d" % (state, num)
- else:
- infotext = None
- levels = params.get(state)
- perf = [state, num]
- if levels:
- warn, crit = levels
- perf.append(warn)
- perf.append(crit)
- if num >= crit:
- worst_state = 2
- infotext += "(!!) (critical at %d)" % crit
- elif num >= warn:
- worst_state = max(1, worst_state)
- infotext += "(!) (warning at %d)" % warn
- perfdata.append(perf)
- if infotext:
- info.append(infotext)
- return (worst_state, ", ".join(info), perfdata)
+ perf = [state, num]
+ levels = params.get(state)
+ if levels:
+ warn, crit = levels
+ perf.append(warn)
+ perf.append(crit)
+ if num >= crit:
+ state = 2
+ infotext += " (critical at %d)" % crit
+ elif num >= warn:
+ state = 1
+ infotext += " (warning at %d)" % warn
+ else:
+ state = 0
+ yield state, infotext, [perf]
check_info["tcp_conn_stats"] = {
diff --git a/modules/automation.py b/modules/automation.py
index 2882a54..93e69c7 100644
--- a/modules/automation.py
+++ b/modules/automation.py
@@ -388,7 +388,7 @@ def automation_try_inventory_node(hostname, leave_no_tcp=False,
with_snmp_scan=F
params = compute_check_parameters(hostname, ct, item, params)
try:
- result = check_function(item, params, info)
+ result = convert_check_result(check_function(item, params, info))
except MKCounterWrapped, e:
result = (None, "WAITING - Counter based check, cannot be done
offline")
except Exception, e:
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 3ca0383..5699b67 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -1127,6 +1127,40 @@ def convert_check_info():
if info["snmp_scan_function"] and basename not in snmp_scan_functions:
snmp_scan_functions[basename] = info["snmp_scan_function"]
+
+def convert_check_result(result):
+ if type(result) == tuple:
+ return result
+
+ # The check function may either return a tuple (pair or triple) or an iterator
+ # (using yield). The latter one is new since version 1.2.5i5.
+ else: # We assume an iterator, convert to tuple
+ subresults = list(result)
+
+ # Simple check with no separate subchecks (yield wouldn't have been
neccessary here!)
+ if len(subresults) == 1:
+ return subresults[0]
+
+ # Several sub results issued with multiple yields. Make that worst sub check
+ # decide the total state, join the texts and performance data
+ else:
+ perfdata = []
+ infotexts = []
+ status = 0
+
+ for subresult in subresults:
+ st, text = subresult[:2]
+ infotexts.append(text + ["", "(!)", "(!!)",
"(?)"][st])
+ if st == 2 or status == 2:
+ status = 2
+ else:
+ status = max(status, st)
+ if len(subresult) == 3:
+ perfdata += subresult[2]
+
+ return status, ", ".join(infotexts), perfdata
+
+
# Loops over all checks for a host, gets the data, calls the check
# function that examines that data and sends the result to Nagios
def do_all_checks_on_host(hostname, ipaddress, only_check_types = None):
@@ -1193,35 +1227,10 @@ def do_all_checks_on_host(hostname, ipaddress, only_check_types =
None):
try:
dont_submit = False
- result = check_function(item, params, info)
- # the new api doesen't return a tuple,
- # it's yield based instead.
- if type(result) != tuple:
- messages = []
- perf = []
- for line in result:
- messages.append(( line[0], line[1] ))
- if len(line) == 3:
- # there can be a single perf value (tuple)
- # or a list of multiple perf values
- if type(line[2]) == tuple:
- perf.append(line[2])
- elif type(line[2]) == list:
- perf += list
- # Check funktion returns only a single line
- if len(messages) == 1:
- result = messages[0][0], messages[0][1], perf
- else:
- # Get the overall status
- return_states = [ x[0] for x in messages ]
- if 3 in return_states:
- if not 2 in return_states and not 1 in return_states:
- status = 3
- else:
- status = max(return_states) - 1
- else:
- status = max(return_states)
- result = status, ", ".join( x[1]+state_markers[x[0]]
for x in messages ), perf
+
+ # Call the actual check function
+ result = convert_check_result(check_function(item, params, info))
+
# handle check implementations that do not yet support the
# handling of wrapped counters via exception. Do not submit