Module: check_mk
Branch: master
Commit: 8968c8085e214a6e689e16e33d94914bc53c4421
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8968c8085e214a…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Jun 6 13:29:55 2014 +0200
States of events can now be set by patterns
The states of events created by a rule can now be set by regex patterns. A rule can now
create events with different states. The mechanism is as follows:
1. The "text to match" patterns needs to match a message
2. When a rule has configured "(set by message text)" as state, and patterns are
defined
for the single states, the message will be matches agains these patterns to calculate
the state of the event.
3. When none of these patterns matches, the event is set to UNKNOWN
---
.werks/816 | 15 ++++++++++
ChangeLog | 3 ++
mkeventd/bin/mkeventd | 53 ++++++++++++++++++++++++++-------
mkeventd/web/plugins/wato/mkeventd.py | 52 ++++++++++++++++++++++++++++----
4 files changed, 107 insertions(+), 16 deletions(-)
diff --git a/.werks/816 b/.werks/816
new file mode 100644
index 0000000..fa6c985
--- /dev/null
+++ b/.werks/816
@@ -0,0 +1,15 @@
+Title: States of events can now be set by patterns
+Level: 2
+Component: ec
+Version: 1.2.5i4
+Date: 1402053935
+Class: feature
+
+The states of events created by a rule can now be set by regex patterns. A rule can now
+create events with different states. The mechanism is as follows:
+
+1. The "text to match" patterns needs to match a message
+2. When a rule has configured "(set by message text)" as state, and patterns
are defined
+for the single states, the message will be matches agains these patterns to calculate
+the state of the event.
+3. When none of these patterns matches, the event is set to UNKNOWN
diff --git a/ChangeLog b/ChangeLog
index dca2aa4..2db0181 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -19,6 +19,9 @@
Reporting & Availability:
* 0985 Availability: display phases of freqent state changes as "chaos"...
+ Event Console:
+ * 0816 States of events can now be set by patterns...
+
1.2.5i3:
Core & Setup:
diff --git a/mkeventd/bin/mkeventd b/mkeventd/bin/mkeventd
index e61528e..e9a9eb0 100755
--- a/mkeventd/bin/mkeventd
+++ b/mkeventd/bin/mkeventd
@@ -1369,6 +1369,22 @@ class EventServer:
count_rules = 0
count_unspecific = 0
+ def compile_matching_value(key, val):
+ value = val.strip()
+ # Remove leading .* from regex. This is redundant and
+ # dramatically destroys performance when doing an infix search.
+ if key in [ "match", "match_ok" ]:
+ while value.startswith(".*") and not
value.startswith(".*?"):
+ value = value[2:]
+
+ if not value:
+ return None
+
+ if is_regex(value):
+ return re.compile(value, re.IGNORECASE)
+ else:
+ return val.lower()
+
for rule in rules:
if rule.get("disabled"):
count_disabled += 1
@@ -1387,19 +1403,22 @@ class EventServer:
try:
for key in [ "match", "match_ok",
"match_host", "match_application" ]:
if key in rule:
- value = rule[key].strip()
- # Remote leading .* from regex. This is redundant and
- # dramatically destroys performance when doing an infix
search.
- if key in [ "match", "match_ok" ]:
- while value.startswith(".*") and not
value.startswith(".*?"):
- value = value[2:]
- if not value:
+ value = compile_matching_value(key, rule[key])
+ if value == None:
del rule[key]
continue
- if is_regex(value):
- rule[key] = re.compile(value, re.IGNORECASE)
- else:
- rule[key] = rule[key].lower()
+
+ rule[key] = value
+
+ if 'state' in rule and type(rule['state']) == tuple
and rule['state'][0] == 'text_pattern':
+ for key in [ '2', '1', '0' ]:
+ if key in rule['state'][1]:
+ value = compile_matching_value('state',
rule['state'][1][key])
+ if value == None:
+ del rule['state'][1][key]
+ else:
+ rule['state'][1][key] = value
+
except Exception, e:
if opt_debug:
raise
@@ -1681,8 +1700,20 @@ class EventServer:
event["state"] = 2
else:
event["state"] = 1
+ elif type(rule["state"]) == tuple and rule["state"][0] ==
"text_pattern":
+ for key in [ '2', '1', '0', '3' ]:
+ if key in rule["state"][1]:
+ log(repr(rule["state"][1][key]))
+ match_groups = match(rule["state"][1][key],
event["text"], complete = False)
+ if match_groups != False:
+ event["state"] = int(key)
+ break
+ elif key == '3': # No rule matched!
+ event["state"] = 3
+
else:
event["state"] = rule["state"]
+
if "sl" not in event:
event["sl"] = rule["sl"]
event["first"] = event["time"]
diff --git a/mkeventd/web/plugins/wato/mkeventd.py
b/mkeventd/web/plugins/wato/mkeventd.py
index ad724a0..48c6d81 100644
--- a/mkeventd/web/plugins/wato/mkeventd.py
+++ b/mkeventd/web/plugins/wato/mkeventd.py
@@ -202,10 +202,45 @@ vs_mkeventd_actions = \
)
-class RuleState(MonitoringState):
+class RuleState(CascadingDropdown):
def __init__(self, **kwargs):
- MonitoringState.__init__(self, **kwargs)
- self._choices.append((-1, _("(set by syslog)")))
+ choices = [
+ ( 0, _("OK")),
+ ( 1, _("WARN")),
+ ( 2, _("CRIT")),
+ ( 3, _("UNKNOWN")),
+ (-1, _("(set by syslog)")),
+ ('text_pattern', _('(set by message text)'),
+ Dictionary(
+ elements = [
+ ('2', RegExpUnicode(
+ title = _("CRIT Pattern"),
+ help = _("When the given regular expression (infix
search) matches "
+ "the events state is set to CRITICAL."),
+ size = 64,
+ )),
+ ('1', RegExpUnicode(
+ title = _("WARN Pattern"),
+ help = _("When the given regular expression (infix
search) matches "
+ "the events state is set to WARNING."),
+ size = 64,
+ )),
+ ('0', RegExpUnicode(
+ title = _("OK Pattern"),
+ help = _("When the given regular expression (infix
search) matches "
+ "the events state is set to OK."),
+ size = 64,
+ )),
+ ],
+ help = _('Individual patterns matching the text (which must have
been matched by '
+ 'the generic "text to match pattern" before)
which set the state of the '
+ 'generated event depending on the
match.<br><br>'
+ 'First the CRITICAL pattern is tested, then WARNING and
OK at last. '
+ 'When none of the patterns matches, the events state is
set to UNKNOWN.'),
+ )
+ ),
+ ]
+ CascadingDropdown.__init__(self, choices = choices, **kwargs)
vs_mkeventd_rule = Dictionary(
title = _("Rule Properties"),
@@ -966,8 +1001,15 @@ def mode_mkeventd_rules(phase):
if rule.get("drop"):
table.cell(_("Priority"), _("DROP"), css="state
statep")
else:
- txt = {0:_("OK"), 1:_("WARN"), 2:_("CRIT"),
3:_("UNKNOWN"), -1:_("(syslog)")}[rule["state"]]
- table.cell(_("Priority"), txt, css="state state%d" %
rule["state"])
+ if type(rule['state']) == tuple:
+ stateval = rule["state"][0]
+ else:
+ stateval = rule["state"]
+ txt = { 0: _("OK"), 1:_("WARN"),
+ 2: _("CRIT"), 3:_("UNKNOWN"),
+ -1: _("(syslog)"),
+ 'text_pattern':_("(set by message text)")
}[stateval]
+ table.cell(_("Priority"), txt, css="state state%s" %
stateval)
# Syslog priority
if "match_priority" in rule: