code-dumps/aws/aws-health-events.sh

59 lines
1.5 KiB
Bash
Raw Normal View History

2024-10-24 23:09:21 +08:00
#!/usr/bin/python3
import feedparser
import sqlite3
import smtplib
import json
from email.message import EmailMessage
from email.headerregistry import Address
feeds = ['https://status.aws.amazon.com/rss/ec2-ap-east-1.rss',
'https://status.aws.amazon.com/rss/rds-ap-east-1.rss',
'https://status.aws.amazon.com/rss/vpnvpc-ap-east-1.rss',
'https://status.aws.amazon.com/rss/directconnect-ap-east-1.rss',
'https://status.aws.amazon.com/rss/directconnect-ap-southeast-1.rss']
dbconn = sqlite3.connect('aws-rss.db')
for f in feeds:
NewsFeed = feedparser.parse(f)
for e in NewsFeed.entries:
try:
dbconn.execute("insert into rss values (?,?,?,?,false)", (e.id, e.published, e.title, e.summary))
except sqlite3.IntegrityError:
pass
dbconn.commit()
results = dbconn.execute('select * from rss where notified = false')
msg = " "
records = results.fetchall()
if len(records) == 0:
print("All events already notified")
quit()
content_list = []
for r in records:
map1 = {
'Service': r[0].split('#')[1].split('-')[0].upper(),
'PublishTime': r[1],
'Link': r[0].replace("http","h t t p"),
'Title': r[2],
'Summary': r[3]
}
content_list.append(map1)
dbconn.execute('update rss set notified = true where notified = false')
dbconn.commit()
dbconn.close()
email = EmailMessage()
email['Subject'] = "[ALERT] AWS event detected!"
email['From'] = 'DoNotReply@racker.pro'
email['To'] = 'user@domain.com'
email.set_content(json.dumps(content_list, sort_keys=False, indent=2, ensure_ascii = False))
s = smtplib.SMTP('localhost')
s.send_message(email)