Script Arguments:
--${event.fields.Category} --${event.fields.ControllerID} --${event.fields.Description} --${event.fields.Event} --${event.fields.EventID} --${event.fields.Severity} --${event.fields.source} --${event.fields.timestamp}
For my purposes, itβs not required to pass any event or backlog fields to the script. Just using script arguments as above.
#!/usr/bin/env python3
import getopt, smtplib, sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def body_html(evnt_fields):
table = "background-color:#f9f9f9;border:none;line-height:1.2;width:100%"
td_odd = "text-align:right;padding-right:20;width:150px"
td_even = "padding-left:20"
th = "background-color:#e6e6e6;line-height:1.5"
try:
rv = f"""\
<html>
<head></head>
<body>
<br />
<table cellpadding="10" cellspacing="0" style="{table}"><tbody>
<tr><th colspan="2" style="{th}"><b>Notification about HDD event on host {evnt_fields['source']}<br />{evnt_fields['Event']}</b></th></tr>
<tr><td style="{td_odd}">Timestamp</td><td style="{td_even}">{evnt_fields['timestamp']}</td></tr>
<tr><td style="{td_odd}">Event</td><td style="{td_even}">[ID{evnt_fields['EventID']}] {evnt_fields['Event']}</td></tr>
<tr><td style="{td_odd}">Severity</td><td style="{td_even}">{evnt_fields['Severity']}</td></tr>
<tr><td style="{td_odd}">Category</td><td style="{td_even}">{evnt_fields['Category']}</td></tr>
<tr><td style="{td_odd}">Controller ID</td><td style="{td_even}">{evnt_fields['ControllerID']}</td></tr>
<tr><td style="{td_odd}">Full Message</td><td style="{td_even}">{evnt_fields['Description']}</td></tr>
</tbody></table>
<br />
</body>
</html>"""
except KeyError:
pass
return rv
def body_plain(evnt_fields):
try:
rv = f"""
--------------------------------------------------------------------------------------------------------------------------
Notification about HDD event on host {evnt_fields['source']}
--------------------------------------------------------------------------------------------------------------------------
Timestamp: {evnt_fields['timestamp']}
Event: [ID{evnt_fields['EventID']}] {evnt_fields['Event']}
Severity: {evnt_fields['Severity']}
Category: {evnt_fields['Category']}
Controller ID: {evnt_fields['ControllerID']}
Full Message: {evnt_fields['Description']}
--------------------------------------------------------------------------------------------------------------------------
"""
except KeyError:
pass
return rv
def parse_cmdline_params(argv):
longopts = ['Category=',
'ControllerID=',
'Description=',
'Event=',
'EventID=',
'Office=',
'Severity=',
'source=',
'timestamp=']
try:
opts, args = getopt.getopt(args=argv,
shortopts="",
longopts=longopts)
except getopt.GetoptError:
sys.exit(2)
rv = {}
for opt, arg in opts:
rv.setdefault(opt[2:], arg)
return rv
def trigger_email(evnt_fields, relay="localhost"):
try:
subject = f"HDD {evnt_fields['Severity']} Event on Host {evnt_fields['source']}"
msgfrom = f"{evnt_fields['Office'].lower()}_hdd_issue@maildomain.com"
msgrcpt = f"{evnt_fields['Office'].lower()}_admins@maildomain.com"
except KeyError:
sys.exit(3)
msg = MIMEMultipart("alternative")
msg['Subject'] = subject
msg['From'] = msgfrom
msg['To'] = msgrcpt
msg.attach(MIMEText(body_plain(evnt_fields), "plain"))
msg.attach(MIMEText(body_html(evnt_fields), "html"))
try:
with smtplib.SMTP(relay) as srv:
srv.send_message(msg)
except Exception:
sys.exit(4)
def main(argv):
evnt_fields = parse_cmdline_params(argv)
trigger_email(evnt_fields)
sys.exit(0)
if __name__ == "__main__":
main(sys.argv[1:])