Module: check_mk
Branch: master
Commit: 15315aed635b1a56f314e9da0ce249dc15f2b4b6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=15315aed635b1a…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Jul 25 12:50:54 2016 +0200
3721 Host macros are now replaced in datasource program command lines
You can now use all host macros including custom variables in the command line of
datasource programs, just like for active checks.
---
.werks/3721 | 10 +++++++
ChangeLog | 1 +
modules/check_mk.py | 44 ++++++++++++++++++++++++++-----
web/htdocs/wato.py | 15 +++++++++++
web/plugins/wato/active_checks.py | 17 +++---------
web/plugins/wato/datasource_programs.py | 3 +--
6 files changed, 69 insertions(+), 21 deletions(-)
diff --git a/.werks/3721 b/.werks/3721
new file mode 100644
index 0000000..f2b2d83
--- /dev/null
+++ b/.werks/3721
@@ -0,0 +1,10 @@
+Title: Host macros are now replaced in datasource program command lines
+Level: 1
+Component: core
+Compatible: compat
+Version: 1.4.0i1
+Date: 1469443786
+Class: feature
+
+You can now use all host macros including custom variables in the command line of
+datasource programs, just like for active checks.
diff --git a/ChangeLog b/ChangeLog
index 9284fb8..e0025af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,7 @@
* 3531 Reduced Check_MK helper size for certain dists/python versions...
* 3580 The macro $HOSTNAME$ is now available for host checks which are based on
service states
* 3155 implemented dual monitoring for systems with management boards (like HP iLO)
+ * 3721 Host macros are now replaced in datasource program command lines...
* 3193 FIX: Fixed fake check results for hosts...
* 3214 FIX: Removing SNMP checks for non SNMP hosts and agent based checks for non
agent hosts...
* 3220 FIX: Check_MK HW/SW Inventory is now always disabled for "No Agent"
hosts
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 3261a82..7bc9c7c 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -2142,18 +2142,31 @@ def get_datasource_program(hostname, ipaddress):
path = local_special_agents_dir + "/agent_" + agentname
else:
path = special_agents_dir + "/agent_" + agentname
- return replace_datasource_program_macros(path + " " +
cmd_arguments)
+ return replace_datasource_program_macros(hostname, ipaddress,
+ path + " " +
cmd_arguments)
programs = host_extra_conf(hostname, datasource_programs)
if not programs:
return None
else:
- return replace_datasource_program_macros(
- programs[0].replace("<IP>",
ipaddress).replace("<HOST>", hostname))
+ return replace_datasource_program_macros(hostname, ipaddress, programs[0])
-def replace_datasource_program_macros(cmd):
- return cmd
+def replace_datasource_program_macros(hostname, ipaddress, cmd):
+ # Make "legacy" translation. The users should use the $...$ macros in
future
+ cmd = cmd.replace("<IP>",
ipaddress).replace("<HOST>", hostname)
+
+ is_clust = is_cluster(hostname)
+
+ tags = tags_of_host(hostname)
+ attrs = get_host_attributes(hostname, tags)
+ if is_cluster(hostname):
+ parents_list = get_cluster_nodes_for_config(hostname)
+ attrs.setdefault("alias", "cluster of %s" % ",
".join(parents_list))
+ attrs.update(get_cluster_attributes(hostname, parents_list))
+
+ macros = get_host_macros_from_attributes(hostname, attrs)
+ return replace_macros(cmd, macros)
# Variables needed during the renaming of hosts (see automation.py)
@@ -3393,7 +3406,7 @@ def get_cluster_nodes_for_config(hostname):
return nodes
-def get_basic_host_macros_from_attributes(hostname, attrs):
+def get_host_macros_from_attributes(hostname, attrs):
macros = {
"$HOSTNAME$" : hostname,
"$HOSTADDRESS$" : attrs['address'],
@@ -3517,6 +3530,25 @@ def fallback_ip_for(hostname, family=None):
return "::"
+def replace_macros(s, macros):
+ for key, value in macros.items():
+ if type(value) in (int, long, float):
+ value = str(value) # e.g. in _EC_SL (service level)
+
+ # TODO: Clean this up
+ try:
+ s = s.replace(key, value)
+ except: # Might have failed due to binary UTF-8 encoding in value
+ try:
+ s = s.replace(key, value.decode("utf-8"))
+ except:
+ # If this does not help, do not replace
+ if opt_debug:
+ raise
+
+ return s
+
+
#.
# .--Main Functions------------------------------------------------------.
# | __ __ _ _____ _ _ |
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 95185c4..2095ba9 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -16511,6 +16511,21 @@ def some_host_hasnt_set(folder, attrname):
return False
+def monitoring_macro_help():
+ return " " + _("You can use monitoring macros here. The most important
are: "
+ "<ul>"
+ "<li><tt>$HOSTADDRESS$</tt>: The IP address of
the host</li>"
+ "<li><tt>$HOSTNAME$</tt>: The name of the
host</li>"
+ "<li><tt>$USER1$</tt>: user macro 1 (usually
path to shipped plugins)</li>"
+ "<li><tt>$USER2$</tt>: user marco 2 (usually
path to your own plugins)</li>"
+ "<li><tt>$_HOSTTAGS$</tt>: List of host
tags</li>"
+ "<li><tt>$_HOSTADDRESS_4$</tt>: The IPv4
address of the host</li>"
+ "<li><tt>$_HOSTADDRESS_6$</tt>: The IPv6
address of the host</li>"
+ "<li><tt>$_HOSTADDRESS_FAMILY$</tt>: The
primary address family of the host</li>"
+ "</ul>"
+ "All custom variables defined for the host are available as
<tt>$_HOST[VARNAME]$</tt>.")
+
+
#.
# .--Plugins-------------------------------------------------------------.
# | ____ _ _ |
diff --git a/web/plugins/wato/active_checks.py b/web/plugins/wato/active_checks.py
index 8de8bfc..8abc66e 100644
--- a/web/plugins/wato/active_checks.py
+++ b/web/plugins/wato/active_checks.py
@@ -1421,19 +1421,10 @@ register_rule(group,
def PluginCommandLine(addhelp = ""):
return TextAscii(
title = _("Command line"),
- help = _("Please enter the complete shell command including "
- "path name and arguments to execute. You can use monitoring
"
- "macros here. The most important are:<ul>"
- "<li><tt>$HOSTADDRESS$</tt>: The IP address of
the host</li>"
- "<li><tt>$HOSTNAME$</tt>: The name of the
host</li>"
- "<li><tt>$USER1$</tt>: user macro 1 (usually
path to shipped plugins)</li>"
- "<li><tt>$USER2$</tt>: user marco 2 (usually
path to your own plugins)</li>"
- "</ul>"
- "If you are using OMD, you can omit the path and just specify
"
- "the command (e.g. <tt>check_foobar</tt>). This
command will be "
- "searched first in the local plugins directory "
- "(<tt>~/local/lib/nagios/plugins</tt>) and then in
the shipped plugins "
- "directory (<tt>~/lib/nagios/plugins</tt>) within
your site directory."),
+ help = _("Please enter the complete shell command including path name and
arguments to execute. "
+ "If the plugin you like to execute is located in either
<tt>~/local/lib/nagios/plugins</tt> "
+ "or <tt>~/lib/nagios/plugins</tt> within your site
directory, you can strip the path name and "
+ "just configure the plugin file name as command
<tt>check_foobar</tt>.") + monitoring_macro_help(),
size = "max",
)
diff --git a/web/plugins/wato/datasource_programs.py
b/web/plugins/wato/datasource_programs.py
index 0d55a62..cd6c249 100644
--- a/web/plugins/wato/datasource_programs.py
+++ b/web/plugins/wato/datasource_programs.py
@@ -37,8 +37,7 @@ register_rule(group,
"program that should be called by Check_MK instead of connecting
the agent "
"via TCP. That program must output the agent's data on standard
output in "
"the same format the agent would do. This is for example useful for
monitoring "
- "via SSH. The command line may contain the placeholders
<tt><IP></tt> and "
- "<tt><HOST></tt>."),
+ "via SSH.") + monitoring_macro_help(),
label = _("Command line to execute"),
empty_text = _("Access Check_MK Agent via TCP"),
size = 80,