2018-05-04 11:53:29 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-27 14:12:02 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2018-10-01 13:56:48 +00:00
|
|
|
import argparse
|
2018-11-05 13:59:40 +00:00
|
|
|
import traceback
|
2018-02-27 14:12:02 +00:00
|
|
|
import smtplib
|
2018-11-05 13:59:40 +00:00
|
|
|
from pubsublogger import publisher
|
2018-04-16 12:50:04 +00:00
|
|
|
from email.mime.multipart import MIMEMultipart
|
2018-02-27 14:12:02 +00:00
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib/'))
|
|
|
|
import ConfigLoader
|
|
|
|
|
2018-02-27 14:12:02 +00:00
|
|
|
"""
|
|
|
|
This module allows the global configuration and management of notification settings and methods.
|
|
|
|
"""
|
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
2018-02-27 14:12:02 +00:00
|
|
|
|
2018-11-05 13:59:40 +00:00
|
|
|
publisher.port = 6380
|
|
|
|
publisher.channel = "Script"
|
|
|
|
|
2018-07-16 13:51:37 +00:00
|
|
|
def sendEmailNotification(recipient, alert_name, content):
|
2018-04-16 12:50:04 +00:00
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
sender = config_loader.get_config_str("Notifications", "sender")
|
2020-02-06 08:18:54 +00:00
|
|
|
sender_user = config_loader.get_config_str("Notifications", "sender_user")
|
2019-11-05 14:18:03 +00:00
|
|
|
sender_host = config_loader.get_config_str("Notifications", "sender_host")
|
|
|
|
sender_port = config_loader.get_config_int("Notifications", "sender_port")
|
|
|
|
sender_pw = config_loader.get_config_str("Notifications", "sender_pw")
|
2018-11-05 13:30:03 +00:00
|
|
|
if sender_pw == 'None':
|
2018-11-05 13:20:12 +00:00
|
|
|
sender_pw = None
|
2018-03-30 09:35:37 +00:00
|
|
|
|
2018-04-04 07:41:13 +00: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 09:35:37 +00:00
|
|
|
|
2018-04-04 07:41:13 +00:00
|
|
|
try:
|
|
|
|
if sender_pw is not None:
|
2018-07-16 13:51:37 +00: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 07:41:13 +00:00
|
|
|
smtp_server.ehlo()
|
2020-02-06 08:18:54 +00:00
|
|
|
if sender_user is not None:
|
|
|
|
smtp_server.login(sender_user, sender_pw)
|
|
|
|
else:
|
|
|
|
smtp_server.login(sender, sender_pw)
|
2018-04-04 07:41:13 +00:00
|
|
|
else:
|
|
|
|
smtp_server = smtplib.SMTP(sender_host, sender_port)
|
2018-04-16 12:50:04 +00:00
|
|
|
|
2018-04-04 07:41:13 +00:00
|
|
|
mime_msg = MIMEMultipart()
|
|
|
|
mime_msg['From'] = sender
|
|
|
|
mime_msg['To'] = recipient
|
2018-10-01 13:56:48 +00:00
|
|
|
mime_msg['Subject'] = "AIL Framework " + alert_name + " Alert"
|
2018-04-16 12:50:04 +00:00
|
|
|
|
2018-07-16 13:51:37 +00:00
|
|
|
body = content
|
2018-04-04 07:41:13 +00:00
|
|
|
mime_msg.attach(MIMEText(body, 'plain'))
|
2018-04-16 12:50:04 +00:00
|
|
|
|
2018-04-04 07:41:13 +00:00
|
|
|
smtp_server.sendmail(sender, recipient, mime_msg.as_string())
|
|
|
|
smtp_server.quit()
|
2018-10-01 13:56:48 +00:00
|
|
|
print('Send notification ' + alert_name + ' to '+recipient)
|
2018-04-16 12:50:04 +00:00
|
|
|
|
2018-11-05 13:59:40 +00:00
|
|
|
except Exception as err:
|
|
|
|
traceback.print_tb(err.__traceback__)
|
|
|
|
publisher.warning(err)
|
2018-10-01 13:56:48 +00: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.')
|