Module: check_mk
Branch: master
Commit: 201c1248a7a9ca9a175ec340e5418d4691a22d7c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=201c1248a7a9ca…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Mar 3 10:45:08 2016 +0100
#3263 Notifications: allow users to restrict by their contact groups
In notification rules in the {{Contact Selection}} you can now restrict
users by their membership in contact groups. Note: this is not the same
as the <b>Selection</b> by contact groups - which already existed. This
feature is used to later reduce that selected contacts.
---
.werks/3263 | 13 +++++++++++++
ChangeLog | 1 +
modules/notify.py | 51 ++++++++++++++++++++++++++++++++++++++-------------
web/htdocs/wato.py | 18 ++++++++++++++++--
4 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/.werks/3263 b/.werks/3263
new file mode 100644
index 0000000..c0d012b
--- /dev/null
+++ b/.werks/3263
@@ -0,0 +1,13 @@
+Title: Notifications: allow users to restrict by their contact groups
+Level: 1
+Component: notifications
+Class: feature
+Compatible: compat
+State: unknown
+Version: 1.2.9i1
+Date: 1456998194
+
+In notification rules in the {{Contact Selection}} you can now restrict
+users by their membership in contact groups. Note: this is not the same
+as the <b>Selection</b> by contact groups - which already existed. This
+feature is used to later reduce that selected contacts.
diff --git a/ChangeLog b/ChangeLog
index 867e5bd..f6c36ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -85,6 +85,7 @@
* 3000 FIX: Host check command: "Use a custom check plugin" was not working
with CMC...
Notifications:
+ * 3263 Notifications: allow users to restrict by their contact groups...
* 3253 FIX: sms: notification script sms now handles single quotes in the message in
the right way
BI:
diff --git a/modules/notify.py b/modules/notify.py
index 995423c..663b720 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -683,23 +683,48 @@ def rbn_rule_contacts(rule, context):
notify_log(" - skipping contact %s: he/she has disabled
notifications" % contactname)
continue
- if "contact_match_macros" in rule:
- match = True
- for macro_name, regexp in rule["contact_match_macros"]:
- value = contact.get("_" + macro_name, "")
- if not regexp.endswith("$"):
- regexp = regexp + "$"
- if not regex(regexp).match(value):
- notify_log(" - skipping contact %s: value '%s' for
macro '%s' does not match '%s'" % (
- contactname, value, macro_name, regexp))
- match = False
- break
- if not match:
- continue
+ reason = rbn_match_contact_macros(rule, contactname, contact) or \
+ rbn_match_contact_groups(rule, contactname, contact)
+
+ if reason:
+ notify_log(" - skipping contact %s: %s" % (contactname,
reason))
+ continue
+
+ else:
+ notify_log("Warning: cannot get information about contact %s: ignoring
restrictions" % contactname)
+
all_enabled.append(contactname)
return all_enabled
+
+def rbn_match_contact_macros(rule, contactname, contact):
+ if "contact_match_macros" in rule:
+ for macro_name, regexp in rule["contact_match_macros"]:
+ value = contact.get("_" + macro_name, "")
+ if not regexp.endswith("$"):
+ regexp = regexp + "$"
+ if not regex(regexp).match(value):
+ macro_overview = ", ".join([
+ "%s=%s" % (varname[1:], val)
+ for (varname, val)
+ in contact.items()
+ if varname.startswith("_")])
+ return "value '%s' for macro '%s' does not match
'%s'. His macros are: %s" % (
+ value, macro_name, regexp, macro_overview)
+
+
+def rbn_match_contact_groups(rule, contactname, contact):
+ if "contact_match_groups" in rule:
+ if "contactgroups" not in contact:
+ notify_log("Warning: cannot determine contact groups of %s: skipping
restrictions" % contactname)
+ return
+ for required_group in rule["contact_match_groups"]:
+ if required_group not in contact["contactgroups"]:
+ return "he/she is not member of the contact group %s (his groups are
%s)" % (
+ required_group, ", ".join(contact["contactgroups"]
or ["<None>"]))
+
+
def rbn_match_notification_comment(rule, context):
if "match_notification_comment" in rule:
r = regex(rule["match_notification_comment"])
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index e3b3e9a..4117126 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -6907,7 +6907,8 @@ def vs_notification_rule(userid = None):
else:
contact_headers = [
( _("Contact Selection"), [ "contact_all",
"contact_all_with_email", "contact_object",
- "contact_users",
"contact_groups", "contact_emails", "contact_match_macros" ]
),
+ "contact_users",
"contact_groups", "contact_emails", "contact_match_macros",
+ "contact_match_groups", ] ),
]
section_contacts = [
# Contact selection
@@ -6979,6 +6980,18 @@ def vs_notification_rule(userid = None):
add_label = _("Add condition"),
)
),
+ ( "contact_match_groups",
+ ListOf(
+ GroupSelection("contact"),
+ title = _("Restrict by contact groups"),
+ help = _("Here you can <i>restrict</i> the list of
contacts that has been "
+ "built up by the previous options to those that are
members of "
+ "selected contact groups. If you select more than one
contact group here then "
+ "the user must be member of <i>all</i> these
groups."),
+ add_label = _("Add Group"),
+ movable = False,
+ )
+ ),
]
section_override = [
( "allow_disable",
@@ -7082,7 +7095,8 @@ def vs_notification_rule(userid = None):
"match_plugin_output",
"match_timeperiod", "match_escalation",
"match_escalation_throttle",
"match_sl", "match_host_event",
"match_service_event", "match_ec",
"match_notification_comment",
- "match_checktype", "bulk",
"contact_users", "contact_groups", "contact_emails",
"contact_match_macros" ],
+ "match_checktype", "bulk",
"contact_users", "contact_groups", "contact_emails",
+ "contact_match_macros",
"contact_match_groups" ],
headers = [
( _("General Properties"), [ "description",
"comment", "disabled", "docu_url", "allow_disable"
] ),
( _("Notification Method"), [ "notify_plugin",
"notify_method", "bulk" ] ),]