-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monitor_website and send email notification.py
36 lines (33 loc) · 1.22 KB
/
Monitor_website and send email notification.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
import requests
import smtplib
import os
import paramiko
EMAIL_ADDRESS = os.environ.get('EMAIL_ADDRESS')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD')
def send_notification(email_msg):
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
message = f"Subject: SITE DOWN\n{email_msg}"
smtp.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS, message)
try:
response= requests.get('Website link')
if response.status_code == 200:
print('Application is running successfully!')
else:
print('Application is down, fix it')
msg = f'Application returned {response.status_code}'
send_notification()
#restart the application
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='Ip-Adress', username='root', key_filename='/Users/diab/.ssh/id_rsa')
stdin, stdout, stderr = ssh.exec_command('docker start')
print(stdout.readline())
ssh.close()
print('application restarted')
except Exception as ex:
print(f'connection error Happened: {ex}')
msg = 'Application not accessible at all'
send_notification(msg)