2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
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--------------------------------------------------.
44 # | | _ \ __ _| |_ __ _ ___ _ __ ___| _ \ _ __ ___ __ _ |
45 # | | | | |/ _` | __/ _` / __| '__/ __| |_) | '__/ _ \ / _` | |
46 # | | |_| | (_| | || (_| \__ \ | | (__| __/| | | (_) | (_| |_ |
47 # | |____/ \__,_|\__\__,_|___/_| \___|_| |_| \___/ \__, (_) |
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
):
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
))
70 if config
.monitoring_core
== "cmc":
71 p
= subprocess
.Popen( # nosec
74 stdin
=subprocess
.PIPE
if command_stdin
else open(os
.devnull
),
75 stdout
=subprocess
.PIPE
,
76 stderr
=subprocess
.PIPE
,
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
86 stdin
=subprocess
.PIPE
if command_stdin
else open(os
.devnull
),
87 stdout
=subprocess
.PIPE
,
88 stderr
=subprocess
.PIPE
,
90 stdout
, stderr
= p
.communicate(input=command_stdin
)
91 exitstatus
= p
.returncode
93 # On timeout exception try to stop the process to prevent child process "leakage"
95 os
.killpg(os
.getpgid(p
.pid
), signal
.SIGTERM
)
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..
107 if exitstatus
== 127:
108 raise MKAgentError("Program '%s' not found (exit code 127)" % exepath
)
110 raise MKAgentError("Agent exited with code %d: %s" % (exitstatus
, stderr
))
114 def _get_command_line_and_stdin(self
):
115 """Returns the final command line to be executed"""
116 raise NotImplementedError()
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
]
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
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
)
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
175 return "special_%s" % self
._special
_agent
_id
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
196 cmd_arguments
= agent_configuration
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
208 path
= special_agents_dir
+ "/agent_" + self
._special
_agent
_id
210 return path
+ " " + final_arguments
, command_stdin