Module: check_mk
Branch: master
Commit: 4c727e363b1acb1c09133e189639a3a3fe60032a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4c727e363b1acb…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Fri Sep 21 10:38:00 2018 +0200
Extract get_bulk_notification_subject
Change-Id: I57eacf57c4fee8e79167a6a550687b8d445672b6
---
cmk/notification_plugins/asciimail.py | 40 +----------------------------------
cmk/notification_plugins/mail.py | 40 +----------------------------------
cmk/notification_plugins/utils.py | 31 +++++++++++++++++++++++++++
3 files changed, 33 insertions(+), 78 deletions(-)
diff --git a/cmk/notification_plugins/asciimail.py
b/cmk/notification_plugins/asciimail.py
index 9fee7ef..4f407bb 100644
--- a/cmk/notification_plugins/asciimail.py
+++ b/cmk/notification_plugins/asciimail.py
@@ -141,44 +141,6 @@ def construct_content(context):
return body
-def find_wato_folder(context):
- # Same as in notify.py
- for tag in context.get("HOSTTAGS", "").split():
- if tag.startswith("/wato/"):
- return tag[6:].rstrip("/")
- return ""
-
-
-def get_bulk_notification_subject(contexts, hosts):
- hosts = list(hosts)
- bulk_subject = None
- folder = None
- bulk_context = {}
- for context in contexts:
- if context.get("PARAMETER_BULK_SUBJECT"):
- bulk_context = context
- bulk_subject = context["PARAMETER_BULK_SUBJECT"]
- folder = find_wato_folder(context)
- break
-
- if bulk_subject:
- subject = bulk_subject
- elif len(hosts) == 1:
- subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" %
hosts[0]
- else:
- subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$
hosts"
-
- if "$FOLDER$" in subject and folder is not None:
- subject = subject.replace("$FOLDER$", folder)
- if "$COUNT_NOTIFICATIONS$" in subject:
- subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts)))
- if "$COUNT_HOSTS$" in subject:
- subject = subject.replace("$COUNT_HOSTS$", str(len(hosts)))
-
- subject = utils.substitute_context(subject, bulk_context)
- return subject
-
-
def main():
if bulk_mode:
content_txt = ""
@@ -190,7 +152,7 @@ def main():
mailto = context['CONTACTEMAIL'] # Assume the same in each context
subject = context['SUBJECT']
hosts.add(context["HOSTNAME"])
- subject = get_bulk_notification_subject(contexts, hosts)
+ subject = utils.get_bulk_notification_subject(contexts, hosts)
else:
# gather all options from env
diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py
index db54919..c055fa3 100644
--- a/cmk/notification_plugins/mail.py
+++ b/cmk/notification_plugins/mail.py
@@ -917,44 +917,6 @@ def render_elements(context, elements):
utils.substitute_context(tmpl_html, context)
-def find_wato_folder(context):
- # Same as in notify.py
- for tag in context.get("HOSTTAGS", "").split():
- if tag.startswith("/wato/"):
- return tag[6:].rstrip("/")
- return ""
-
-
-def get_bulk_notification_subject(contexts, hosts):
- hosts = list(hosts)
- bulk_subject = None
- folder = None
- bulk_context = {}
- for context in contexts:
- if context.get("PARAMETER_BULK_SUBJECT"):
- bulk_context = context
- bulk_subject = context["PARAMETER_BULK_SUBJECT"]
- folder = find_wato_folder(context)
- break
-
- if bulk_subject:
- subject = bulk_subject
- elif len(hosts) == 1:
- subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" %
hosts[0]
- else:
- subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$
hosts"
-
- if "$FOLDER$" in subject and folder is not None:
- subject = subject.replace("$FOLDER$", folder)
- if "$COUNT_NOTIFICATIONS$" in subject:
- subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts)))
- if "$COUNT_HOSTS$" in subject:
- subject = subject.replace("$COUNT_HOSTS$", str(len(hosts)))
-
- subject = utils.substitute_context(subject, bulk_context)
- return subject
-
-
def main():
if bulk_mode:
attachments = []
@@ -971,7 +933,7 @@ def main():
mailto = context['CONTACTEMAIL'] # Assume the same in each context
subject = context['SUBJECT']
hosts.add(context["HOSTNAME"])
- subject = get_bulk_notification_subject(contexts, hosts)
+ subject = utils.get_bulk_notification_subject(contexts, hosts)
else:
# gather all options from env
diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py
index 3299cd2..573529b 100644
--- a/cmk/notification_plugins/utils.py
+++ b/cmk/notification_plugins/utils.py
@@ -36,6 +36,7 @@ except ImportError:
# Default to python2
from cgi import escape as html_escape
+from cmk_base.notify import find_wato_folder
def collect_context():
# type: () -> Dict
@@ -125,6 +126,7 @@ def set_mail_headers(target, subject, from_address, reply_to, mail):
return mail
+
def send_mail_sendmail(m, target, from_address):
cmd = ["/usr/sbin/sendmail"]
if from_address:
@@ -169,3 +171,32 @@ def read_bulk_contexts():
context[key] = value
return parameters, contexts
+
+def get_bulk_notification_subject(contexts, hosts):
+ hosts = list(hosts)
+ bulk_subject = None
+ folder = None
+ bulk_context = {}
+ for context in contexts:
+ if context.get("PARAMETER_BULK_SUBJECT"):
+ bulk_context = context
+ bulk_subject = context["PARAMETER_BULK_SUBJECT"]
+ folder = find_wato_folder(context)
+ break
+
+ if bulk_subject:
+ subject = bulk_subject
+ elif len(hosts) == 1:
+ subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" %
hosts[0]
+ else:
+ subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$
hosts"
+
+ if "$FOLDER$" in subject and folder is not None:
+ subject = subject.replace("$FOLDER$", folder)
+ if "$COUNT_NOTIFICATIONS$" in subject:
+ subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts)))
+ if "$COUNT_HOSTS$" in subject:
+ subject = subject.replace("$COUNT_HOSTS$", str(len(hosts)))
+
+ subject = substitute_context(subject, bulk_context)
+ return subject