2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
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
38 import cmk_base
.cee
.keepalive
as keepalive
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"""
48 def wrapped_check_func(hostname
, *args
, **kwargs
):
49 exit_spec
= config
.exit_code_spec(hostname
)
51 status
, infotexts
, long_infotexts
, perfdata
= 0, [], [], []
54 status
, infotexts
, long_infotexts
, perfdata
= check_func(hostname
, *args
, **kwargs
)
60 if _in_keepalive_mode():
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))
75 if cmk
.utils
.debug
.enabled():
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
),
86 output_txt
+= " | %s" % " ".join(perfdata
)
88 output_txt
= "%s\n%s" % (output_txt
, "\n".join(long_infotexts
))
91 if _in_keepalive_mode():
92 keepalive
.add_keepalive_active_check_result(hostname
, output_txt
)
93 console
.verbose(output_txt
.encode("utf-8"))
95 console
.output(output_txt
.encode("utf-8"))
99 return wrapped_check_func
104 def _in_keepalive_mode():
105 return keepalive
and keepalive
.enabled()