/
check_mk
executable file
·124 lines (106 loc) · 4.88 KB
/
check_mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# | ____ _ _ __ __ _ __ |
# | / ___| |__ ___ ___| | __ | \/ | |/ / |
# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
# | | |___| | | | __/ (__| < | | | | . \ |
# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
# | |
# | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation in version 2. check_mk is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more de-
# tails. You should have received a copy of the GNU General Public
# License along with GNU Make; see the file COPYING. If not, write
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
# Future convention within all Check_MK modules for variable names:
#
# - host_name - Monitoring name of a host (string)
# - node_name - Name of cluster member (string)
# - cluster_name - Name of a cluster (string)
# - realhost_name - Name of a *real* host, not a cluster (string)
import logging
import os
import sys
import getopt
# Needs to be placed before cmk modules, because they are not available
# when executed as non site user. Alas, this screws up our import ordering
# a bit, so we have to tell pylint about that.
# pylint: disable=wrong-import-position
if not os.environ.get("OMD_SITE"):
sys.stderr.write("Check_MK can be used only as site user.\n")
sys.exit(1)
from cmk.utils.exceptions import MKGeneralException, MKTerminate, MKBailOut
import cmk.utils.debug
import cmk.utils.log
import cmk.utils.paths
import cmk.base # pylint: disable=cmk-module-layer-violation
import cmk.base.console as console # pylint: disable=cmk-module-layer-violation
import cmk.base.config as config # pylint: disable=cmk-module-layer-violation
import cmk.base.profiling as profiling # pylint: disable=cmk-module-layer-violation
from cmk.base.modes import modes # pylint: disable=cmk-module-layer-violation
import cmk.base.check_api as check_api # pylint: disable=cmk-module-layer-violation
import cmk.base.crash_reporting # pylint: disable=cmk-module-layer-violation
cmk.utils.log.setup_console_logging()
logger = logging.getLogger("cmk.base")
cmk.base.utils.register_sigint_handler()
try:
opts, args = getopt.getopt(sys.argv[1:], modes.short_getopt_specs(), modes.long_getopt_specs())
except getopt.GetoptError as err:
console.error("ERROR: %s\n\n" % err)
modes.get("help").handler_function()
sys.exit(1)
# First load the general modifying options
modes.process_general_options(opts)
try:
# Now find the requested mode and execute it
mode_name, mode_args = None, None
for o, a in opts:
if modes.exists(o):
mode_name, mode_args = o, a
break
# At least in case the config is needed, the checks are needed too, because
# the configuration may refer to check config variable names.
if mode_name not in modes.non_checks_options():
config.load_all_checks(check_api.get_check_api_context)
# Read the configuration files (main.mk, autochecks, etc.), but not for
# certain operation modes that does not need them and should not be harmed
# by a broken configuration
if mode_name not in modes.non_config_options():
config.load()
done, exit_status = False, 0
if mode_name is not None and mode_args is not None:
exit_status = modes.call(mode_name, mode_args, opts, args)
done = True
# When no mode was found, Check_MK is running the "check" mode
if not done:
if (args and len(args) <= 2) or "--keepalive" in [o[0] for o in opts]:
exit_status = modes.call("--check", None, opts, args)
else:
modes.get("help").handler_function()
exit_status = 0
sys.exit(exit_status)
except MKTerminate as e:
console.output("<Interrupted>\n", stream=sys.stderr)
sys.exit(1)
except (MKGeneralException, MKBailOut) as e:
sys.stderr.write("%s\n" % e)
if cmk.utils.debug.enabled():
raise
sys.exit(3)
except Exception:
crash = cmk.base.crash_reporting.CMKBaseCrashReport.from_exception()
cmk.base.crash_reporting.CrashReportStore().save(crash)
raise
finally:
profiling.output_profile()