-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_canary.py
43 lines (34 loc) · 1.29 KB
/
lambda_canary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import smtplib
from datetime import datetime
from urllib.request import Request, urlopen
from email.mime.text import MIMEText
SITE = os.environ['SITE']
SMTP_DOMAIN = os.environ['SMPT_DOMAIN']
SMTP_PASSWORD = os.environ['SMTP_PASSWORD']
SMTP_SERVER = os.environ['SMTP_SERVER']
SMTP_USERNAME = os.environ['SMTP_USERNAME']
REPORT_EMAIL = os.environ['REPORT_EMAIL']
def send_error():
server = smtplib.SMTP(SMTP_SERVER, 587)
server.login(SMTP_USERNAME, SMTP_PASSWORD)
msg = "Subject: \n\n"
msg = MIMEText('Could not open estimate url.', 'html')
msg['Subject'] = 'Offshore Air Ping Error'
msg['From'] = 'Offshore Air Error <offshoreair-error@{}>'.format(SMTP_DOMAIN)
msg['To'] = REPORT_EMAIL
server.sendmail('Offshore Air Error <offshoreair-error@{}>'.format(SMTP_DOMAIN), REPORT_EMAIL, msg.as_string())
def lambda_handler(event, context):
print('Checking {} at {}...'.format(SITE, event['time']))
try:
req = Request(SITE, headers={'User-Agent': 'AWS Lambda'})
resp = urlopen(req)
except:
send_error()
print('Check failed!')
else:
print('Check passed!')
finally:
print('Check complete at {}'.format(str(datetime.now())))
if __name__ == '__main__':
lambda_handler({'time': str(datetime.now())}, None)