Files
Gestion_sondes/supervisor_watchdog.py

45 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import subprocess
import smtplib
from email.mime.text import MIMEText
from datetime import datetime
heure_actuelle = datetime.now().strftime("%H:%M")
etat_services = []
anomalies = []
try:
output = subprocess.check_output("/usr/bin/supervisorctl status", shell=True, text=True)
for line in output.splitlines():
parts = line.split()
if len(parts) >= 2:
nom, statut = parts[0], parts[1]
etat_services.append(f"{nom}{statut}")
if statut != "RUNNING":
anomalies.append(f"{nom}{statut}")
except Exception as e:
etat_services.append("❌ Impossible d'exécuter supervisorctl")
anomalies.append(f"Erreur : {e}")
# Déclenchement mail si anomalie ou à 07:00
envoyer_mail = bool(anomalies) or heure_actuelle == "07:00"
if envoyer_mail:
sujet = "⚠️ Alerte Supervisor" if anomalies else "✅ Rapport quotidien Supervisor"
intro = "🛑 Les services suivants ne sont pas en RUNNING :" if anomalies else "✅ Tous les services supervisés sont en RUNNING."
contenu = f"{intro}\n\n" + "\n".join(etat_services)
msg = MIMEText(contenu)
msg["Subject"] = sujet
msg["From"] = "alertes_saclay@domo91.fr"
msg["To"] = "services@domo91.fr"
try:
with smtplib.SMTP_SSL("smtp.mail.ovh.net", 465) as server:
server.login("alertes_saclay@domo91.fr", "Kdpke674y23Feq^H")
server.sendmail(msg["From"], [msg["To"]], msg.as_string())
print("📧 Mail envoyé.")
except Exception as e:
print(f"Erreur envoi mail : {e}")
else:
print("🕖 Aucun mail envoyé (tout est OK et ce nest pas lheure du rapport).")