Initial resurrection kit — 81 scripts, 62 references, configs, systemd units, crons, Docker compose files, Caddy config, master README
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""apex-mail-watchdog.py — Check Apex WPForms email delivery every 5 min.
|
||||
|
||||
Connects to the Apex SMTP, sends a test, and checks debug events for
|
||||
recent failures. If mail is down, sends an alert via Hermes cron.
|
||||
"""
|
||||
import smtplib, ssl, json, sys, os
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from pathlib import Path
|
||||
import mysql.connector
|
||||
|
||||
LOG = "/var/log/apex-mail-watchdog.log"
|
||||
SMTP_HOST = "c1113726.sgvps.net"
|
||||
SMTP_PORT = 2525
|
||||
SMTP_USER = "contact@apextrackexperience.com"
|
||||
SMTP_PASS = "apex.track!!"
|
||||
ALERT_TO = "g@germainebrown.com"
|
||||
FROM = "contact@apextrackexperience.com"
|
||||
|
||||
def log(msg):
|
||||
ts = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
line = f"[{ts}] {msg}"
|
||||
print(line)
|
||||
with open(LOG, "a") as f:
|
||||
f.write(line + "\n")
|
||||
|
||||
def check_smtp():
|
||||
"""Try sending a test email to the admin address."""
|
||||
try:
|
||||
ctx = ssl.create_default_context()
|
||||
with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=15) as s:
|
||||
s.starttls(context=ctx)
|
||||
s.login(SMTP_USER, SMTP_PASS)
|
||||
msg = (
|
||||
f"From: {FROM}\n"
|
||||
f"To: {ALERT_TO}\n"
|
||||
f"Subject: Apex Mail Watchdog Test\n\n"
|
||||
f"Test email from apex-mail-watchdog.py — {datetime.now().isoformat()}"
|
||||
)
|
||||
s.sendmail(FROM, [ALERT_TO], msg)
|
||||
return True, None
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
def check_debug_events():
|
||||
"""Check WP Mail SMTP debug table for recent failures (> 0 in last 10 min)."""
|
||||
try:
|
||||
conn = mysql.connector.connect(
|
||||
host="localhost",
|
||||
user="apextrackexperience_1781549652",
|
||||
password="K3E1ZZWvHDu0q8ZmoBCAhzKUZawEapdGBlbaPME1sOTKgGk9FCuYS",
|
||||
database="apextrackexperience_1781549652",
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
# Count errors (event_type=0) in last 10 minutes
|
||||
cursor.execute(
|
||||
"SELECT COUNT(*) FROM wp_wpmailsmtp_debug_events "
|
||||
"WHERE event_type = 0 AND created_at >= NOW() - INTERVAL 10 MINUTE"
|
||||
)
|
||||
count = cursor.fetchone()[0]
|
||||
conn.close()
|
||||
return count, None
|
||||
except Exception as e:
|
||||
return None, str(e)
|
||||
|
||||
def main():
|
||||
log("Apex mail watchdog check starting...")
|
||||
|
||||
smtp_ok, smtp_err = check_smtp()
|
||||
errors, debug_err = check_debug_events()
|
||||
|
||||
if not smtp_ok:
|
||||
log(f"SMTP FAILED: {smtp_err}")
|
||||
print(f"RESULT:FAIL|{smtp_err}")
|
||||
sys.exit(1)
|
||||
|
||||
if errors and errors > 0:
|
||||
log(f"DEBUG EVENTS: {errors} failures in last 10 min")
|
||||
print(f"RESULT:WARN|{errors} email failures detected")
|
||||
sys.exit(2)
|
||||
|
||||
if debug_err:
|
||||
log(f"DB ERROR: {debug_err}")
|
||||
# Don't fail on DB errors — SMTP test passed
|
||||
|
||||
log("SMTP OK — all clear")
|
||||
print("RESULT:OK|Mail delivery healthy")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user