Module: check_mk
Branch: master
Commit: 1f8d2adfb5e24a4ec1257574103bfca50fecf35e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1f8d2adfb5e24a…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Nov 23 15:39:58 2012 +0100
Better exception handling when executing "Check_MK"-Check. Printing python
exception to status output and traceback to long output now.
---
.bugs/796 | 8 ++++++--
ChangeLog | 4 ++++
checks/uptime | 2 +-
modules/check_mk.py | 29 +++++++++++++++++++++++++++--
4 files changed, 38 insertions(+), 5 deletions(-)
diff --git a/.bugs/796 b/.bugs/796
index 3de38dc..cc456a2 100644
--- a/.bugs/796
+++ b/.bugs/796
@@ -1,9 +1,9 @@
Title: Better Check-Output if Check_MK check crashes
Component: core
-State: open
+Class: nastiness
+State: done
Date: 2012-10-19 15:46:26
Targetversion: future
-Class: nastiness
In the very very rare and unbelievable case that the Check_MK check
itself crashes - the output of the Check in Nagios is currently '(null)'.
@@ -14,3 +14,7 @@ We should try to wrap the whole code in some general exception handling
2. Make the str(exception) be the plugin output
3. Optionally put the exception trace into the long output
4. If the debug log is active, then also log into that something
+
+2012-11-23 15:28:57: changed state open -> done
+Better exception handling when executing "Check_MK"-Check. Printing python
+ exception to status output and traceback to long output now.
diff --git a/ChangeLog b/ChangeLog
index c9ab647..b810a0a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
1.2.1i4:
+ Core:
+ * Better exception handling when executing "Check_MK"-Check. Printing
python
+ exception to status output and traceback to long output now.
+
Notifications:
* Fix flexible notifications on non-OMD systems
diff --git a/checks/uptime b/checks/uptime
index 9cb6749..0942d2c 100644
--- a/checks/uptime
+++ b/checks/uptime
@@ -37,7 +37,7 @@ def check_uptime(_no_item, _no_params, info):
days = rem / 1440
now = int(time.time())
since = time.strftime("%c", time.localtime(now - uptime))
- return (0, "OK - up since %s (%dd %02d:%02d:%02d)" % (since, days, hours,
minutes, seconds), [ ("uptime", uptime) ])
+ return ('xxx', "OK - up since %s (%dd %02d:%02d:%02d)" % (since,
days, hours, minutes, seconds), [ ("uptime", uptime) ])
check_info["uptime"] = (check_uptime, "Uptime", 1, inventory_uptime)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 122389d..8eeb594 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -2791,8 +2791,33 @@ no_inventory_possible = None
for var in check_info[check_type].get("check_config_variables", []):
output.write("%s = %r\n" % (var, eval(var)))
- # perform actual check
- output.write("do_check(%r, %r)\n" % (hostname, ipaddress))
+ # perform actual check with a general exception handler
+ output.write("try:\n")
+ output.write(" do_check(%r, %r)\n" % (hostname, ipaddress))
+ output.write("except Exception, e:\n")
+ output.write(" import traceback, pprint\n")
+
+ # status output message
+ output.write(" sys.stdout.write(\"UNKNOWN - Exception in precompiled
check: %s (details in long output)\\n\" % e)\n")
+
+ # generate traceback for long output
+ output.write(" sys.stdout.write(\"Traceback: %s\\n\" %
traceback.format_exc())\n")
+
+ # debug logging
+ output.write(" if debug_log:\n")
+ output.write(" l = file(debug_log, \"a\")\n")
+ output.write(" l.write((\"Exception in precompiled
check:\\n\"\n")
+ output.write(" \" Check_MK Version: %s\\n\"\n")
+ output.write(" \" Date: %s\\n\"\n")
+ output.write(" \" Host: %s\\n\"\n")
+ output.write(" \" %s\\n\") % (\n")
+ output.write(" check_mk_version,\n")
+ output.write(" time.strftime(\"%Y-%d-%m
%H:%M:%S\"),\n")
+ output.write(" \"%s\",\n" % hostname)
+ output.write(" traceback.format_exc().replace('\\n',
'\\n ')))\n")
+ output.write(" l.close()\n")
+
+ output.write(" sys.exit(3)\n")
output.close()
# compile python (either now or delayed), but only if the source