Module: check_mk
Branch: master
Commit: 9beb92bb39ebc4641ff713e62cb3ab6305a3a838
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9beb92bb39ebc4…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri May 17 13:08:47 2013 +0200
EC: New rule match: only apply rule within specified time period
---
ChangeLog | 1 +
mkeventd/bin/mkeventd | 111 ++++++++++++++++++++++++++-------
mkeventd/web/plugins/wato/mkeventd.py | 13 +++-
scripts/setup.sh | 1 +
4 files changed, 101 insertions(+), 25 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 4869f98..4eb9a49 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,7 @@
* New rule feature: automatically delete event after actions
* New filter for maximum services level (minimum already existed)
* New global setting: hostname translation (allows e.g. to drop domain name)
+ * New rule match: only apply rule within specified time period
Checks & Agents:
* solaris_mem: New check for memory and swap for Solaris agent
diff --git a/mkeventd/bin/mkeventd b/mkeventd/bin/mkeventd
index 563a6f6..67289e8 100755
--- a/mkeventd/bin/mkeventd
+++ b/mkeventd/bin/mkeventd
@@ -241,6 +241,60 @@ class VerboseLock:
#.
+# .--Timeperiods---------------------------------------------------------.
+# | _____ _ _ _ |
+# | |_ _(_)_ __ ___ ___ _ __ ___ _ __(_) ___ __| |___ |
+# | | | | | '_ ` _ \ / _ \ '_ \ / _ \ '__| |/ _ \ / _` / __| |
+# | | | | | | | | | | __/ |_) | __/ | | | (_) | (_| \__ \ |
+# | |_| |_|_| |_| |_|\___| .__/ \___|_| |_|\___/ \__,_|___/ |
+# | |_| |
+# +----------------------------------------------------------------------+
+# | Timeperiods are used in rule conditions |
+# '----------------------------------------------------------------------'
+
+# Dictionary from name to True/False (active / inactive)
+g_timeperiods = None
+g_last_timeperiod_update = 0
+
+def update_timeperiods():
+ global g_timeperiods, g_last_timeperiod_update
+
+ if g_timeperiods != None and time.time() < g_last_config_reload + 60:
+ return # only update once a minute
+ log("Updating timeperiod information")
+
+ try:
+ livesock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ livesock.connect(g_livestatus_socket)
+ livesock.send("GET timeperiods\nColumns: name alias in\n")
+ livesock.shutdown(socket.SHUT_WR)
+ answer = livesock.recv(10000000)
+ table = [ line.split(';') for line in answer.split('\n')[:-1] ]
+ new_timeperiods = {}
+ for tpname, alias, isin in table:
+ new_timeperiods[tpname] = (alias, isin == '1' and True or False)
+ g_timeperiods = new_timeperiods
+ g_last_timeperiod_update = time.time()
+ except Exception, e:
+ log("Cannot update timeperiod information: %s" % e)
+ if opt_debug:
+ raise
+
+def check_timeperiod(tpname):
+ update_timeperiods()
+ if not g_timeperiods:
+ log("Warning: no timeperiod information. Assuming %s active" % tpname)
+ return True
+
+ elif tpname not in g_timeperiods:
+ log("Warning: no such timeperiod %s. Assume to active" % tpname)
+ return True
+
+ else:
+ return g_timeperiods[tpname][1]
+
+
+#.
# .--Daemonize-----------------------------------------------------------.
# | ____ _ |
# | | _ \ __ _ ___ _ __ ___ ___ _ __ (_)_______ |
@@ -1356,6 +1410,11 @@ class EventServer:
log(" did not match because of wrong syslog facility")
return False
+ if "match_timeperiod" in rule and not
check_timeperiod(rule["match_timeperiod"]):
+ if debug:
+ log(" did not match, because timeperiod %s is not active"
% rule["match_timeperiod"])
+ return False
+
if "match_ok" in rule or "cancel_priority" in rule:
if "cancel_priority" in rule:
up, lo = rule["cancel_priority"]
@@ -2729,6 +2788,7 @@ def usage():
-C, --configdir Path to directory where mkevent.mk lives
-S, --socket P Path to unix socket for querying status
-E, --eventsocket P Path to unix socket for receiving events (optional)
+ -L, --livestatus P Path to livestatus socket of monitoring core (optional)
-P, --pipe P Path to pipe for receiving events
--syslog Enable builtin syslog server
--syslog-fd FD Do not open UDP port 514, but inherit it via this FD
@@ -2742,13 +2802,14 @@ def usage():
sys.stdout.write("""You are running OMD, which is generally a good
idea.
The following defaults are set:
- Config dir: %(g_config_dir)s
- Unix socket: %(g_socket_path)s
- Event socket: %(g_eventsocket_path)s
- Event Pipe: %(g_pipe_path)s
- PID file: %(g_pid_file)s
- Log file: %(g_logfile_path)s
- Status dir: %(g_state_dir)s
+ Config dir: %(g_config_dir)s
+ Unix socket: %(g_socket_path)s
+ Event socket: %(g_eventsocket_path)s
+ Livestatus socket %(g_livestatus_socket)s
+ Event Pipe: %(g_pipe_path)s
+ PID file: %(g_pid_file)s
+ Log file: %(g_logfile_path)s
+ Status dir: %(g_state_dir)s
""" % globals())
@@ -2781,27 +2842,29 @@ opt_syslog_fd = None
# Set default values for options
omd_root = os.getenv("OMD_ROOT")
if omd_root:
- g_config_dir = omd_root + "/etc/check_mk"
- g_socket_path = omd_root + "/tmp/run/mkeventd/status"
- g_eventsocket_path = omd_root + "/tmp/run/mkeventd/eventsocket"
- g_pipe_path = omd_root + "/tmp/run/mkeventd/events"
- g_pid_file = omd_root + "/tmp/run/mkeventd/pid"
- g_logfile_path = omd_root + "/var/log/mkeventd.log"
- g_state_dir = omd_root + "/var/mkeventd"
+ g_config_dir = omd_root + "/etc/check_mk"
+ g_socket_path = omd_root + "/tmp/run/mkeventd/status"
+ g_eventsocket_path = omd_root + "/tmp/run/mkeventd/eventsocket"
+ g_livestatus_socket = omd_root + "/tmp/run/live"
+ g_pipe_path = omd_root + "/tmp/run/mkeventd/events"
+ g_pid_file = omd_root + "/tmp/run/mkeventd/pid"
+ g_logfile_path = omd_root + "/var/log/mkeventd.log"
+ g_state_dir = omd_root + "/var/mkeventd"
else:
- g_config_dir = "/etc/check_mk"
- g_socket_path = None
- g_eventsocket_path = None
- g_pipe_path = None
- g_pid_file = "/var/run/mkeventd.pid"
- g_logfile_path = "/var/log/mkeventd.log"
- g_state_dir = "/var/lib/mkeventd"
+ g_config_dir = "/etc/check_mk"
+ g_socket_path = None
+ g_eventsocket_path = None
+ g_livestatus_socket = None
+ g_pipe_path = None
+ g_pid_file = "/var/run/mkeventd.pid"
+ g_logfile_path = "/var/log/mkeventd.log"
+ g_state_dir = "/var/lib/mkeventd"
-short_options = "hvVgS:P:p:C:E:"
+short_options = "hvVgS:P:p:C:E:L:"
long_options = [ "help", "version", "verbose",
"debug", "foreground", "socket=", "eventsocket=",
"pipe=",
"pidfile=", "statedir=", "configdir=",
"logdir=", "profile-status", "profile-event",
"debug-locking",
- "syslog", "syslog-fd=" ]
+ "syslog", "syslog-fd=", "livestatus=" ]
try:
opts, args = getopt.getopt(sys.argv[1:], short_options, long_options)
@@ -2820,6 +2883,8 @@ try:
g_socket_path = a
elif o in [ '-E', '--eventsocket' ]:
g_eventsocket_path = a
+ elif o in [ '-L', '--livestatus' ]:
+ g_livestatus_socket = a
elif o in [ '-P', '--pipe' ]:
g_pipe_path = a
elif o == '--syslog':
diff --git a/mkeventd/web/plugins/wato/mkeventd.py
b/mkeventd/web/plugins/wato/mkeventd.py
index 86e70b1..a67370a 100644
--- a/mkeventd/web/plugins/wato/mkeventd.py
+++ b/mkeventd/web/plugins/wato/mkeventd.py
@@ -523,6 +523,15 @@ vs_mkeventd_rule = Dictionary(
],
),
),
+ ( "match_timeperiod",
+ TimeperiodSelection(
+ title = _("Match only during timeperiod"),
+ help = _("Match this rule only during times where the selected
timeperiod from the monitoring "
+ "system is active. The Timeperiod definitions are taken from
the monitoring core that "
+ "is running on the same host or OMD site as the event daemon.
Please note, that this "
+ "selection only offers timeperiods that are defined with
WATO."),
+ ),
+ ),
( "match_ok",
RegExpUnicode(
title = _("Text to cancel event"),
@@ -610,13 +619,13 @@ vs_mkeventd_rule = Dictionary(
),
],
optional_keys = [ "delay", "livetime", "count",
"expect", "match_priority", "match_priority",
- "match_facility", "match_sl",
"match_host", "match_application",
+ "match_facility", "match_sl",
"match_host", "match_application", "match_timeperiod",
"set_text", "set_host",
"set_application", "set_comment",
"set_contact", "cancel_priority",
"match_ok" ],
headers = [
( _("General Properties"), [ "id", "description",
"disabled" ] ),
( _("Matching Criteria"), [ "match", "match_host",
"match_application", "match_priority", "match_facility",
- "match_sl", "match_ok",
"cancel_priority" ]),
+ "match_sl", "match_ok",
"cancel_priority", "match_timeperiod" ]),
( _("Outcome & Action"), [ "state", "sl",
"actions", "drop", "autodelete" ]),
( _("Counting & Timing"), [ "count",
"expect", "delay", "livetime", ]),
( _("Rewriting"), [ "set_text", "set_host",
"set_application", "set_comment", "set_contact" ]),
diff --git a/scripts/setup.sh b/scripts/setup.sh
index 90a7989..aafa7ff 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -612,6 +612,7 @@ VARDIR=$mkeventdstatedir
CONFDIR=$confdir
MKEVENTD_SYSLOG=off
RUNUSER=$nagiosuser
+LIVESTATUS=$livesock
# DEBUG="--debug --foreground"
PIDFILE=\$SOCKETDIR/pid