Module: check_mk
Branch: master
Commit: fd0e35722f0e2470dd85e09889a2740b5def9dc5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fd0e35722f0e24…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Mar 9 11:47:31 2015 +0100
Handle exceptions in parse_function() correctly
---
modules/check_mk_base.py | 27 ++++++++++++++++++++++++---
modules/discovery.py | 3 +++
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 77a5ef9..81643af 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -42,7 +42,7 @@ import sys
# to python module names like "random"
sys.path.pop(0)
-import socket, os, time, re, signal, math, tempfile
+import socket, os, time, re, signal, math, tempfile, traceback
# PLANNED CLEANUP:
# - central functions for outputting verbose information and bailing
@@ -235,6 +235,13 @@ class MKAgentError(Exception):
def __str__(self):
return self.reason
+class MKParseFunctionError(Exception):
+ def __init__(self, orig_exception, backtrace):
+ self.orig_exception = orig_exception
+ self.backtrace = backtrace
+ def __str__(self):
+ return str(str(self.orig_exception) + "\n" + self.backtrace)
+
class MKSNMPError(Exception):
def __init__(self, reason):
self.reason = reason
@@ -268,7 +275,14 @@ def apply_parse_function(info, section_name):
if info != None and section_name in check_info:
parse_function = check_info[section_name]["parse_function"]
if parse_function:
- return parse_function(info)
+ try:
+ return parse_function(info)
+ except Exception, e:
+ if opt_debug:
+ raise
+ # In case of a failed parse function return the exception instead of
+ # an empty result.
+ raise MKParseFunctionError(e, traceback.format_exc())
return info
def get_info_for_check(hostname, ipaddress, section_name, max_cachefile_age=None,
ignore_check_interval=False):
@@ -1303,6 +1317,9 @@ def do_all_checks_on_host(hostname, ipaddress, only_check_types =
None):
g_broken_agent_hosts.add(hostname)
continue
+ except MKParseFunctionError, e:
+ info = e
+
if info or info == []:
num_success += 1
try:
@@ -1315,6 +1332,10 @@ def do_all_checks_on_host(hostname, ipaddress, only_check_types =
None):
# Call the actual check function
reset_wrapped_counters()
+
+ if isinstance(info, MKParseFunctionError):
+ raise Exception(str(info))
+
result = convert_check_result(check_function(item, params, info),
check_uses_snmp(checkname))
if last_counter_wrap():
raise last_counter_wrap()
@@ -1330,7 +1351,7 @@ def do_all_checks_on_host(hostname, ipaddress, only_check_types =
None):
except Exception, e:
text = "check failed - please submit a crash report!"
try:
- import traceback, pprint, tarfile, base64
+ import pprint, tarfile, base64
# Create a crash dump with a backtrace and the agent output.
# This is put into a directory per service. The content is then
# put into a tarball, base64 encoded and put into the long output
diff --git a/modules/discovery.py b/modules/discovery.py
index 1c7008c..901c4d4 100644
--- a/modules/discovery.py
+++ b/modules/discovery.py
@@ -386,6 +386,9 @@ def discover_check_type(hostname, ipaddress, check_type, use_caches):
except MKSNMPError, e:
if str(e):
raise
+ except MKParseFunctionError, e:
+ if opt_debug:
+ raise
if info == None: # No data for this check type
return []