Module: check_mk
Branch: master
Commit: df182d9abf76b5325c05cde400f11e824dc7b5eb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=df182d9abf76b5…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Feb 25 16:33:11 2014 +0100
RBN: implement some of the rule conditions
---
modules/notify.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++
web/htdocs/valuespec.py | 6 +++-
web/htdocs/wato.py | 20 +++++++++----
3 files changed, 90 insertions(+), 7 deletions(-)
diff --git a/modules/notify.py b/modules/notify.py
index 39b1bd8..04fa0bb 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -336,6 +336,10 @@ def notify_keepalive():
while True:
try:
+ # Invalidate timeperiod cache
+ global g_inactive_timerperiods
+ g_inactive_timerperiods = None
+
# If the configuration has changed, we do a restart. But we do
# this check just before the next notification arrives. We must
# *not* read data from stdin, just peek! There is still one
@@ -886,6 +890,73 @@ def rbn_match_rule(rule, context):
if rule.get("disabled"):
return "This rule is disabled"
+ return \
+ rbn_match_hosttags(rule, context) or \
+ rbn_match_hosts(rule, context) or \
+ rbn_match_exclude_hosts(rule, context) or \
+ rbn_match_services(rule, context) or \
+ rbn_match_exclude_services(rule, context) or \
+ rbn_match_checktype(rule, context) or \
+ rbn_match_timeperiod(rule)
+
+
+def rbn_match_hosttags(rule, context):
+ required = rule.get("match_hosttags")
+ if required:
+ tags = context.get("HOSTTAGS", "").split()
+ if not hosttags_match_taglist(tags, required):
+ return "The host's tags %s do not match the required tags %s" %
(
+ "|".join(tags), "|".join(required))
+
+def rbn_match_hosts(rule, context):
+ if "match_hosts" in rule:
+ hostlist = rule["match_hosts"]
+ if context["HOSTNAME"] not in hostlist:
+ return "The host's name '%s' is not on the list of allowed
hosts (%s)" % (
+ context["HOSTNAME"], ", ".join(hostlist))
+
+def rbn_match_exclude_hosts(rule, context):
+ if context["HOSTNAME"] in rule.get("match_exclude_hosts", []):
+ return "The host's name '%s' is on the list of excluded
hosts" % context["HOSTNAME"]
+
+
+def rbn_match_services(rule, context):
+ if "match_services" in rule:
+ if context["WHAT"] != "SERVICE":
+ return "The rule specifies a list of services, but this is a host
notification."
+ servicelist = rule["match_services"]
+ service = context["SERVICEDESC"]
+ if not in_extraconf_servicelist(servicelist, service):
+ return "The service's description '%s' dows not match by the
list of " \
+ "allowed services (%s)" % (service, ",
".join(servicelist))
+
+def rbn_match_exclude_services(rule, context):
+ excludelist = rule.get("match_exclude_services", [])
+ service = context["SERVICEDESC"]
+ if in_extraconf_servicelist(excludelist, service):
+ return "The service's description '%s' matches the list of
excluded services" \
+ % context["SERVICEDESC"]
+
+def rbn_match_checktype(rule, context):
+ if "match_checktype" in rule:
+ if context["WHAT"] != "SERVICE":
+ return "The rule specifies a list of Check_MK plugins, but this is a
host notification."
+ command = context["SERVICECHECKCOMMAND"]
+ if not command.startswith("check_mk-"):
+ return "The rule specified a list of Check_MK plugins, but his is no
Check_MK service."
+ plugin = command[9:]
+ allowed = rule["match_checktype"]
+ if plugin not in allowed:
+ return "The Check_MK plugin '%s' is not on the list of allowed
plugins (%s)" % \
+ (plugin, ", ".join(allowed))
+
+def rbn_match_timeperiod(rule):
+ if "match_timeperiod" in rule:
+ timeperiod = rule["match_timeperiod"]
+ if timeperiod != "24X7" and not check_timeperiod(timeperiod):
+ return "The timeperiod '%s' is currently not active." %
timeperiod
+
+
def rbn_rule_contacts(rule, context):
contacts = set([])
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index fdb2ec2..c47ad63 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -707,7 +707,11 @@ class ListOfStrings(ValueSpec):
def validate_value(self, value, vp):
if len(value) == 0 and not self._allow_empty:
- raise MKUserError(vp + "_0", _("Please specify at least one
value"))
+ if self._empty_text:
+ msg = self._empty_text
+ else:
+ msg = _("Please specify at least one value")
+ raise MKUserError(vp + "_0", msg)
if self._valuespec:
for nr, s in enumerate(value):
self._valuespec.validate_value(s, vp + "_%d" % nr)
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 0040a3a..da4f276 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -6374,13 +6374,13 @@ def factory_reset():
# '----------------------------------------------------------------------'
-class CheckTypeSelection(ListChoice):
+class CheckTypeSelection(DualListChoice):
def __init__(self, **kwargs):
- ListChoice.__init__(self, columns=3, **kwargs)
+ DualListChoice.__init__(self, **kwargs)
def get_elements(self):
checks = check_mk_local_automation("get-check-information")
- elements = [ (cn, "<span title=\"%s\">%s</span>"
% (c["title"], cn)) for (cn, c) in checks.items()]
+ elements = [ (cn, (cn + " - " + c["title"])[:60]) for (cn, c)
in checks.items()]
elements.sort()
return elements
@@ -7402,7 +7402,8 @@ def vs_notification_rule():
title = _("Match only the following hosts"),
size = 24,
orientation = "horizontal",
-
+ allow_empty = False,
+ empty_text = _("Please specify at least one host. Disable the
option if you want to allow all hosts."),
)
),
( "match_exclude_hosts",
@@ -7414,9 +7415,14 @@ def vs_notification_rule():
),
( "match_services",
ListOfStrings(
- title = _("Match only the following hosts"),
+ title = _("Match only the following services"),
+ help = _("Specify a list of regular expressions that must match
the <b>beginning</b> of the "
+ "service name in order for the rule to match. Note: Host
notifications never match this "
+ "rule if this option is being used."),
valuespec = TextUnicode(size = 32),
orientation = "horizontal",
+ allow_empty = False,
+ empty_text = _("Please specify at least one service regex. Disable
the option if you want to allow all services."),
)
),
( "match_exclude_services",
@@ -7428,7 +7434,9 @@ def vs_notification_rule():
),
( "match_checktype",
CheckTypeSelection(
- title = _("Match the following check types")
+ title = _("Match the following check types"),
+ help = _("Only apply the rule if the notification originates from
certain types of check plugins. "
+ "Note: Host notifications never match this rule if this
option is being used."),
)
),
( "match_timeperiod",