GUI CSS: Removed snapin styles from py modules and added a _snapins.scss for the...
[check_mk.git] / cmk_base / data_sources / programs.py
blob2bc1615a5c444c97e667eb591614f3bfc107699e
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 import os
28 import signal
29 import subprocess
30 import collections
32 import cmk.utils.paths
33 from cmk.utils.exceptions import MKTimeout
35 import cmk_base.config as config
36 import cmk_base.core_config as core_config
37 from cmk_base.exceptions import MKAgentError
39 from .abstract import CheckMKAgentDataSource
42 # .--Datasoure Programs--------------------------------------------------.
43 # | ____ _ ____ |
44 # | | _ \ __ _| |_ __ _ ___ _ __ ___| _ \ _ __ ___ __ _ |
45 # | | | | |/ _` | __/ _` / __| '__/ __| |_) | '__/ _ \ / _` | |
46 # | | |_| | (_| | || (_| \__ \ | | (__| __/| | | (_) | (_| |_ |
47 # | |____/ \__,_|\__\__,_|___/_| \___|_| |_| \___/ \__, (_) |
48 # | |___/ |
49 # +----------------------------------------------------------------------+
50 # | Fetching agent data from program calls instead of an agent |
51 # '----------------------------------------------------------------------'
54 class ProgramDataSource(CheckMKAgentDataSource):
55 """Abstract base class for all data source classes that execute external programs"""
57 def _cpu_tracking_id(self):
58 return "ds"
60 def _execute(self):
61 command_line, command_stdin = self._get_command_line_and_stdin()
62 return self._get_agent_info_program(command_line, command_stdin)
64 def _get_agent_info_program(self, commandline, command_stdin):
65 exepath = commandline.split()[0] # for error message, hide options!
67 self._logger.debug("Calling external program %r" % (commandline))
68 p = None
69 try:
70 if config.monitoring_core == "cmc":
71 p = subprocess.Popen( # nosec
72 commandline,
73 shell=True,
74 stdin=subprocess.PIPE if command_stdin else open(os.devnull),
75 stdout=subprocess.PIPE,
76 stderr=subprocess.PIPE,
77 preexec_fn=os.setsid,
78 close_fds=True)
79 else:
80 # We can not create a separate process group when running Nagios
81 # Upon reaching the service_check_timeout Nagios only kills the process
82 # group of the active check.
83 p = subprocess.Popen( # nosec
84 commandline,
85 shell=True,
86 stdin=subprocess.PIPE if command_stdin else open(os.devnull),
87 stdout=subprocess.PIPE,
88 stderr=subprocess.PIPE,
89 close_fds=True)
90 stdout, stderr = p.communicate(input=command_stdin)
91 exitstatus = p.returncode
92 except MKTimeout:
93 # On timeout exception try to stop the process to prevent child process "leakage"
94 if p:
95 os.killpg(os.getpgid(p.pid), signal.SIGTERM)
96 p.wait()
97 raise
98 finally:
99 # The stdout and stderr pipe are not closed correctly on a MKTimeout
100 # Normally these pipes getting closed after p.communicate finishes
101 # Closing them a second time in a OK scenario won't hurt neither..
102 if p:
103 p.stdout.close()
104 p.stderr.close()
106 if exitstatus:
107 if exitstatus == 127:
108 raise MKAgentError("Program '%s' not found (exit code 127)" % exepath)
109 else:
110 raise MKAgentError("Agent exited with code %d: %s" % (exitstatus, stderr))
112 return stdout
114 def _get_command_line_and_stdin(self):
115 """Returns the final command line to be executed"""
116 raise NotImplementedError()
118 def describe(self):
119 """Return a short textual description of the agent"""
120 command_line, command_stdin = self._get_command_line_and_stdin()
121 response = ["Program: %s" % command_line]
122 if command_stdin:
123 response.extend([" Program stdin:", command_stdin])
124 return "\n".join(response)
127 class DSProgramDataSource(ProgramDataSource):
128 def __init__(self, hostname, ipaddress, command_template):
129 super(DSProgramDataSource, self).__init__(hostname, ipaddress)
130 self._command_template = command_template
132 def id(self):
133 return "agent"
135 def name(self):
136 """Return a unique (per host) textual identification of the data source"""
137 command_line, _command_stdin = self._get_command_line_and_stdin()
138 program = command_line.split(" ")[0]
139 return os.path.basename(program)
141 def _get_command_line_and_stdin(self):
142 cmd = self._command_template
144 cmd = self._translate_legacy_macros(cmd)
145 cmd = self._translate_host_macros(cmd)
147 return cmd, None
149 def _translate_legacy_macros(self, cmd):
150 # Make "legacy" translation. The users should use the $...$ macros in future
151 return cmd.replace("<IP>", self._ipaddress or "").replace("<HOST>", self._hostname)
153 def _translate_host_macros(self, cmd):
154 tags = self._config_cache.tags_of_host(self._hostname)
155 attrs = core_config.get_host_attributes(self._hostname, tags)
156 if config.is_cluster(self._hostname):
157 parents_list = core_config.get_cluster_nodes_for_config(self._hostname)
158 attrs.setdefault("alias", "cluster of %s" % ", ".join(parents_list))
159 attrs.update(core_config.get_cluster_attributes(self._hostname, parents_list))
161 macros = core_config.get_host_macros_from_attributes(self._hostname, attrs)
162 return core_config.replace_macros(cmd, macros)
165 SpecialAgentConfiguration = collections.namedtuple("SpecialAgentConfiguration", ["args", "stdin"])
168 class SpecialAgentDataSource(ProgramDataSource):
169 def __init__(self, hostname, ipaddress, special_agent_id, params):
170 self._special_agent_id = special_agent_id
171 super(SpecialAgentDataSource, self).__init__(hostname, ipaddress)
172 self._params = params
174 def id(self):
175 return "special_%s" % self._special_agent_id
177 @property
178 def special_agent_plugin_file_name(self):
179 return "agent_%s" % self._special_agent_id
181 def _get_individual_exit_code_spec(self, exit_code_spec):
182 return exit_code_spec["individual"]["special"]
184 # TODO: Can't we make this more specific in case of special agents?
185 def _gather_check_plugin_names(self):
186 return config.discoverable_tcp_checks()
188 def _get_command_line_and_stdin(self):
189 """Create command line using the special_agent_info"""
190 info_func = config.special_agent_info[self._special_agent_id]
191 agent_configuration = info_func(self._params, self._hostname, self._ipaddress)
192 if isinstance(agent_configuration, SpecialAgentConfiguration):
193 cmd_arguments = agent_configuration.args
194 command_stdin = agent_configuration.stdin
195 else:
196 cmd_arguments = agent_configuration
197 command_stdin = None
199 final_arguments = config.prepare_check_command(
200 cmd_arguments, self._hostname, description=None)
202 special_agents_dir = cmk.utils.paths.agents_dir + "/special"
203 local_special_agents_dir = cmk.utils.paths.local_agents_dir + "/special"
205 if os.path.exists(local_special_agents_dir + "/agent_" + self._special_agent_id):
206 path = local_special_agents_dir + "/agent_" + self._special_agent_id
207 else:
208 path = special_agents_dir + "/agent_" + self._special_agent_id
210 return path + " " + final_arguments, command_stdin