Module: check_mk
Branch: master
Commit: e211771664cf232d6684ff9b3a725c959188c1f2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e211771664cf23…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Dec 14 16:16:20 2016 +0100
Remove livestatus_fetch_query(), use livestatus.LocalConnection() instead
Change-Id: Ia58b1bac8cd9edccca53a98bee5b331da4365369
---
modules/events.py | 42 +++++++++++++++++++++---------------------
modules/notify.py | 21 ++++++++++++++-------
2 files changed, 35 insertions(+), 28 deletions(-)
diff --git a/modules/events.py b/modules/events.py
index f81f5ba..89faef8 100644
--- a/modules/events.py
+++ b/modules/events.py
@@ -28,6 +28,7 @@
# only) by the alert handling
import pprint, urllib, select, subprocess, socket
+import livestatus, os
from cmk.regex import regex
@@ -187,36 +188,31 @@ def find_host_service_in_context(context):
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" % (
+ query = "GET services\nFilter: host_name = %s\nFilter:
service_description = %s\nColumns: contacts" % (
host, service)
else:
- query = "GET hosts\nFilter: host_name = %s\nColumns: contacts\n" %
host
+ query = "GET hosts\nFilter: host_name = %s\nColumns: contacts" %
host
+
+ contact_list = livestatus.LocalConnection().query_value(query)
+ if "check-mk-notify" in contact_list: # Remove artifical contact used
for rule based notifications
+ contact_list.remove("check-mk-notify")
+ return contact_list
- 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 livestatus.MKLivestatusNotFoundError:
+ if not service:
+ return None
+ else:
+ # Service not found: try again with contacts of host!
+ return livestatus_fetch_contacts(host, None)
except Exception, e:
- if cmk.debug.enabled():
+ if True or cmk.debug.enabled():
raise
- return "" # We must allow notifications without Livestatus access
-
+ return None # We must allow notifications without Livestatus access
@@ -224,7 +220,11 @@ 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"))
+ contact_list = livestatus_fetch_contacts(
+ raw_context["HOSTNAME"],
+ raw_context.get("SERVICEDESC"))
+ if contact_list != None:
+ raw_context["CONTACTS"] = ",".join(contact_list)
# Add a pseudo contact name. This is needed for the correct creation
diff --git a/modules/notify.py b/modules/notify.py
index e372475..abc0cf6 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -795,17 +795,24 @@ def rbn_all_contacts(with_email=None):
def rbn_groups_contacts(groups):
if not groups:
return {}
- contacts = set([])
query = "GET contactgroups\nColumns: members\n"
for group in groups:
query += "Filter: name = %s\n" % group
query += "Or: %d\n" % len(groups)
- response = livestatus_fetch_query(query)
- for line in response.splitlines():
- line = line.strip()
- if line:
- contacts.update(line.split(","))
- return contacts
+
+ try:
+ contacts = set([])
+ for contact_list in livestatus.LocalConnection().query_column(query):
+ contacts.update(contact_list)
+ return contacts
+
+ except livestatus.MKLivestatusNotFoundError:
+ return []
+
+ except Exception, e:
+ if cmk.config.debug:
+ raise
+ return []
def rbn_emails_contacts(emails):