Module: check_mk
Branch: master
Commit: 81f797a722c248993ca70726fea633c83526acc7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=81f797a722c248…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Fri Sep 21 09:55:42 2018 +0200
Extract sendmail by sendmail
Change-Id: Id24813276d972597a96a450ba47bd72005a2d53a
---
cmk/notification_plugins/asciimail.py | 20 +-------------------
cmk/notification_plugins/mail.py | 23 +----------------------
cmk/notification_plugins/utils.py | 20 ++++++++++++++++++++
3 files changed, 22 insertions(+), 41 deletions(-)
diff --git a/cmk/notification_plugins/asciimail.py
b/cmk/notification_plugins/asciimail.py
index fb7dd41..98b55ad 100644
--- a/cmk/notification_plugins/asciimail.py
+++ b/cmk/notification_plugins/asciimail.py
@@ -59,24 +59,6 @@ Handler output: $ALERTHANDLEROUTPUT$
tmpl_alerthandler_service_body = "Service: $SERVICEDESC$\n" +
tmpl_alerthandler_host_body
-def send_mail(m, target, from_address):
- cmd = ["/usr/sbin/sendmail"]
- if from_address:
- cmd += ['-F', from_address, "-f", from_address]
- cmd += ["-i", target.encode("utf-8")]
-
- try:
- p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
- except OSError:
- raise Exception("Failed to send the mail: /usr/sbin/sendmail is
missing")
-
- p.communicate(m.as_string())
- if p.returncode != 0:
- raise Exception("sendmail returned with exit code: %d" % p.returncode)
-
- return 0
-
-
def construct_content(context):
# Create a notification summary in a new context variable
@@ -255,7 +237,7 @@ def main():
m = utils.set_mail_headers(mailto, subject, from_address, reply_to,
MIMEText(content_txt, 'plain',
_charset='utf-8'))
try:
- sys.exit(send_mail(m, mailto, from_address))
+ sys.exit(utils.send_mail_sendmail(m, mailto, from_address))
except Exception, e:
sys.stderr.write("Unhandled exception: %s\n" % e)
# unhandled exception, don't retry this...
diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py
index a1f9f1f..9a83ce9 100644
--- a/cmk/notification_plugins/mail.py
+++ b/cmk/notification_plugins/mail.py
@@ -33,7 +33,6 @@
import base64
import os
import socket
-import subprocess
import sys
import urllib
import urllib2
@@ -45,7 +44,6 @@ from email.mime.text import MIMEText
from cmk.notification_plugins import utils
-
def tmpl_head_html(html_section):
return '''
<html>
@@ -517,25 +515,6 @@ def multipart_mail(target, subject, from_address, reply_to,
content_txt, content
return utils.set_mail_headers(target, subject, from_address, reply_to, m)
-def send_mail_sendmail(m, target, from_address):
- cmd = ["/usr/sbin/sendmail"]
- if from_address:
- cmd += ['-F', from_address, "-f", from_address]
- cmd += ["-i", target.encode("utf-8")]
-
- try:
- p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
- except OSError:
- raise Exception("Failed to send the mail: /usr/sbin/sendmail is
missing")
-
- p.communicate(m.as_string())
- if p.returncode != 0:
- raise Exception("sendmail returned with exit code: %d" % p.returncode)
-
- sys.stdout.write("Spooled mail to local mail transmission agent\n")
- return 0
-
-
def send_mail_smtp(message, target, from_address, context):
import smtplib # for the error messages
host_index = 1
@@ -643,7 +622,7 @@ def send_mail(message, target, from_address, context):
if "PARAMETER_SMTP_PORT" in context:
return send_mail_smtp(message, target, from_address, context)
else:
- return send_mail_sendmail(message, target, from_address)
+ return utils.send_mail_sendmail(message, target, from_address)
def fetch_pnp_data(context, params):
diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py
index 200be9f..37f61fa 100644
--- a/cmk/notification_plugins/utils.py
+++ b/cmk/notification_plugins/utils.py
@@ -26,6 +26,8 @@
from typing import Dict # pylint: disable=unused-import
import os
import re
+import sys
+import subprocess
try:
# First try python3
@@ -122,3 +124,21 @@ def set_mail_headers(target, subject, from_address, reply_to, mail):
mail['Reply-To'] = target
return mail
+
+def send_mail_sendmail(m, target, from_address):
+ cmd = ["/usr/sbin/sendmail"]
+ if from_address:
+ cmd += ['-F', from_address, "-f", from_address]
+ cmd += ["-i", target.encode("utf-8")]
+
+ try:
+ p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
+ except OSError:
+ raise Exception("Failed to send the mail: /usr/sbin/sendmail is
missing")
+
+ p.communicate(m.as_string())
+ if p.returncode != 0:
+ raise Exception("sendmail returned with exit code: %d" % p.returncode)
+
+ sys.stdout.write("Spooled mail to local mail transmission agent\n")
+ return 0