Skip to content

Commit d2a371b

Browse files
committed
Website Monitoring Script
1 parent 11d4c9f commit d2a371b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

‎Python/Site-Monitor/monitor.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
importos
2+
importsmtplib
3+
importrequests
4+
# import logging
5+
fromlinode_api4importLinodeClient, Instance
6+
7+
EMAIL_ADDRESS=os.environ.get('EMAIL_USER')
8+
EMAIL_PASSWORD=os.environ.get('EMAIL_PASS')
9+
LINODE_TOKEN=os.environ.get('LINODE_TOKEN')
10+
11+
# logging.basicConfig(filename='PATH_TO_DESIRED_LOG_FILE',
12+
# level=logging.INFO,
13+
# format='%(asctime)s:%(levelname)s:%(message)s')
14+
15+
16+
defnotify_user():
17+
withsmtplib.SMTP('smtp.gmail.com', 587) assmtp:
18+
smtp.ehlo()
19+
smtp.starttls()
20+
smtp.ehlo()
21+
22+
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
23+
24+
subject='YOUR SITE IS DOWN!'
25+
body='Make sure the server restarted and it is back up'
26+
msg=f'Subject: {subject}\n\n{body}'
27+
28+
# logging.info('Sending Email...')
29+
smtp.sendmail(EMAIL_ADDRESS, 'INSERT_RECEIVER_ADDRESS', msg)
30+
31+
32+
defreboot_server():
33+
client=LinodeClient(LINODE_TOKEN)
34+
my_server=client.load(Instance, 376715)
35+
my_server.reboot()
36+
# logging.info('Attempting to reboot server...')
37+
38+
39+
try:
40+
r=requests.get('https://example.com', timeout=5)
41+
42+
ifr.status_code!=200:
43+
# logging.info('Website is DOWN!')
44+
notify_user()
45+
reboot_server()
46+
else:
47+
# logging.info('Website is UP')
48+
exceptExceptionase:
49+
# logging.info('Website is DOWN!')
50+
notify_user()
51+
reboot_server()

0 commit comments

Comments
(0)