Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / autochecks.py
blob74ff933ab13671c8f626f4f1234d80a90d0e41c5
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 os
28 import sys
30 import six
32 import cmk.utils.debug
33 import cmk.utils.exceptions
34 import cmk.utils.paths
36 import cmk_base.config
37 import cmk_base.console
40 # Read automatically discovered checks of one host.
41 # Returns a table with three columns:
42 # 1. check_plugin_name
43 # 2. item
44 # 3. parameters evaluated!
45 # TODO: use store.load_data_from_file()
46 # TODO: Common code with parse_autochecks_file? Cleanup.
47 def read_autochecks_of(hostname):
48 basedir = cmk.utils.paths.autochecks_dir
49 filepath = basedir + '/' + hostname + '.mk'
51 if not os.path.exists(filepath):
52 return []
54 check_config = cmk_base.config.get_check_variables()
55 try:
56 cmk_base.console.vverbose("Loading autochecks from %s\n", filepath)
57 autochecks_raw = eval(file(filepath).read(), check_config, check_config)
58 except SyntaxError as e:
59 cmk_base.console.verbose("Syntax error in file %s: %s\n", filepath, e, stream=sys.stderr)
60 if cmk.utils.debug.enabled():
61 raise
62 return []
63 except Exception as e:
64 cmk_base.console.verbose("Error in file %s:\n%s\n", filepath, e, stream=sys.stderr)
65 if cmk.utils.debug.enabled():
66 raise
67 return []
69 # Exchange inventorized check parameters with those configured by
70 # the user. Also merge with default levels for modern dictionary based checks.
71 autochecks = []
72 for entry in autochecks_raw:
73 if len(entry) == 4: # old format where hostname is at the first place
74 entry = entry[1:]
75 check_plugin_name, item, parameters = entry
77 # With Check_MK 1.2.7i3 items are now defined to be unicode strings. Convert
78 # items from existing autocheck files for compatibility. TODO remove this one day
79 if isinstance(item, str):
80 item = cmk_base.config.decode_incoming_string(item)
82 if not isinstance(check_plugin_name, six.string_types):
83 raise cmk.utils.exceptions.MKGeneralException(
84 "Invalid entry '%r' in check table of host '%s': "
85 "The check type must be a string." % (entry, hostname))
87 autochecks.append((check_plugin_name, item,
88 cmk_base.config.compute_check_parameters(hostname, check_plugin_name,
89 item, parameters)))
90 return autochecks