Module: check_mk
Branch: master
Commit: 026fbdeaa57fa599d3ffd1f581989697ca5846ae
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=026fbdeaa57fa5…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Thu Sep 20 17:18:22 2018 +0200
Isolate Collecting context
Change-Id: Idae5f70342c7d61c40f30b6d9155ecae42306745
---
cmk/notification_plugins/asciimail.py | 7 ++----
cmk/notification_plugins/mail.py | 8 +++----
cmk/notification_plugins/pushover.py | 44 +++++++++++------------------------
cmk/notification_plugins/slack.py | 11 +++------
cmk/notification_plugins/spectrum.py | 8 +++----
cmk/notification_plugins/utils.py | 10 ++++++++
6 files changed, 35 insertions(+), 53 deletions(-)
diff --git a/cmk/notification_plugins/asciimail.py
b/cmk/notification_plugins/asciimail.py
index 9033da5..d199742 100644
--- a/cmk/notification_plugins/asciimail.py
+++ b/cmk/notification_plugins/asciimail.py
@@ -22,15 +22,14 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-
# This script creates an ASCII email. It replaces the builtin ASCII email feature and
# is configurable via WATO with named parameters (only).
-import os
import re
import subprocess
import sys
from email.mime.text import MIMEText
+from cmk.notification_plugins import utils
opt_debug = '-d' in sys.argv
bulk_mode = '--bulk' in sys.argv
@@ -276,9 +275,7 @@ def main():
else:
# gather all options from env
- context = dict([(var[7:], value.decode("utf-8"))
- for (var, value) in os.environ.items()
- if var.startswith("NOTIFY_")])
+ context = utils.collect_context()
content_txt = construct_content(context)
mailto = context['CONTACTEMAIL']
subject = context['SUBJECT']
diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py
index 6c1a705..791ef4e 100644
--- a/cmk/notification_plugins/mail.py
+++ b/cmk/notification_plugins/mail.py
@@ -44,7 +44,7 @@ from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
-from cmk.notification_plugins.utils import extend_context_with_link_urls
+from cmk.notification_plugins import utils
def tmpl_head_html(html_section):
@@ -868,7 +868,7 @@ def construct_content(context):
if context.get('PARAMETER_2'):
context["PARAMETER_URL_PREFIX"] = context["PARAMETER_2"]
- extend_context_with_link_urls(context, '<a
href="%s">%s</a>')
+ utils.extend_context_with_link_urls(context, '<a
href="%s">%s</a>')
# Create a notification summary in a new context variable
# Note: This code could maybe move to cmk --notify in order to
@@ -1068,9 +1068,7 @@ def main():
else:
# gather all options from env
- context = dict([(var[7:], value.decode("utf-8"))
- for (var, value) in os.environ.items()
- if var.startswith("NOTIFY_")])
+ context = utils.collect_context()
content_txt, content_html, attachments = construct_content(context)
mailto = context['CONTACTEMAIL']
subject = context['SUBJECT']
diff --git a/cmk/notification_plugins/pushover.py b/cmk/notification_plugins/pushover.py
index fc83afc..b17c3f3 100644
--- a/cmk/notification_plugins/pushover.py
+++ b/cmk/notification_plugins/pushover.py
@@ -23,18 +23,14 @@
# Boston, MA 02110-1301 USA.
import requests
-import os
import re
import sys
-
+from cmk.notification_plugins import utils
api_url = "https://api.pushover.net/1/messages.json"
def main():
- context = dict([(var[7:], value.decode("utf-8"))
- for (var, value) in os.environ.items()
- if var.startswith("NOTIFY_")])
-
+ context = utils.collect_context()
subject = get_subject(context)
text = get_text(context)
@@ -44,19 +40,6 @@ def main():
send_push_notification(api_key, recipient_key, subject, text, context)
-def get_url(what, context):
- url_prefix = context.get("PARAMETER_URL_PREFIX")
- if url_prefix:
- base_url = url_prefix.rstrip('/')
- if base_url.endswith("/check_mk"):
- base_url = base_url[:-9]
-
- if what == "HOST":
- return base_url + context['HOSTURL']
- else:
- return base_url + context['SERVICEURL']
-
-
def get_subject(context):
s = context["HOSTNAME"]
@@ -93,15 +76,14 @@ def get_subject(context):
def get_text(context):
s = ""
- #s += "<font color=\"%s\">" % color
- #s += "</font>"
s += "$@OUTPUT$"
if "PARAMETER_URL_PREFIX" in context:
+ utils.extend_context_with_link_urls(context, '<a
href="%s">%s</a>')
s += " <i>Link: </i>"
- s += "<a href=\"%s\">Host</a>" %
get_url("HOST", context)
+ s += context["LINKEDHOSTNAME"]
if context["WHAT"] != "HOST":
- s += ", <a href=\"%s\">Service</a>" %
get_url("SERVICE", context)
+ s += context["LINKEDSERVICEDESC"]
return substitute_context(s.replace("@", context["WHAT"]),
context)
@@ -117,12 +99,12 @@ def substitute_context(template, context):
def send_push_notification(api_key, recipient_key, subject, text, context):
params = [
- ("token", api_key),
- ("user", recipient_key),
- ("title", subject.encode("utf-8")),
- ("message", text.encode("utf-8")),
- ("timestamp", int(context["MICROTIME"])/1000000),
- ("html", 1),
+ ("token", api_key),
+ ("user", recipient_key),
+ ("title", subject.encode("utf-8")),
+ ("message", text.encode("utf-8")),
+ ("timestamp", int(context["MICROTIME"]) / 1000000),
+ ("html", 1),
]
if context.get("PARAMETER_PRIORITY") in ["-2", "-1",
"0", "1"]:
@@ -131,8 +113,8 @@ def send_push_notification(api_key, recipient_key, subject, text,
context):
elif context.get("PARAMETER_PRIORITY_PRIORITY") == "2":
params += [
("priority", context["PARAMETER_PRIORITY_PRIORITY"]),
- ("expire", context.get("PARAMETER_PRIORITY_EXPIRE",
0)),
- ("retry", context.get("PARAMETER_PRIORITY_RETRY",
0)),
+ ("expire", context.get("PARAMETER_PRIORITY_EXPIRE", 0)),
+ ("retry", context.get("PARAMETER_PRIORITY_RETRY", 0)),
]
if context.get("PARAMETER_PRIORITY_RECEIPTS"):
params.append(("receipts",
context["PARAMETER_PRIORITY_RECEIPTS"]))
diff --git a/cmk/notification_plugins/slack.py b/cmk/notification_plugins/slack.py
index 49f8c4d..5f82560 100644
--- a/cmk/notification_plugins/slack.py
+++ b/cmk/notification_plugins/slack.py
@@ -30,11 +30,10 @@ Use a slack webhook to send notification messages
"""
from typing import Dict # pylint: disable=unused-import
-import os
import sys
import requests
-from cmk.notification_plugins.utils import extend_context_with_link_urls
+from cmk.notification_plugins import utils
COLORS = {
"CRITICAL": "#EE0000",
@@ -51,7 +50,7 @@ def construct_message(context):
# type: (Dict) -> Dict
"""Build the message for slack"""
- extend_context_with_link_urls(context, '<%s|%s>')
+ utils.extend_context_with_link_urls(context, '<%s|%s>')
if context.get('SERVICESTATE', None):
color = COLORS.get(context["SERVICESTATE"])
@@ -88,11 +87,7 @@ def construct_message(context):
def main():
- context = {
- var[7:]: value.decode("utf-8")
- for (var, value) in os.environ.items()
- if var.startswith("NOTIFY_")
- }
+ context = utils.collect_context()
url = context.get("PARAMETER_WEBHOOK_URL")
diff --git a/cmk/notification_plugins/spectrum.py b/cmk/notification_plugins/spectrum.py
index 43bb2e0..54972d0 100644
--- a/cmk/notification_plugins/spectrum.py
+++ b/cmk/notification_plugins/spectrum.py
@@ -22,9 +22,9 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-import os
import subprocess
import sys
+from cmk.notification_plugins import utils
# Note: This script contains an example configuration
# You will probably have to modify the sent information
@@ -50,13 +50,13 @@ def send_trap(oids, target, community):
def main():
# gather all options from env
- context = dict([(var[7:], value.decode("utf-8")) for (var, value) in
os.environ.items()])
+ context = utils.collect_context()
# check if configured via flexible notifications
if "PARAMETER_1" in context:
- context["PARAMETER_COMMUNITY"] = context["PARAMETER_1"]
+ context["PARAMETER_COMMUNITY"] = context["PARAMETER_1"]
context["PARAMETER_DESTINATION"] = context["PARAMETER_2"]
- context["PARAMETER_BASEOID"] = context["PARAMETER_3"]
+ context["PARAMETER_BASEOID"] = context["PARAMETER_3"]
base_oid = context.get("PARAMETER_BASEOID", "1.3.6.1.4.1.1234")
diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py
index 3b7f797..4a4a7df 100644
--- a/cmk/notification_plugins/utils.py
+++ b/cmk/notification_plugins/utils.py
@@ -24,9 +24,19 @@
# Boston, MA 02110-1301 USA.
from typing import Dict # pylint: disable=unused-import
+import os
import re
+def collect_context():
+ # type: () -> Dict
+ return {
+ var[7:]: value.decode("utf-8")
+ for (var, value) in os.environ.items()
+ if var.startswith("NOTIFY_")
+ }
+
+
def extend_context_with_link_urls(context, link_template):
# type: (Dict, str) -> None
if context.get("PARAMETER_URL_PREFIX"):