' réorganisation ""
This commit is contained in:
53
supervisor_watchdog.py
Normal file
53
supervisor_watchdog.py
Normal file
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user