commit 2334239ede9f1510094f147fcf118a71d1c32829 Author: James Pattinson Date: Wed Feb 19 21:02:38 2025 +0000 first commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..46127bc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM python:latest +WORKDIR /usr/local/bin +COPY pinger.py . +CMD ["python","-u","pinger.py"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2763460 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,7 @@ +version: '3.8' + +services: + meshmqtt: + restart: unless-stopped + build: + context: . diff --git a/pinger.py b/pinger.py new file mode 100644 index 0000000..8946701 --- /dev/null +++ b/pinger.py @@ -0,0 +1,52 @@ +import os +import time +import smtplib +from email.mime.text import MIMEText + +def send_email_alert(ip_address): + smtp_server = "mail.smtp2go.com" + smtp_port = 587 + smtp_username = "pinger" + smtp_password = "Qix46X7aPhvIZwrk" + sender_email = "pinger@pattinson.org" + receiver_email = "james@pattinson.org" + subject = "ALERT: EGFH gateway unreachable" + body = f"The server with IP {ip_address} has been unreachable for over 5 minutes." + + msg = MIMEText(body) + msg["Subject"] = subject + msg["From"] = sender_email + msg["To"] = receiver_email + + try: + with smtplib.SMTP(smtp_server, smtp_port) as server: + server.starttls() + server.login(smtp_username, smtp_password) + server.sendmail(sender_email, receiver_email, msg.as_string()) + print("Alert email sent.") + except Exception as e: + print(f"Failed to send email: {e}") + +def ping_host(ip_address): + response = os.system(f"ping -c 1 {ip_address} > /dev/null 2>&1") + return response == 0 + +def monitor(ip_address, timeout=300, check_interval=10): + downtime_start = None + print(f"Starting monitor process on {ip_address}") + + while True: + if ping_host(ip_address): + downtime_start = None # Reset downtime if host is reachable + else: + if downtime_start is None: + downtime_start = time.time() + elif time.time() - downtime_start > timeout: + send_email_alert(ip_address) + downtime_start = None # Reset after sending email + time.sleep(timeout) # Avoid spamming alerts + + time.sleep(check_interval) + +if __name__ == "__main__": + monitor("192.168.24.1") # Replace with the actual IP