Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / bin / check_mk
blobfee831f92c24db7f1cffde147cb3f3935766f44f
1 #!/usr/bin/env 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 # Future convention within all Check_MK modules for variable names:
29 # - host_name - Monitoring name of a host (string)
30 # - node_name - Name of cluster member (string)
31 # - cluster_name - Name of a cluster (string)
32 # - realhost_name - Name of a *real* host, not a cluster (string)
34 import os
35 import sys
36 import getopt
38 # Needs to be placed before cmk modules, because they are not available
39 # when executed as non site user. Alas, this screws up our import ordering
40 # a bit, so we have to tell pylint about that.
41 # pylint: disable=wrong-import-position
42 if not os.environ.get("OMD_SITE"):
43 sys.stderr.write("Check_MK can be used only as site user.\n")
44 sys.exit(1)
46 from cmk.utils.exceptions import MKGeneralException, MKTerminate, MKBailOut
47 import cmk.utils.debug
48 import cmk.utils.log
49 import cmk.utils.paths
51 import cmk_base
52 import cmk_base.console as console
53 import cmk_base.config as config
54 import cmk_base.profiling as profiling
55 from cmk_base.modes import modes
56 import cmk_base.check_api as check_api
58 cmk.utils.log.setup_console_logging()
59 logger = cmk.utils.log.get_logger("base")
61 cmk_base.utils.register_sigint_handler()
63 try:
64 opts, args = getopt.getopt(sys.argv[1:], modes.short_getopt_specs(), modes.long_getopt_specs())
65 except getopt.GetoptError, err:
66 console.error("ERROR: %s\n\n" % err)
67 modes.get("help").handler_function()
68 sys.exit(1)
70 # First load the general modifying options
71 modes.process_general_options(opts)
73 try:
74 # At least in case the config is needed, the checks are needed too, because
75 # the configuration may refer to check config variable names.
76 if len(set.intersection(set(modes.non_checks_options()), [o[0] for o in opts])) == 0:
77 config.load_all_checks(check_api.get_check_api_context)
79 # Read the configuration files (main.mk, autochecks, etc.), but not for
80 # certain operation modes that does not need them and should not be harmed
81 # by a broken configuration
82 if len(set.intersection(set(modes.non_config_options()), [o[0] for o in opts])) == 0:
83 config.load()
85 # Now find the requested mode and execute it
86 done, exit_status = False, 0
87 for o, a in opts:
88 if modes.exists(o):
89 exit_status = modes.call(o, a, opts, args)
90 done = True
91 break
93 # When no mode was found, Check_MK is running the "check" mode
94 if not done:
95 if (args and len(args) <= 2) or "--keepalive" in [o[0] for o in opts]:
96 exit_status = modes.call("--check", None, opts, args)
97 else:
98 modes.get("help").handler_function()
99 exit_status = 0
101 profiling.output_profile()
102 sys.exit(exit_status)
104 except MKTerminate, e:
105 console.output("<Interrupted>\n", stream=sys.stderr)
106 sys.exit(1)
108 except (MKGeneralException, MKBailOut), e:
109 sys.stderr.write("%s\n" % e)
110 if cmk.utils.debug.enabled():
111 raise
112 sys.exit(3)