Module: check_mk
Branch: master
Commit: 82bcdb60f0dc19c2af44e463094a9a86da92d1fd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=82bcdb60f0dc19…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Feb 25 10:37:16 2014 +0100
RBN: prepare notification context for rule based notifications
---
modules/check_mk.py | 31 ++++++++++++++++++++------
modules/notify.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 82 insertions(+), 11 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index e1844ff..9fb692c 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -1444,14 +1444,10 @@ def host_contactgroups_of(hostlist):
first_list = False
else:
cgrs.append(entry)
+ if monitoring_core == "nagios" and enable_rulebased_notifications:
+ cgrs.append("check-mk-notify")
return list(set(cgrs))
-def host_contactgroups_nag(hostlist):
- cgrs = host_contactgroups_of(hostlist)
- if len(cgrs) > 0:
- return " contact_groups " + ",".join(cgrs) +
"\n"
- else:
- return ""
def parents_of(hostname):
par = host_extra_conf(hostname, parents)
@@ -1481,6 +1477,8 @@ def extra_service_conf_of(hostname, description):
sercgr = service_extra_conf(hostname, description, service_contactgroups)
contactgroups_to_define.update(sercgr)
if len(sercgr) > 0:
+ if enable_rulebased_notifications:
+ sercgr.append("check-mk-notify") # not nessary if not explicit
groups defined
conf += " contact_groups\t\t" + ",".join(sercgr) +
"\n"
sergr = service_extra_conf(hostname, description, service_groups)
@@ -2614,7 +2612,11 @@ def create_nagios_config_contacts(outfile):
outfile.write(" email\t\t\t\t%s\n" %
contact["email"])
if "pager" in contact:
outfile.write(" pager\t\t\t\t%s\n" %
contact["pager"])
- not_enabled = contact.get("notifications_enabled", True)
+ if enable_rulebased_notifications:
+ not_enabled = False
+ else:
+ not_enabled = contact.get("notifications_enabled", True)
+
for what in [ "host", "service" ]:
no = contact.get(what + "_notification_options", "")
if not no or not not_enabled:
@@ -2630,6 +2632,21 @@ def create_nagios_config_contacts(outfile):
outfile.write(" contactgroups\t\t\t%s\n" % ",
".join(cgrs))
outfile.write("}\n\n")
+ if enable_rulebased_notifications:
+ outfile.write(
+ "# Needed for rule based notifications\n"
+ "define contact {\n"
+ " contact_name\t\t\tcheck-mk-notify\n"
+ " alias\t\t\t\tContact for rule based notifications\n"
+ " host_notification_options\td,u,r,f,s\n"
+ " service_notification_options\tu,c,w,r,f,s\n"
+ " host_notification_period\t24X7\n"
+ " service_notification_period\t24X7\n"
+ " host_notification_commands\tcheck-mk-notify\n"
+ " service_notification_commands\tcheck-mk-notify\n"
+ " contactgroups\t\t\tcheck-mk-notify\n"
+ "}\n\n");
+
# Quote string for use in a nagios command execution.
# Please note that also quoting for ! and \ vor Nagios
diff --git a/modules/notify.py b/modules/notify.py
index d52a55e..34a07a2 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -34,7 +34,7 @@
# hostname, servicedesc, hoststate, servicestate, output in
# the form %(variable)s
-import pprint, urllib, select, subprocess
+import pprint, urllib, select, subprocess, socket
# Default settings
notification_logdir = var_dir + "/notify"
@@ -294,7 +294,8 @@ def do_notify(args):
notify_mode, argument = args
if notify_mode in ['fake-service', 'fake-host']:
- plugin = argument
+ global fake_plugin
+ fake_plugin = argument
if notify_mode in ['spoolfile']:
filename = argument
@@ -447,10 +448,11 @@ def convert_context_to_unicode(context):
context[key] = value_unicode
def notify_notify(context):
+ if notification_logging >= 2:
+
notify_log("----------------------------------------------------------------------")
notify_log("Got notification context with %s variables" % len(context))
# Add a few further helper variables
- import socket
context["MONITORING_HOST"] = socket.gethostname()
if omd_root:
context["OMD_ROOT"] = omd_root
@@ -483,13 +485,17 @@ def notify_notify(context):
(context['HOSTNAME'],
context['SERVICEDESC']))
if notify_mode in [ 'fake-service', 'fake-host' ]:
- sys.exit(call_notification_script(plugin, [], context, True))
+ sys.exit(call_notification_script(fake_plugin, [], context, True))
if 'LASTHOSTSTATECHANGE' in context:
context['LASTHOSTSTATECHANGE_REL'] =
get_readable_rel_date(context['LASTHOSTSTATECHANGE'])
if context['WHAT'] != 'HOST' and 'LASTSERVICESTATECHANGE' in
context:
context['LASTSERVICESTATECHANGE_REL'] =
get_readable_rel_date(context['LASTSERVICESTATECHANGE'])
+ # Rule based notifications enabled? We might need to complete a few macros
+ if enable_rulebased_notifications:
+ add_rulebased_macros(context)
+
convert_context_to_unicode(context)
if notification_logging >= 2:
@@ -511,6 +517,54 @@ def notify_notify(context):
process_context(context, notification_spooling)
+def add_rulebased_macros(context):
+ # For the rule based notifications we need the list of contacts
+ # an object has. The CMC does send this in the macro "CONTACTS"
+ # (the count) and macros of the form CONTACTALIAS[hh]=Harry Hirsch
+ if "NUM_CONTACTS" not in context:
+ livestatus_fetch_contacts(context, context["HOSTNAME"],
context.get("SERVICEDESC"))
+
+def livestatus_fetch_contacts(context, host, service):
+
+ if service:
+ query = "GET services\nFilter: host_name = %s\nFilter: service_description =
%s\nColumns: contacts\n" % (
+ host, service)
+ else:
+ query = "GET hosts\nFilter: host_name = %s\nColumns: contacts\n" %
host
+
+ contacts = livestatus_fetch_query(query).split(",")
+
+ # Now get all relevant information
+ query = "GET contacts\nColumns: name alias email pager\nSeparators: 1 2 3
4\n"
+ for contact in contacts:
+ query += "Filter: name = %s\n" % contact.strip()
+ query += "Or: %d\n" % len(contacts)
+ response = livestatus_fetch_query(query)
+ lines = response.split('\1')
+ count = 0
+ for line in lines:
+ parts = line.split('\2')
+ if len(parts) == 4:
+ name, alias, email, pager = parts
+ if name != "check-mk-notify":
+ context["CONTACTALIAS[%s]" % name] = alias
+ context["CONTACTEMAIL[%s]" % name] = email
+ context["CONTACTPAGER[%s]" % name] = pager
+ count += 1
+ context["NUM_CONTACTS"] = str(count)
+
+def livestatus_fetch_query(query):
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.connect(livestatus_unix_socket)
+ sock.send(query)
+ sock.shutdown(socket.SHUT_WR)
+ response = sock.recv(10000000)
+ sock.close()
+ return response
+
+
+
+
def notify_via_email(context, write_into_spoolfile):
if write_into_spoolfile:
notify_log("Spooled this notification.")