Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / cmk / utils / paths.py
blobc65e8788d27664d3592443983039ac39bc4b78b7
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2016 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.
26 """This module serves the path structure of the Check_MK environment
27 to all components of Check_MK."""
29 import os
32 # First declare the possible paths for the linters. Then set it within _set_paths()
35 # TODO: Add piggyback_dir and use it in code
36 omd_root = '' # type: str
37 default_config_dir = '' # type: str
38 main_config_file = None
39 final_config_file = None
40 local_config_file = None
41 check_mk_config_dir = None
42 modules_dir = None
43 var_dir = ''
44 log_dir = ''
45 precompiled_checks_dir = None
46 autochecks_dir = None
47 precompiled_hostchecks_dir = None
48 snmpwalks_dir = None
49 counters_dir = None
50 tcp_cache_dir = None
51 data_source_cache_dir = None
52 snmp_scan_cache_dir = None
53 include_cache_dir = None
54 tmp_dir = ''
55 logwatch_dir = None
56 nagios_objects_file = None
57 nagios_command_pipe_path = None
58 check_result_path = None
59 nagios_status_file = None
60 nagios_conf_dir = None
61 nagios_config_file = None
62 nagios_startscript = None
63 nagios_binary = None
64 apache_config_dir = None
65 htpasswd_file = None
66 livestatus_unix_socket = None
67 pnp_rraconf_dir = None
68 livebackendsdir = None
70 share_dir = ''
71 checks_dir = None
72 notifications_dir = None
73 inventory_dir = None
74 check_manpages_dir = None
75 agents_dir = None
76 mibs_dir = None
77 web_dir = None
78 pnp_templates_dir = None
79 doc_dir = None
80 locale_dir = '' # type: str
81 bin_dir = None
82 lib_dir = None
83 mib_dir = None
85 # TODO: Add active_checks_dir and make it used in code
86 local_share_dir = ''
87 local_checks_dir = ''
88 local_notifications_dir = ''
89 local_inventory_dir = ''
90 local_check_manpages_dir = ''
91 local_agents_dir = ''
92 local_mibs_dir = ''
93 local_web_dir = ''
94 local_pnp_templates_dir = ''
95 local_doc_dir = ''
96 local_locale_dir = ''
97 local_bin_dir = ''
98 local_lib_dir = ''
99 local_mib_dir = ''
100 inventory_output_dir = ''
101 inventory_archive_dir = ''
102 status_data_dir = ''
105 def _set_paths():
106 global omd_root
107 omd_root = _omd_root()
109 globals().update({
110 "default_config_dir": os.path.join(omd_root, "etc/check_mk"),
111 "main_config_file": os.path.join(omd_root, "etc/check_mk/main.mk"),
112 "final_config_file": os.path.join(omd_root, "etc/check_mk/final.mk"),
113 "local_config_file": os.path.join(omd_root, "etc/check_mk/local.mk"),
114 "check_mk_config_dir": os.path.join(omd_root, "etc/check_mk/conf.d"),
115 "modules_dir": os.path.join(omd_root, "share/check_mk/modules"),
116 "var_dir": os.path.join(omd_root, "var/check_mk"),
117 "log_dir": os.path.join(omd_root, "var/log"),
118 "precompiled_checks_dir": os.path.join(omd_root, "var/check_mk/precompiled_checks"),
119 "autochecks_dir": os.path.join(omd_root, "var/check_mk/autochecks"),
120 "precompiled_hostchecks_dir": os.path.join(omd_root, "var/check_mk/precompiled"),
121 "snmpwalks_dir": os.path.join(omd_root, "var/check_mk/snmpwalks"),
122 "counters_dir": os.path.join(omd_root, "tmp/check_mk/counters"),
123 "tcp_cache_dir": os.path.join(omd_root, "tmp/check_mk/cache"),
124 "data_source_cache_dir": os.path.join(omd_root, "tmp/check_mk/data_source_cache"),
125 "snmp_scan_cache_dir": os.path.join(omd_root, "tmp/check_mk/snmp_scan_cache"),
126 "include_cache_dir": os.path.join(omd_root, "tmp/check_mk/check_includes"),
127 "tmp_dir": os.path.join(omd_root, "tmp/check_mk"),
128 "logwatch_dir": os.path.join(omd_root, "var/check_mk/logwatch"),
129 "nagios_startscript": os.path.join(omd_root, "etc/init.d/core"),
131 # Switched via symlinks on icinga/nagios change
132 "nagios_conf_dir": os.path.join(omd_root, "etc/nagios/conf.d"),
133 "nagios_objects_file": os.path.join(omd_root, "etc/nagios/conf.d/check_mk_objects.cfg"),
134 "check_result_path": os.path.join(omd_root, "tmp/nagios/checkresults"),
135 "nagios_status_file": os.path.join(omd_root, "tmp/nagios/status.dat"),
136 "apache_config_dir": os.path.join(omd_root, "etc/apache"),
137 "htpasswd_file": os.path.join(omd_root, "etc/htpasswd"),
138 "livestatus_unix_socket": os.path.join(omd_root, "tmp/run/live"),
139 "pnp_rraconf_dir": os.path.join(omd_root, "share/check_mk/pnp-rraconf"),
140 "livebackendsdir": os.path.join(omd_root, "share/check_mk/livestatus"),
141 "inventory_output_dir": os.path.join(omd_root, "var/check_mk/inventory"),
142 "inventory_archive_dir": os.path.join(omd_root, "var/check_mk/inventory_archive"),
143 "status_data_dir": os.path.join(omd_root, "tmp/check_mk/status_data"),
146 _set_core_specific_paths()
147 _set_overridable_paths()
148 _set_overridable_paths(local=True)
151 def _omd_root():
152 return os.environ.get("OMD_ROOT", "")
155 def _set_core_specific_paths():
156 core = _get_core_name()
158 if core == "icinga":
159 globals().update({
160 "nagios_binary": os.path.join(omd_root, "bin/icinga"),
161 "nagios_config_file": os.path.join(omd_root, "tmp/icinga/icinga.cfg"),
162 "nagios_command_pipe_path": os.path.join(omd_root, "tmp/run/icinga.cmd"),
164 else:
165 globals().update({
166 "nagios_binary": os.path.join(omd_root, "bin/nagios"),
167 "nagios_config_file": os.path.join(omd_root, "tmp/nagios/nagios.cfg"),
168 "nagios_command_pipe_path": os.path.join(omd_root, "tmp/run/nagios.cmd"),
172 # TODO: Find a better way to determine the currently configured core.
173 # For example generalize the etc/check_mk/conf.d/microcore.mk which is written by the CORE
174 # hook -> Change the name to core.mk and write it for all configured cores.
175 def _get_core_name():
176 try:
177 for l in open(os.path.join(omd_root, "etc/omd/site.conf")):
178 if l.startswith("CONFIG_CORE='"):
179 return l.split("'")[1]
180 except IOError as e:
181 # At least in test environment the file is not available. We only added this try/except for
182 # this case. This should better be solved in a cleaner way.
183 if e.errno == 2:
184 pass
185 else:
186 raise
189 def _set_overridable_paths(local=False):
190 rel_base = "local" if local else ""
191 var_prefix = "local_" if local else ""
193 globals().update({
194 var_prefix + "share_dir": os.path.join(omd_root, rel_base, "share/check_mk"),
195 var_prefix + "checks_dir": os.path.join(omd_root, rel_base, "share/check_mk/checks"),
196 var_prefix + "notifications_dir": os.path.join(omd_root, rel_base,
197 "share/check_mk/notifications"),
198 var_prefix + "inventory_dir": os.path.join(omd_root, rel_base, "share/check_mk/inventory"),
199 var_prefix + "check_manpages_dir": os.path.join(omd_root, rel_base,
200 "share/check_mk/checkman"),
201 var_prefix + "agents_dir": os.path.join(omd_root, rel_base, "share/check_mk/agents"),
202 var_prefix + "mibs_dir": os.path.join(omd_root, rel_base, "share/check_mk/mibs"),
203 var_prefix + "web_dir": os.path.join(omd_root, rel_base, "share/check_mk/web"),
204 var_prefix + "pnp_templates_dir": os.path.join(omd_root, rel_base,
205 "share/check_mk/pnp-templates"),
206 var_prefix + "doc_dir": os.path.join(omd_root, rel_base, "share/doc/check_mk"),
207 var_prefix + "locale_dir": os.path.join(omd_root, rel_base, "share/check_mk/locale"),
208 var_prefix + "bin_dir": os.path.join(omd_root, rel_base, "bin"),
209 var_prefix + "lib_dir": os.path.join(omd_root, rel_base, "lib"),
210 var_prefix + "mib_dir": os.path.join(omd_root, rel_base, "share/snmp/mibs"),
214 _set_paths()