[checkmk-commits] Move some functions from notify.py to events.py

Mathias Kettner mk at mathias-kettner.de
Wed Dec 14 16:16:48 CET 2016


Module: check_mk
Branch: master
Commit: f017dc63308bc63914e5edfccfb56fec30d83f44
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f017dc63308bc63914e5edfccfb56fec30d83f44

Author: Mathias Kettner <mk at mathias-kettner.de>
Date:   Wed Dec 14 15:36:09 2016 +0100

Move some functions from notify.py to events.py

Reason: notify depends on events - not vice versa. But these functions
were called from events to notify.

Change-Id: I45877bbb4ba107181bc8d931ff8e8f205ecf683e

---

 modules/events.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 modules/notify.py | 43 -------------------------------------------
 2 files changed, 49 insertions(+), 43 deletions(-)

diff --git a/modules/events.py b/modules/events.py
index 0c4afa2..f81f5ba 100644
--- a/modules/events.py
+++ b/modules/events.py
@@ -186,6 +186,55 @@ def find_host_service_in_context(context):
     else:
         return host
 
+
+def livestatus_fetch_query(query):
+    # TODO: use livestatus.py instead!
+    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    sock.connect(cmk.paths.livestatus_unix_socket)
+    sock.send(query)
+    sock.shutdown(socket.SHUT_WR)
+    response = sock.recv(10000000)
+    sock.close()
+    return response
+
+
+def livestatus_fetch_contacts(host, service):
+    try:
+        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
+
+        commasepped = livestatus_fetch_query(query).strip()
+        aslist = commasepped.split(",")
+        if "check-mk-notify" in aslist: # Remove artifical contact used for rule based notifications
+            aslist.remove("check-mk-notify")
+        return ",".join(aslist)
+
+    except Exception, e:
+        if cmk.debug.enabled():
+            raise
+        return "" # We must allow notifications without Livestatus access
+
+
+
+
+def add_rulebased_macros(raw_context):
+    # For the rule based notifications we need the list of contacts
+    # an object has. The CMC does send this in the macro "CONTACTS"
+    if "CONTACTS" not in raw_context:
+        raw_context["CONTACTS"] = livestatus_fetch_contacts(raw_context["HOSTNAME"], raw_context.get("SERVICEDESC"))
+
+
+    # Add a pseudo contact name. This is needed for the correct creation
+    # of spool files. Spool files are created on a per-contact-base, as in classical
+    # notifications the core sends out one individual notification per contact.
+    # In the case of rule based notifications we do not make distinctions between
+    # the various contacts.
+    raw_context["CONTACTNAME"] = "check-mk-notify"
+
+
 # Add a few further helper variables that are useful in notification and alert plugins
 def complete_raw_context(raw_context, with_dump, event_log):
     raw_keys = list(raw_context.keys())
diff --git a/modules/notify.py b/modules/notify.py
index 1e26a4e..e372475 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -508,19 +508,6 @@ def rbn_finalize_plugin_parameters(hostname, plugin, rule_parameters):
     else:
         return rule_parameters
 
-def add_rulebased_macros(raw_context):
-    # For the rule based notifications we need the list of contacts
-    # an object has. The CMC does send this in the macro "CONTACTS"
-    if "CONTACTS" not in raw_context:
-        raw_context["CONTACTS"] = livestatus_fetch_contacts(raw_context["HOSTNAME"], raw_context.get("SERVICEDESC"))
-
-    # Add a pseudo contact name. This is needed for the correct creation
-    # of spool files. Spool files are created on a per-contact-base, as in classical
-    # notifications the core sends out one individual notification per contact.
-    # In the case of rule based notifications we do not make distinctions between
-    # the various contacts.
-    raw_context["CONTACTNAME"] = "check-mk-notify"
-
 
 # Create a table of all user specific notification rules. Important:
 # create deterministic order, so that rule analyses can depend on
@@ -574,27 +561,6 @@ def rbn_add_contact_information(plugin_context, contact):
         rbn_add_contact_information(plugin_context, contact_dict)
 
 
-def livestatus_fetch_contacts(host, service):
-    try:
-        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
-
-        commasepped = livestatus_fetch_query(query).strip()
-        aslist = commasepped.split(",")
-        if "check-mk-notify" in aslist: # Remove artifical contact used for rule based notifications
-            aslist.remove("check-mk-notify")
-        return ",".join(aslist)
-
-    except:
-        if cmk.debug.enabled():
-            raise
-        return "" # We must allow notifications without Livestatus access
-
-
-
 def rbn_match_rule(rule, context):
     if rule.get("disabled"):
         return "This rule is disabled"
@@ -1643,15 +1609,6 @@ def substitute_context(template, context):
 #   |  Some generic helper functions                                       |
 #   '----------------------------------------------------------------------'
 
-def livestatus_fetch_query(query):
-    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-    sock.connect(cmk.paths.livestatus_unix_socket)
-    sock.send(query)
-    sock.shutdown(socket.SHUT_WR)
-    response = sock.recv(10000000)
-    sock.close()
-    return response
-
 def livestatus_send_command(command):
     try:
         message = "COMMAND [%d] %s\n" % (time.time(), command)



More information about the checkmk-commits mailing list