Module: check_mk
Branch: master
Commit: e2fdab6ddec88ae9cb099cb9f99ce44e25359f07
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e2fdab6ddec88a…
Author: Bastian Kuhn <bk(a)mathias-kettner.de>
Date: Mon Jul 21 14:55:37 2014 +0200
First version of next yield based check api
---
checks/pdu_gude.include | 28 ++++++++++++----------------
modules/check_mk_base.py | 33 +++++++++++++++++++++++++++++++--
2 files changed, 43 insertions(+), 18 deletions(-)
diff --git a/checks/pdu_gude.include b/checks/pdu_gude.include
index 054a7de..64fc745 100644
--- a/checks/pdu_gude.include
+++ b/checks/pdu_gude.include
@@ -38,7 +38,8 @@ def check_pdu_gude(item, params, info):
try:
values = info[item - 1]
except ValueError:
- return (3, "Item not found")
+ yield 3, "Item not found"
+ return
units = {
0 : ("kWh", 1000),
@@ -48,32 +49,27 @@ def check_pdu_gude(item, params, info):
4 : ("VA",False),
}
- message = []
- perf = []
- state = 0
for pos, data in units.items():
value = savefloat(values[pos])
unit, div = data
if div:
value = value / div
warn, crit = params.get(unit, (None, None))
- perf.append((unit, value, warn, crit))
- label = ""
+ perf = unit, value, warn, crit
+ message = "%.2f%s" % (value, unit)
if warn > crit:
if value < crit:
- state = 2
- label = "(!!)"
+ yield 2, message, perf
elif value < warn:
- state = max(state, 1)
- label = "(!)"
+ yield 1, message, perf
+ else:
+ yield 0, message, perf
else:
if crit != None and value > crit:
- state = 2
- label = "(!!)"
+ yield 2, message, perf
elif warn != None and value > warn:
- state = max(state, 1)
- label = "(!)"
- message.append("%.2f%s%s" % (value, unit, label))
+ yield 1, message, perf
+ else:
+ yield 0, message, perf
- return(state, ", ".join(message), perf)
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index bfea8f7..b165056 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -1179,6 +1179,35 @@ 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 )
+
# handle check implementations that do not yet support the
# handling of wrapped counters via exception. Do not submit
# any check result in that case:
@@ -1618,8 +1647,8 @@ def get_regex(pattern):
# Names of texts usually output by checks
nagios_state_names = ["OK", "WARN", "CRIT", "UNKNOWN"]
-# Symbolic representations of states
-state_markers = ["(.)", "(!)", "(!!)", "(?)"]
+# Symbolic representations of states (Needed for new 2.0 check api)
+state_markers = ["", "(!)", "(!!)", "(?)"]
# int() function that return 0 for strings the
# cannot be converted to a number