Module: check_mk
Branch: master
Commit: 97a42faf08f78100aeac586ba331f2e1b0c0c39e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=97a42faf08f781…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Feb 27 12:14:21 2014 +0100
RBN: User specific notifications work
---
modules/notify.py | 34 +++++++++++++++++++++++++++++-----
web/htdocs/wato.py | 39 +++++++++++++++++++++++++--------------
2 files changed, 54 insertions(+), 19 deletions(-)
diff --git a/modules/notify.py b/modules/notify.py
index fe6d753..ec8c329 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -746,8 +746,12 @@ def notify_rulebased(context):
notifications = {}
num_rule_matches = 0
- for rule in notification_rules:
- notify_log("Trying rule '%s'..." %
rule["description"])
+ for rule in notification_rules + user_notification_rules():
+ if "contact" in rule:
+ notify_log("User %s's rule '%s'..." %
(rule["contact"], rule["description"]))
+ else:
+ notify_log("Global rule '%s'..." %
rule["description"])
+
why_not = rbn_match_rule(rule, context) # also checks disabling
if why_not:
notify_log(" -> does not match: %s" % why_not)
@@ -761,9 +765,12 @@ def notify_rulebased(context):
for contact in contacts:
key = contact, plugin
if key in notifications:
- notify_log(" - cancelling notification of %s via %s"
% key)
- del notifications[key]
- # TODO: honor locked flag for user specific rules
+ locked, method = notifications[key]
+ if locked and "contact" in rule:
+ notify_log(" - cannot cancel notification of %s via
%s: it is locked" % key)
+ else:
+ notify_log(" - cancelling notification of %s via
%s" % key)
+ del notifications[key]
else:
for contact in contacts:
key = contact, plugin
@@ -802,6 +809,23 @@ def notify_rulebased(context):
notify_log(fe)
+# Create a table of all user specific notification rules
+def user_notification_rules():
+ user_rules = []
+ for contactname, contact in contacts.iteritems():
+ notify_log("Hat %s was?" % contactname)
+ for rule in contact.get("notification_rules", []):
+ # Save the owner of the rule for later debugging
+ rule["contact"] = contactname
+ # We assume that the "contact_..." entries in the
+ # rule are allowed and only contain one entry of the
+ # type "contact_contacts" : [ contactname ]. This
+ # is handled by WATO. Contact specific rules are a
+ # WATO-only feature anyway...
+ user_rules.append(rule)
+ notify_log("Found %d user specific rules" % len(user_rules))
+ return user_rules
+
def rbn_fake_email_contact(email):
return {
"name" : email.split("@")[0],
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 20eb882..2844b0b 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -7365,6 +7365,7 @@ def vs_notification_rule(userid = None):
if userid:
contact_headers = []
section_contacts = []
+ section_override = []
else:
contact_headers = [
( _("Contact Selection"), [ "contact_all",
"contact_object", "contact_contacts", "contact_groups",
"contact_emails" ] ),
@@ -7408,6 +7409,17 @@ def vs_notification_rule(userid = None):
)
),
]
+ section_override = [
+ ( "allow_disable",
+ Checkbox(
+ title = _("Overriding by users"),
+ help = _("If you uncheck this option then users are not allowed to
deactive notifications "
+ "that are created by this rule."),
+ label = _("allow users to deactivate this notification"),
+ default_value = True,
+ )
+ ),
+ ]
return Dictionary(
title = _("Rule Properties"),
@@ -7428,15 +7440,9 @@ def vs_notification_rule(userid = None):
label = _("do not apply this rule"),
)
),
- ( "allow_disable",
- Checkbox(
- title = _("Overriding by users"),
- help = _("If you uncheck this option then users are not allowed to
deactive notifications "
- "that are created by this rule."),
- label = _("allow users to deactivate this notification"),
- default_value = True,
- )
- ),
+ ] + section_override +
+ [
+
# Matching
( "match_hosttags",
HostTagCondition(
@@ -7832,17 +7838,22 @@ def mode_user_notifications(phase):
if phase == "title":
return _("Custom notification table for user ") + userid
- elif phase == "buttons":
+ users = userdb.load_users(lock = phase == 'action')
+ user = users[userid]
+ is_contact = not not user.get("contactgroups")
+ if phase == "buttons":
html.context_button(_("All Users"), make_link([("mode",
"users")]), "back")
html.context_button(_("User Properties"), make_link([("mode",
"edit_user"), ("edit", userid)]), "edit")
- html.context_button(_("New Rule"), make_link([("mode",
"notification_rule"), ("user", userid)]), "new")
+ if is_contact:
+ html.context_button(_("New Rule"), make_link([("mode",
"notification_rule"), ("user", userid)]), "new")
return
elif phase == "action":
return
- users = userdb.load_users(lock = phase == 'action')
- user = users[userid]
+ if not is_contact:
+ html.show_warning(_("This user is not member of any contact group and thus
cannot have notification rules!"))
+ return
rules = user.get("notification_rules", [])
render_notification_rules(rules, userid)
@@ -10146,7 +10157,7 @@ def mode_users(phase):
html.icon_button(delete_url, _("Delete"), "delete")
notifications_url = make_link([("mode",
"user_notifications"), ("user", id)])
- if
load_configuration_settings().get("enable_rulebased_notifications"):
+ if load_configuration_settings().get("enable_rulebased_notifications")
and user.get("contactgroups"):
html.icon_button(notifications_url, _("Custom notification table of this
user"), "notifications")
# ID