Module: check_mk
Branch: master
Commit: d615289c77f5814522f27b345210b957c184897d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d615289c77f581…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu May 3 13:41:13 2012 +0200
WATO: configure timeperiod exclusion
---
ChangeLog | 1 +
modules/check_mk.py | 4 ++-
web/htdocs/check_mk.css | 3 ++
web/htdocs/valuespec.py | 4 +-
web/htdocs/views.css | 2 +-
web/htdocs/wato.py | 64 ++++++++++++++++++++++++++++++++++++++++------
6 files changed, 65 insertions(+), 13 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index acfab98..656a3de 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -45,6 +45,7 @@
* FIX: do not loose host tags when both using WATO-configured and
manual ones (via multisite.mk)
* Timeperiods: Make list of exceptions dynamic, not fixed to 10 entries
+ * Timeperiods: Configure exclusion of other timeperiods
1.2.0b2:
Core:
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 461c4d5..67c42b9 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -1926,10 +1926,12 @@ def create_nagios_config_timeperiods(outfile):
if "alias" in tp:
outfile.write(" alias\t\t\t\t%s\n" %
tp["alias"].encode("utf-8"))
for key, value in tp.items():
- if key != "alias":
+ if key not in [ "alias", "exclude" ]:
times = ",".join([ ("%s-%s" % (fr, to)) for (fr,
to) in value ])
if times:
outfile.write(" %-20s\t\t%s\n" % (key, times))
+ if "exclude" in tp:
+ outfile.write(" exclude\t\t\t%s\n" %
",".join(tp["exclude"]))
outfile.write("}\n\n")
def create_nagios_config_contacts(outfile):
diff --git a/web/htdocs/check_mk.css b/web/htdocs/check_mk.css
index 9dd620c..a0c5c43 100644
--- a/web/htdocs/check_mk.css
+++ b/web/htdocs/check_mk.css
@@ -300,6 +300,9 @@ div.error {
border-color: #e40;
color: white;
}
+div.error a {
+ color: white;
+}
div.warning {
background-color: #ff4;
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 666fa3b..5a29b85 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -942,8 +942,8 @@ class ListChoice(ValueSpec):
html.write("</tr>")
html.write("<tr>")
html.write("<td>")
- html.checkbox("%s_%d" % (varprefix, nr), key in value)
- html.write(" %s</td>\n" % title)
+ html.checkbox("%s_%d" % (varprefix, nr), key in value, label =
title)
+ html.write("</td>")
html.write("</tr></table>")
def value_to_text(self, value):
diff --git a/web/htdocs/views.css b/web/htdocs/views.css
index 3dcd248..00cf5c9 100644
--- a/web/htdocs/views.css
+++ b/web/htdocs/views.css
@@ -171,7 +171,7 @@ div.floatfilter {
}
div.floatfilter.double {
- height: 111px;
+ height: 110px;
}
div.floatfilter input[type=text] {
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 2c48d2e..d037128 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -5704,7 +5704,7 @@ class ExceptionName(TextAscii):
if value in [ "monday", "tuesday", "wednesday",
"thursday",
"friday", "saturday", "sunday" ]:
raise MKUserError(varprefix, _("You cannot use weekday names (%s) in
exceptions" % value))
- if value in [ "name", "alias", "timeperiod_name",
"register", "use" ]:
+ if value in [ "name", "alias", "timeperiod_name",
"register", "use", "exclude" ]:
raise MKUserError(varprefix, _("<tt>%s</tt> is a reserved
keyword."))
TextAscii.validate_value(self, value, varprefix)
@@ -5745,10 +5745,27 @@ class MultipleTimeRanges(ValueSpec):
for c, v in enumerate(value):
self._rangevs.validate_value(v, varprefix + "_%d" % c)
+# Check, if timeperiod tpa excludes or is tpb
+def timeperiod_excludes(timeperiods, tpa_name, tpb_name):
+ if tpa_name == tpb_name:
+ return True
+
+ tpa = timeperiods[tpa_name]
+ for ex in tpa.get("exclude", []):
+ if ex == tpb_name:
+ return True
+ if timeperiod_excludes(timeperiods, ex, tpb_name):
+ return True
+ return False
+
def mode_edit_timeperiod(phase):
num_columns = 3
+ timeperiods = load_timeperiods()
+ name = html.var("edit") # missing -> new group
+ new = name == None
+ # ValueSpec for the list of Exceptions
vs_ex = ListOf(
Tuple(
orientation = "horizontal",
@@ -5760,6 +5777,16 @@ def mode_edit_timeperiod(phase):
movable = False,
add_label = _("Add Exception"))
+ # ValueSpec for excluded Timeperiods. We offer the list of
+ # all other timeperiods - but only those that do not
+ # exclude the current timeperiod (in order to avoid cycles)
+ other_tps = []
+ for tpname, tp in timeperiods.items():
+ if not new and not timeperiod_excludes(timeperiods, tpname, name):
+ other_tps.append((tpname, tp.get("alias") or name))
+
+ vs_excl = ListChoice(choices = other_tps)
+
# convert Check_MK representation of range to ValueSpec-representation
def convert_from_tod(tod):
# "00:30" -> (0, 30)
@@ -5792,10 +5819,6 @@ def mode_edit_timeperiod(phase):
MultipleTimeRanges().validate_value(value, varprefix)
return map(convert_to_range, value)
-
- name = html.var("edit") # missing -> new group
- new = name == None
-
if phase == "title":
if new:
return _("Create new time period")
@@ -5806,7 +5829,6 @@ def mode_edit_timeperiod(phase):
html.context_button(_("All Timeperiods"), make_link([("mode",
"timeperiods")]), "back")
return
- timeperiods = load_timeperiods()
if new:
timeperiod = {}
else:
@@ -5844,6 +5866,12 @@ def mode_edit_timeperiod(phase):
for exname, ranges in exceptions:
timeperiod[exname] = map(convert_to_range, ranges)
+ # extract excludes
+ excludes = vs_excl.from_html_vars("exclude")
+ vs_excl.validate_value(excludes, "exclude")
+ if excludes:
+ timeperiod["exclude"] = excludes
+
if new:
name = html.var("name")
if len(name) == 0:
@@ -5915,11 +5943,21 @@ def mode_edit_timeperiod(phase):
exceptions = []
for k in timeperiod:
- if k not in [ w[0] for w in weekdays ] and k != "alias":
+ if k not in [ w[0] for w in weekdays ] and k not in [ "alias",
"exclude" ]:
exceptions.append((k, map(convert_from_range, timeperiod[k])))
exceptions.sort()
vs_ex.render_input("except", exceptions)
+
+ # Excludes
+ if other_tps:
+ html.write("<tr><td class=legend>")
+ html.write(_("Exclude"))
+ html.write("</td><td class=content>")
+ vs_excl.render_input("exclude", timeperiod.get("exclude",
[]))
+ html.write("</td></tr>")
+
+
html.write("<tr><td colspan=2 class=buttons>")
html.button("save", _("Save"))
html.write("</td></tr>")
@@ -5940,8 +5978,9 @@ class TimeperiodSelection(ElementSelection):
# Check if a timeperiod is currently in use and cannot be deleted
# Returns a list of occurrances.
# Possible usages:
-# - user accounts (notification period)
-# - rules: service/host-notification/check-period
+# - 1. rules: service/host-notification/check-period
+# - 2. user accounts (notification period)
+# - 3. excluded by other timeperiods
def find_usage_of_timeperiod(tpname):
# Part 1: Rules
@@ -5963,6 +6002,13 @@ def find_usage_of_timeperiod(tpname):
used_in.append(("%s: %s" % (_("User"), userid),
make_link([("mode", "edit_user"), ("edit",
userid)])))
+ # Part 3: Other Timeperiods
+ for tpn, tp in load_timeperiods().items():
+ if tpname in tp.get("exclude", []):
+ used_in.append(("%s: %s (%s)" % (_("Timeperiod"),
tp.get("alias", tpn),
+ _("excluded")),
+ make_link([("mode", "edit_timeperiod"),
("edit", tpn)])))
+
return used_in