Module: check_mk
Branch: master
Commit: 37c248c65f5f8dbc4ec908fa20156fd98ac51e4a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=37c248c65f5f8d…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Mar 25 14:37:20 2019 +0100
Move history entry formatting for notifications to separate module.
Various cleanups on the way.
Change-Id: I64d3b769b59bd928696f9ae8bf1d52c9bd52dd8a
---
cmk/utils/notify.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++
cmk_base/notify.py | 25 ++++------------
2 files changed, 88 insertions(+), 19 deletions(-)
diff --git a/cmk/utils/notify.py b/cmk/utils/notify.py
new file mode 100644
index 0000000..20d841d
--- /dev/null
+++ b/cmk/utils/notify.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+import cmk.utils.defines
+
+
+def _state_for(exit_code):
+ # The exit codes are not really service states, but we treat them like this.
+ # 0 -> OK
+ # 1 -> temporary issue
+ # 2 -> permanent issue
+ return cmk.utils.defines.service_state_name(exit_code, "UNKNOWN")
+
+
+def notification_message(plugin, context):
+ contact = context["CONTACTNAME"]
+ hostname = context["HOSTNAME"]
+ service = context.get("SERVICEDESC")
+ if service:
+ what = "SERVICE NOTIFICATION"
+ spec = "%s;%s" % (hostname, service)
+ state = context["SERVICESTATE"]
+ output = context["SERVICEOUTPUT"]
+ else:
+ what = "HOST NOTIFICATION"
+ spec = hostname
+ state = context["HOSTSTATE"]
+ output = context["HOSTOUTPUT"]
+ return "%s: %s;%s;%s;%s;%s" % (what, contact, spec, state, plugin, output)
+
+
+def notification_progress_message(plugin, context, exit_code, output):
+ contact = context["CONTACTNAME"]
+ hostname = context["HOSTNAME"]
+ service = context.get("SERVICEDESC")
+ if service:
+ what = "SERVICE NOTIFICATION PROGRESS"
+ spec = "%s;%s" % (hostname, service)
+ else:
+ what = "HOST NOTIFICATION PROGRESS"
+ spec = hostname
+ state = _state_for(exit_code)
+ return "%s: %s;%s;%s;%s;%s" % (what, contact, spec, state, plugin, output)
+
+
+def notification_result_message(plugin, context, exit_code, output):
+ contact = context["CONTACTNAME"]
+ hostname = context["HOSTNAME"]
+ service = context.get("SERVICEDESC")
+ if service:
+ what = "SERVICE NOTIFICATION RESULT"
+ spec = "%s;%s" % (hostname, service)
+ else:
+ what = "HOST NOTIFICATION RESULT"
+ spec = hostname
+ state = _state_for(exit_code)
+ comment = " -- ".join(output)
+ output = output[-1]
+ return "%s: %s;%s;%s;%s;%s;%s" % (what, contact, spec, state, plugin,
output, comment)
diff --git a/cmk_base/notify.py b/cmk_base/notify.py
index 94f90ea..91309f0 100755
--- a/cmk_base/notify.py
+++ b/cmk_base/notify.py
@@ -47,6 +47,7 @@ import time
# suppress "Cannot find module" error from mypy
import livestatus # type: ignore
import cmk.utils.debug
+from cmk.utils.notify import notification_message
from cmk.utils.regex import regex
import cmk.utils.paths
from cmk.utils.exceptions import MKGeneralException
@@ -1310,7 +1311,7 @@ def path_to_notification_script(plugin):
#
# Note: this function is *not* being called for bulk notification.
def call_notification_script(plugin, plugin_context):
- core_notification_log(plugin, plugin_context)
+ _log_to_history(notification_message(plugin or "plain email",
plugin_context))
def plugin_log(s):
notify_log(" %s" % s)
@@ -1744,7 +1745,7 @@ def notify_bulk(dirname, uuids):
# a single entry for each notification contained in the bulk.
# It is important later to have this precise information.
plugin_name = "bulk " + (plugin or "plain email")
- core_notification_log(plugin_name, context)
+ _log_to_history(notification_message(plugin_name, context))
if bulk_context: # otherwise: only corrupted files
# Per default the uuids are sorted chronologically from oldest to newest
@@ -1945,25 +1946,11 @@ def fresh_uuid():
return str(uuid.uuid4())
-def core_notification_log(plugin, plugin_context):
- what = plugin_context["WHAT"]
- contact = plugin_context["CONTACTNAME"]
- spec = plugin_context["HOSTNAME"]
- if what == "HOST":
- state = plugin_context["HOSTSTATE"]
- output = plugin_context["HOSTOUTPUT"]
- if what == "SERVICE":
- spec += ";" + plugin_context["SERVICEDESC"]
- state = plugin_context["SERVICESTATE"]
- output = plugin_context["SERVICEOUTPUT"]
-
- log_message = "%s NOTIFICATION: %s;%s;%s;%s;%s" % (what, contact, spec,
state, plugin or
- "plain email", output)
-
- _send_livestatus_command("LOG;%s" % log_message)
+def _log_to_history(message):
+ _livestatus_cmd("LOG;%s" % message)
-def _send_livestatus_command(command):
+def _livestatus_cmd(command):
try:
livestatus.LocalConnection().command("[%d] %s" % (time.time(),
command.encode("utf-8")))
except Exception as e: