Module: check_mk
Branch: master
Commit: 114b818e4b0854affe822583533c00accb0b858d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=114b818e4b0854…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Mar 4 12:11:07 2013 +0100
FIX: added checks wether or not a contactgroup can be deleted
---
.bugs/916 | 7 +++-
ChangeLog | 1 +
web/htdocs/wato.py | 76 +++++++++++++++++++++++++++++++++++++++++-----------
3 files changed, 66 insertions(+), 18 deletions(-)
diff --git a/.bugs/916 b/.bugs/916
index 7385abb..0985c70 100644
--- a/.bugs/916
+++ b/.bugs/916
@@ -1,9 +1,9 @@
Title: Contactgroup deletion does not affect rules / Rules break on next save
Component: wato
-State: open
+Class: bug
+State: done
Date: 2013-02-07 11:10:41
Targetversion: 1.2.2
-Class: bug
When having a contactgroup and a rule which assignes the group,
then deleting the group, the rule still points to the group.
@@ -12,3 +12,6 @@ When opening the edit dialog for that rule, the configured
value can not be selected anymore, since the value does not
exist in the dropdown field. The first element is selected
. When saving this rule it changes the value! this is bad.
+
+2013-03-04 12:06:07: changed state open -> done
+Added checks to find all places where this contactgroup could be referenced
diff --git a/ChangeLog b/ChangeLog
index 074d8e2..9a3fd1d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -86,6 +86,7 @@
* FIX: stop doing snapin refreshes after they have been removed
* FIX: sidebar snapins which refresh do not register for restart detection anymore
* FIX: fix user database corruption in case of a race condition
+ * FIX: added checks wether or not a contactgroup can be deleted
* Changed sidebar snapin reload to a global interval (option:
sidebar_update_interval), defaults to 30 seconds
* Sidebar snapins are now bulk updated with one HTTP request each interval
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 4e6c116..9ff16fa 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -5647,6 +5647,57 @@ def save_configuration_vars(vars, filename):
# | Mode for editing host-, service- and contact groups |
# '----------------------------------------------------------------------'
+# Check if a group is currently in use and cannot be deleted
+# Returns a list of occurrances.
+# Possible usages:
+# - 1. rules: host to contactgroups, services to contactgroups
+# - 2. user memberships
+def find_usage_of_contact_groups(name):
+ # Part 1: Rules
+ used_in = []
+ rulesets = load_all_rulesets()
+ for varname in [ 'host_contactgroups', 'service_contactgroups' ]:
+ ruleset = rulesets[varname]
+ rulespec = g_rulespecs[varname]
+ for folder, rule in ruleset:
+ value, tag_specs, host_list, item_list, rule_options = parse_rule(rulespec,
rule)
+ if value == name:
+ used_in.append(("%s: %s" % (_("Ruleset"),
g_rulespecs[varname]["title"]),
+ make_link([("mode", "edit_ruleset"),
("varname", varname)])))
+
+ # Is the contactgroup assigned to a user?
+ users = filter_hidden_users(userdb.load_users())
+ entries = users.items()
+ entries.sort(cmp = lambda a, b: cmp(a[1].get("alias"),
b[1].get("alias")))
+ for userid, user in entries:
+ cgs = user.get("contactgroups", [])
+ if name in cgs:
+ used_in.append(('%s: %s' % (_('User'),
user.get('alias')),
+ make_link([('mode', 'edit_user'), ('edit',
userid)])))
+
+ global_config = load_configuration_settings()
+
+ # Used in default_user_profile?
+ domain, valuespec, need_restart, allow_reset =
g_configvars['default_user_profile']
+ configured = global_config.get('default_user_profile', {})
+ default_value = valuespec.default_value()
+ if (configured and name in configured['contactgroups']) \
+ or name in default_value['contactgroups']:
+ used_in.append(('%s' % (_('Default User Profile')),
+ make_link([('mode', 'edit_configvar'), ('varname',
'default_user_profile')])))
+
+ # Is the contactgroup used in mkeventd notify (if available)?
+ if 'mkeventd_notify_contactgroup' in g_configvars:
+ domain, valuespec, need_restart, allow_reset =
g_configvars['mkeventd_notify_contactgroup']
+ configured = global_config.get('mkeventd_notify_contactgroup')
+ default_value = valuespec.default_value()
+ if (configured and name == configured) \
+ or name == default_value:
+ used_in.append(('%s' % (valuespec.title()),
+ make_link([('mode', 'edit_configvar'),
('varname', 'mkeventd_notify_contactgroup')])))
+
+ return used_in
+
def mode_groups(phase, what):
if what == "host":
what_name = _("host groups")
@@ -5676,22 +5727,15 @@ def mode_groups(phase, what):
delname = html.var("_delete")
if what == 'contact':
- # Is the contactgroup in use?
- member_links = []
- users = filter_hidden_users(userdb.load_users())
- entries = users.items()
- entries.sort(cmp = lambda a, b: cmp(a[1].get("alias"),
b[1].get("alias")))
- for id, user in entries:
- cgs = user.get("contactgroups", [])
- if delname in cgs:
- member_links.append('<a
href="%s">%s</a>' %
- (make_link([('mode', 'edit_user'),
('edit', id)]), user.get('alias')))
-
- if member_links:
- raise MKUserError(None,
- _('Unable to delete the group %s. It still has the following
members: %s. '
- ' You must first remove all members from the group to be able
to delete the group.') %
- (delname, ',
'.join(member_links)))
+ usages = find_usage_of_contact_groups(delname)
+ if usages:
+ message = "<b>%s</b><br>%s:<ul>" % \
+ (_("You cannot delete this contactgroup."),
+ _("It is still in use by"))
+ for title, link in usages:
+ message += '<li><a
href="%s">%s</a></li>\n' % (link, title)
+ message += "</ul>"
+ raise MKUserError(None, message)
confirm_txt = _('Do you really want to delete the %s group %s?') %
(what, delname)
else: