Skip to content

Commit d2a371b

Browse files
committedFeb 27, 2019
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

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import smtplib
3+
import requests
4+
# import logging
5+
from linode_api4 import LinodeClient, 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+
def notify_user():
17+
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
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+
def reboot_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+
if r.status_code != 200:
43+
# logging.info('Website is DOWN!')
44+
notify_user()
45+
reboot_server()
46+
else:
47+
# logging.info('Website is UP')
48+
except Exception as e:
49+
# logging.info('Website is DOWN!')
50+
notify_user()
51+
reboot_server()

0 commit comments

Comments
 (0)
Please sign in to comment.