|
| 1 | +import requests |
| 2 | +import smtplib |
| 3 | +import os |
| 4 | +import paramiko |
| 5 | +import time |
| 6 | +import boto3 |
| 7 | + |
| 8 | +# from dotenv import load_dotenv |
| 9 | +# load_dotenv() |
| 10 | + |
| 11 | +EMAIL_ADDRESS = os.getenv('EMAIL_ADDRESS') |
| 12 | +EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD') |
| 13 | + |
| 14 | + |
| 15 | +def send_notification(email_content): |
| 16 | + print(email_content) |
| 17 | +""" with smtplib.SMTP('smtp.gmail.com', 587) as smtp: |
| 18 | + smtp.ehlo() |
| 19 | + smtp.starttls() |
| 20 | + smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) |
| 21 | + #smtp.sendmail(sender, receiver, message) |
| 22 | + email_content = f"Subject:WEB MONITOR ALERT\n{email_content}" |
| 23 | + smtp.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS, email_content) """ |
| 24 | + |
| 25 | +# assume we configured ec2 docker and containerd services to auto start on boot |
| 26 | +# check Official Post-Installation steps on Docker Install |
| 27 | +def restart_container(): |
| 28 | + print("Restarting container...") |
| 29 | + ssh = paramiko.SSHClient() |
| 30 | + |
| 31 | + # known_hosts prompt |
| 32 | + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 33 | + |
| 34 | + # connect as ec2-user with .pem | root connection might require key and key.pub pair |
| 35 | + ssh.connect(hostname='18.192.106.193', |
| 36 | + username="ec2-user", |
| 37 | + key_filename='/home/mltamd/.ssh/ec2-key-pair-frankfurt.pem') |
| 38 | + stdin, stdout, stderr = ssh.exec_command('docker start 06b420614a68') |
| 39 | + print(stdout.readlines()) |
| 40 | + ssh.close() |
| 41 | + |
| 42 | +def reboot_server(): |
| 43 | + print("Rebooting server...") |
| 44 | + ssh = paramiko.SSHClient() |
| 45 | + |
| 46 | + # known_hosts prompt |
| 47 | + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 48 | + |
| 49 | + # connect as ec2-user with .pem | root connection might require key and key.pub pair |
| 50 | + ssh.connect(hostname='18.192.106.193', |
| 51 | + username="ec2-user", |
| 52 | + key_filename='/home/mltamd/.ssh/ec2-key-pair-frankfurt.pem') |
| 53 | + stdin, stdout, stderr = ssh.exec_command('sudo reboot') |
| 54 | + print(stdout.readlines()) |
| 55 | + ssh.close() |
| 56 | + print('------------ssh closed--------------') |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +def stop_and_start_server(): |
| 61 | + ec2_res = boto3.resource('ec2') |
| 62 | + instance = ec2_res.Instance('i-06fba9c3d9747cd17') |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +def wait_for_server_status_and_states_ok(): |
| 70 | + print('Waiting server to be up..') |
| 71 | + while True: |
| 72 | + ec2 = boto3.client('ec2', region_name='eu-central-1') |
| 73 | + print('Waiting 10..') |
| 74 | + time.sleep(10) |
| 75 | + |
| 76 | + # check instance state, status and system status from describe_instance_status |
| 77 | + response = ec2.describe_instance_status( |
| 78 | + IncludeAllInstances=True, |
| 79 | + InstanceIds=[ |
| 80 | + 'i-06fba9c3d9747cd17', |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | + server_state = '' |
| 85 | + instance_status = '' |
| 86 | + system_status = '' |
| 87 | + |
| 88 | + statuses = response["InstanceStatuses"] |
| 89 | + for status in statuses: #improve :no for, pick it up explicitly |
| 90 | + print(f"Instance {status['InstanceId']} state is: {status['InstanceState']['Name']} with instance status: {status['InstanceStatus']['Status']} and system status: {status['SystemStatus']['Status']}") |
| 91 | + print("#######") |
| 92 | + server_state = status['InstanceState']['Name'] |
| 93 | + instance_status = status['InstanceStatus']['Status'] |
| 94 | + system_status = status['SystemStatus']['Status'] |
| 95 | + |
| 96 | + if server_state == 'running' and instance_status == 'ok' and system_status == 'ok': |
| 97 | + print("Server is up...exiting waiting loop") |
| 98 | + time.sleep(2) |
| 99 | + break |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +try: |
| 104 | + r = requests.get( |
| 105 | + 'http://ec2-18-192-106-193.eu-central-1.compute.amazonaws.com:8080/') |
| 106 | + if r.status_code == 200: |
| 107 | + print("Website is up!") |
| 108 | + else: |
| 109 | + print("Website is down!") |
| 110 | + # send email |
| 111 | + message = "Website is down!" |
| 112 | + send_notification(message) |
| 113 | + restart_container() |
| 114 | +except Exception as e: |
| 115 | + print(f"Connection error: {e}") |
| 116 | + message = "Connection error!" |
| 117 | + send_notification(message) |
| 118 | + print("Attempt to restart server and container. This may take some time...") |
| 119 | + reboot_server() |
| 120 | + wait_for_server_status_and_states_ok() |
| 121 | + restart_container() |
| 122 | + |
| 123 | + |
0 commit comments