forked from CoreyMSchafer/code_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
51 lines (39 loc) · 1.35 KB
/
monitor.py
File metadata and controls
51 lines (39 loc) · 1.35 KB
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
44
45
46
47
48
49
50
51
import os
import smtplib
import requests
# import logging
from linode_api4 import LinodeClient, Instance
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
LINODE_TOKEN = os.environ.get('LINODE_TOKEN')
# logging.basicConfig(filename='PATH_TO_DESIRED_LOG_FILE',
# level=logging.INFO,
# format='%(asctime)s:%(levelname)s:%(message)s')
def notify_user():
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = 'YOUR SITE IS DOWN!'
body = 'Make sure the server restarted and it is back up'
msg = f'Subject: {subject}\n\n{body}'
# logging.info('Sending Email...')
smtp.sendmail(EMAIL_ADDRESS, 'INSERT_RECEIVER_ADDRESS', msg)
def reboot_server():
client = LinodeClient(LINODE_TOKEN)
my_server = client.load(Instance, 376715)
my_server.reboot()
# logging.info('Attempting to reboot server...')
try:
r = requests.get('https://example.com', timeout=5)
if r.status_code != 200:
# logging.info('Website is DOWN!')
notify_user()
reboot_server()
else:
# logging.info('Website is UP')
except Exception as e:
# logging.info('Website is DOWN!')
notify_user()
reboot_server()