From 5d653e0bc2efd6720e9d26c96929caa92b00d6f9 Mon Sep 17 00:00:00 2001 From: Michel Date: Sun, 13 Apr 2025 13:57:33 +0200 Subject: [PATCH] =?UTF-8?q?'=20r=C3=A9organisation=20""?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervisor_watchdog.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 supervisor_watchdog.py diff --git a/supervisor_watchdog.py b/supervisor_watchdog.py new file mode 100644 index 0000000..8dfdaaa --- /dev/null +++ b/supervisor_watchdog.py @@ -0,0 +1,53 @@ +import subprocess +import smtplib +from email.mime.text import MIMEText +from datetime import datetime + +# --- Config mail --- +EMAIL_FROM = "alertes_saclay@domo91.fr" +EMAIL_PASSWORD = "Kdpke674y23Feq^H" +EMAIL_TO = ["services@domo91.fr"] +SMTP_SERVER = "smtp.mail.ovh.net" +SMTP_PORT = 465 + +# --- Lister tous les processus --- +output = subprocess.getoutput("supervisorctl status") +lines = output.splitlines() + +# --- Analyse des statuts --- +anomalies = [] +rapport = [] + +for line in lines: + parts = line.split() + if len(parts) >= 2: + nom = parts[0] + statut = parts[1] + rapport.append(f"{nom:25} ➤ {statut}") + if statut != "RUNNING": + anomalies.append(f"{nom} ➤ {statut}") + +# --- Mail à envoyer --- +now = datetime.now().strftime("%Y-%m-%d %H:%M") + +if anomalies: + sujet = f"⚠️ Alerte Supervisor - {now}" + corps = "🛑 Les services suivants ne sont pas en RUNNING :\n\n" + "\n".join(anomalies) +else: + sujet = f"✅ Rapport quotidien Supervisor - {now}" + corps = "Tous les services supervisés sont en RUNNING.\n\n" + "\n".join(rapport) + +msg = MIMEText(corps) +msg["Subject"] = sujet +msg["From"] = EMAIL_FROM +msg["To"] = ", ".join(EMAIL_TO) + +# --- Envoi --- +try: + with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server: + server.login(EMAIL_FROM, EMAIL_PASSWORD) + server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) + print(f"📧 Mail envoyé : {sujet}") +except Exception as e: + print(f"Erreur envoi mail : {e}") +