AIL-framework/bin/NotificationHelper.py

95 lines
3.0 KiB
Python
Raw Normal View History

2018-05-04 13:53:29 +02:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
2018-10-01 15:56:48 +02:00
import argparse
2018-04-16 14:50:04 +02:00
import configparser
import os
import smtplib
2018-04-16 14:50:04 +02:00
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
"""
This module allows the global configuration and management of notification settings and methods.
"""
# CONFIG #
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
# notifications enabled/disabled
TrackedTermsNotificationEnabled_Name = "TrackedNotifications"
# associated notification email addresses for a specific term`
# Keys will be e.g. TrackedNotificationEmails<TERMNAME>
TrackedTermsNotificationEmailsPrefix_Name = "TrackedNotificationEmails_"
2018-10-01 15:56:48 +02:00
2018-07-16 15:51:37 +02:00
def sendEmailNotification(recipient, alert_name, content):
2018-04-16 14:50:04 +02:00
if not os.path.exists(configfile):
raise Exception('Unable to find the configuration file. \
Did you set environment variables? \
Or activate the virtualenv?')
2018-04-16 14:50:04 +02:00
cfg = configparser.ConfigParser()
cfg.read(configfile)
sender = cfg.get("Notifications", "sender"),
sender_host = cfg.get("Notifications", "sender_host"),
sender_port = cfg.getint("Notifications", "sender_port"),
sender_pw = cfg.get("Notifications", "sender_pw"),
2018-04-16 14:50:04 +02:00
if isinstance(sender, tuple):
sender = sender[0]
if isinstance(sender_host, tuple):
sender_host = sender_host[0]
2018-04-16 14:50:04 +02:00
if isinstance(sender_port, tuple):
sender_port = sender_port[0]
2018-04-16 14:50:04 +02:00
if isinstance(sender_pw, tuple):
2018-04-16 14:50:04 +02:00
sender_pw = sender_pw[0]
2018-03-30 11:35:37 +02:00
2018-04-04 09:41:13 +02:00
# raise an exception if any of these is None
if (sender is None or
sender_host is None or
sender_port is None
):
raise Exception('SMTP configuration (host, port, sender) is missing or incomplete!')
2018-03-30 11:35:37 +02:00
2018-04-04 09:41:13 +02:00
try:
if sender_pw is not None:
2018-07-16 15:51:37 +02:00
try:
smtp_server = smtplib.SMTP(sender_host, sender_port)
smtp_server.starttls()
except smtplib.SMTPNotSupportedError:
print("The server does not support the STARTTLS extension.")
smtp_server = smtplib.SMTP_SSL(sender_host, sender_port)
2018-04-04 09:41:13 +02:00
smtp_server.ehlo()
smtp_server.login(sender, sender_pw)
else:
smtp_server = smtplib.SMTP(sender_host, sender_port)
2018-04-16 14:50:04 +02:00
2018-04-04 09:41:13 +02:00
mime_msg = MIMEMultipart()
mime_msg['From'] = sender
mime_msg['To'] = recipient
2018-10-01 15:56:48 +02:00
mime_msg['Subject'] = "AIL Framework " + alert_name + " Alert"
2018-04-16 14:50:04 +02:00
2018-07-16 15:51:37 +02:00
body = content
2018-04-04 09:41:13 +02:00
mime_msg.attach(MIMEText(body, 'plain'))
2018-04-16 14:50:04 +02:00
2018-04-04 09:41:13 +02:00
smtp_server.sendmail(sender, recipient, mime_msg.as_string())
smtp_server.quit()
2018-10-01 15:56:48 +02:00
print('Send notification ' + alert_name + ' to '+recipient)
2018-04-16 14:50:04 +02:00
2018-04-04 09:41:13 +02:00
except Exception as e:
2018-04-16 14:50:04 +02:00
print(str(e))
2018-04-04 09:41:13 +02:00
# raise e
2018-10-01 15:56:48 +02:00
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test notification sender.')
parser.add_argument("addr", help="Test mail 'to' address")
args = parser.parse_args()
sendEmailNotification(args.addr, '_mail test_', 'Success.')