Module: check_mk
Branch: master
Commit: 480aa77377c3df75bd0dd0e8e617fa10e3876e67
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=480aa77377c3df…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Jul 19 11:38:28 2016 +0200
Rework helper function of notifications, support alert handlers
---
modules/events.py | 35 +++++++++++++++++++++++++++++++++++
modules/notify.py | 40 +---------------------------------------
web/htdocs/valuespec.py | 1 +
web/htdocs/wato.py | 16 ++++++++++++----
4 files changed, 49 insertions(+), 43 deletions(-)
diff --git a/modules/events.py b/modules/events.py
index a659703..b220ea6 100644
--- a/modules/events.py
+++ b/modules/events.py
@@ -565,3 +565,38 @@ def remove_context_from_environment(plugin_context, prefix):
for key in plugin_context:
os.unsetenv(prefix + key)
+
+# recursively turns a python object (with lists, dictionaries and pods) containing
parameters
+# into a flat contextlist for use as environment variables in plugins
+#
+# this: { "LVL1": [{"VALUE": 42}, {"VALUE": 13}] }
+# would be added as:
+# PARAMETER_LVL1_1_VALUE = 42
+# PARAMETER_LVL1_2_VALUE = 13
+def add_to_event_context(plugin_context, prefix, param):
+ if isinstance(param, list):
+ plugin_context[prefix + "S"] = " ".join(param)
+ for nr, value in enumerate(param):
+ add_to_event_context(plugin_context, "%s_%d" % (prefix, nr + 1),
value)
+ elif isinstance(param, dict):
+ for key, value in param.items():
+ add_to_event_context(plugin_context, "%s_%s" % (prefix,
key.upper()), value)
+ else:
+ plugin_context[prefix] = plugin_param_to_string(param)
+
+
+def plugin_param_to_string(value):
+ if type(value) in ( str, unicode ):
+ return value
+ elif type(value) in ( int, float ):
+ return str(value)
+ elif value == None:
+ return ""
+ elif value == True:
+ return "yes"
+ elif value == False:
+ return ""
+ elif type(value) in ( tuple, list ):
+ return "\t".join(value)
+ else:
+ return repr(value) # Should never happen
diff --git a/modules/notify.py b/modules/notify.py
index 4107db0..43bab3f 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -1082,25 +1082,6 @@ def notify_via_email(plugin_context):
# 2: Cannot send, retry does not make sense
-# recursively turns a python object (with lists, dictionaries and pods) containing
parameters
-# into a flat contextlist for use as environment variables in plugins
-#
-# this: { "LVL1": [{"VALUE": 42}, {"VALUE": 13}] }
-# would be added as:
-# PARAMETER_LVL1_1_VALUE = 42
-# PARAMETER_LVL1_2_VALUE = 13
-def add_to_context(plugin_context, prefix, param):
- if isinstance(param, list):
- plugin_context[prefix + "S"] = " ".join(param)
- for nr, value in enumerate(param):
- add_to_context(plugin_context, "%s_%d" % (prefix, nr + 1), value)
- elif isinstance(param, dict):
- for key, value in param.items():
- add_to_context(plugin_context, "%s_%s" % (prefix, key.upper()),
value)
- else:
- plugin_context[prefix] = plugin_param_to_string(param)
-
-
# Add the plugin parameters to the envinroment. We have two types of parameters:
# - list, the legacy style. This will lead to PARAMETERS_1, ...
# - dict, the new style for scripts with WATO rule. This will lead to
@@ -1108,9 +1089,7 @@ def add_to_context(plugin_context, prefix, param):
def create_plugin_context(raw_context, params):
plugin_context = {}
plugin_context.update(raw_context) # Make a real copy
-
- add_to_context(plugin_context, "PARAMETER", params)
-
+ add_to_event_context(plugin_context, "PARAMETER", params)
return plugin_context
@@ -1120,23 +1099,6 @@ def create_bulk_parameter_context(params):
for (varname, value) in dict_context.items() ]
-def plugin_param_to_string(value):
- if type(value) in ( str, unicode ):
- return value
- elif type(value) in ( int, float ):
- return str(value)
- elif value == None:
- return ""
- elif value == True:
- return "yes"
- elif value == False:
- return ""
- elif type(value) in ( tuple, list ):
- return "\t".join(value)
- else:
- return repr(value) # Should never happen
-
-
def path_to_notification_script(plugin):
# Call actual script without any arguments
if local_notifications_dir:
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 2b44708..ee52e40 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -1837,6 +1837,7 @@ class OptionalDropdownChoice(DropdownChoice):
input_value = value
else:
input_value = self._explicit.default_value()
+ html.help(self._explicit.help())
self._explicit.render_input(varprefix + "_ex", input_value)
html.write("</span>")
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 8418191..154f428 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -13473,6 +13473,7 @@ def register_check_parameters(subgroup, checkgroup, title,
valuespec, itemspec,
match = "all",
deprecated = deprecated)
+
# Registers notification parameters for a certain notification script,
# e.g. "mail" or "sms". This will create:
# - A WATO host rule
@@ -13481,21 +13482,28 @@ def register_check_parameters(subgroup, checkgroup, title,
valuespec, itemspec,
# The match type will be set to "dict".
g_notification_parameters = {}
def register_notification_parameters(scriptname, valuespec):
+ register_user_script_parameters(
+ g_notification_parameters,
+ "notification_parameters",
+ "monconf/" + _("Notifications"),
+ scriptname,
+ valuespec)
+
+def register_user_script_parameters(ruleset_dict, ruleset_dict_name, ruleset_group,
scriptname, valuespec):
script_title = notification_script_title(scriptname)
title = _("Parameters for %s") % script_title
valuespec._title = _("Call with the following parameters:")
register_rule(
- "monconf/" + _("Notifications"),
- "notification_parameters:" + scriptname,
+ ruleset_group,
+ ruleset_dict_name + ":" + scriptname,
valuespec,
title,
itemtype = None,
match = "dict"
)
-
- g_notification_parameters[scriptname] = valuespec
+ ruleset_dict[scriptname] = valuespec