Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / cmk_base / decorator.py
blob580be966582addae6804334a9854bd3522e690a5
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 import cmk
28 import cmk.utils.debug
29 import cmk.utils.defines as defines
30 from cmk.utils.exceptions import MKGeneralException, MKTimeout
32 import cmk_base.config as config
33 import cmk_base.console as console
34 import cmk_base.crash_reporting
35 from cmk_base.exceptions import MKAgentError, MKSNMPError, MKIPAddressLookupError
37 try:
38 import cmk_base.cee.keepalive as keepalive
39 except Exception:
40 keepalive = None # type: ignore
43 def handle_check_mk_check_result(check_plugin_name, description):
44 """Decorator function used to wrap all functions used to execute the "Check_MK *" checks
45 Main purpose: Equalize the exception handling of all such functions"""
47 def wrap(check_func):
48 def wrapped_check_func(hostname, *args, **kwargs):
49 exit_spec = config.exit_code_spec(hostname)
51 status, infotexts, long_infotexts, perfdata = 0, [], [], []
53 try:
54 status, infotexts, long_infotexts, perfdata = check_func(hostname, *args, **kwargs)
56 except SystemExit:
57 raise
59 except MKTimeout:
60 if _in_keepalive_mode():
61 raise
62 else:
63 infotexts.append("Timed out")
64 status = max(status, exit_spec.get("timeout", 2))
66 except (MKAgentError, MKSNMPError, MKIPAddressLookupError) as e:
67 infotexts.append("%s" % e)
68 status = exit_spec.get("connection", 2)
70 except MKGeneralException as e:
71 infotexts.append("%s" % e)
72 status = max(status, exit_spec.get("exception", 3))
74 except Exception:
75 if cmk.utils.debug.enabled():
76 raise
77 crash_output = cmk_base.crash_reporting.create_crash_dump(
78 hostname, check_plugin_name, None, False, None, description, [])
79 infotexts.append(crash_output.replace("Crash dump:\n", "Crash dump:\\n"))
80 status = max(status, exit_spec.get("exception", 3))
82 # Produce the service check result output
83 output_txt = "%s - %s" % (defines.short_service_state_name(status),
84 ", ".join(infotexts))
85 if perfdata:
86 output_txt += " | %s" % " ".join(perfdata)
87 if long_infotexts:
88 output_txt = "%s\n%s" % (output_txt, "\n".join(long_infotexts))
89 output_txt += "\n"
91 if _in_keepalive_mode():
92 keepalive.add_keepalive_active_check_result(hostname, output_txt)
93 console.verbose(output_txt.encode("utf-8"))
94 else:
95 console.output(output_txt.encode("utf-8"))
97 return status
99 return wrapped_check_func
101 return wrap
104 def _in_keepalive_mode():
105 return keepalive and keepalive.enabled()