🔥 Règlage du fichier supervisor_watchdog.py
This commit is contained in:
@@ -1,45 +1,44 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import smtplib
|
import smtplib
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
# Récupère tous les services via supervisorctl
|
heure_actuelle = datetime.now().strftime("%H:%M")
|
||||||
try:
|
|
||||||
output = subprocess.getoutput("/usr/bin/supervisorctl status") # adapte le chemin si besoin
|
|
||||||
except Exception as e:
|
|
||||||
output = f"Erreur récupération supervisorctl: {e}"
|
|
||||||
|
|
||||||
# Liste tous les états
|
|
||||||
etat_services = []
|
etat_services = []
|
||||||
anomalies = []
|
anomalies = []
|
||||||
|
|
||||||
for line in output.splitlines():
|
try:
|
||||||
|
output = subprocess.check_output("/usr/bin/supervisorctl status", shell=True, text=True)
|
||||||
|
for line in output.splitlines():
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) >= 2:
|
if len(parts) >= 2:
|
||||||
nom, statut = parts[0], parts[1]
|
nom, statut = parts[0], parts[1]
|
||||||
etat_services.append(f"{nom} ➤ {statut}")
|
etat_services.append(f"{nom} ➤ {statut}")
|
||||||
if statut != "RUNNING":
|
if statut != "RUNNING":
|
||||||
anomalies.append(f"{nom} ➤ {statut}")
|
anomalies.append(f"{nom} ➤ {statut}")
|
||||||
|
except Exception as e:
|
||||||
|
etat_services.append("❌ Impossible d'exécuter supervisorctl")
|
||||||
|
anomalies.append(f"Erreur : {e}")
|
||||||
|
|
||||||
# Prépare le message
|
# Déclenchement mail si anomalie ou à 07:00
|
||||||
if anomalies:
|
envoyer_mail = bool(anomalies) or heure_actuelle == "07:00"
|
||||||
sujet = "⚠️ Alerte Supervisor"
|
|
||||||
intro = "🛑 Les services suivants ne sont pas en RUNNING :"
|
|
||||||
else:
|
|
||||||
sujet = "✅ Rapport quotidien Supervisor"
|
|
||||||
intro = "✅ Tous les services supervisés sont en RUNNING."
|
|
||||||
|
|
||||||
contenu = f"{intro}\n\n" + "\n".join(etat_services)
|
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)
|
||||||
|
|
||||||
# Prépare et envoie le mail
|
msg = MIMEText(contenu)
|
||||||
msg = MIMEText(contenu)
|
msg["Subject"] = sujet
|
||||||
msg["Subject"] = sujet
|
msg["From"] = "alertes_saclay@domo91.fr"
|
||||||
msg["From"] = "alertes_saclay@domo91.fr"
|
msg["To"] = "services@domo91.fr"
|
||||||
msg["To"] = "services@domo91.fr"
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with smtplib.SMTP_SSL("smtp.mail.ovh.net", 465) as server:
|
with smtplib.SMTP_SSL("smtp.mail.ovh.net", 465) as server:
|
||||||
server.login("alertes_saclay@domo91.fr", "Kdpke674y23Feq^H")
|
server.login("alertes_saclay@domo91.fr", "Kdpke674y23Feq^H")
|
||||||
server.sendmail(msg["From"], [msg["To"]], msg.as_string())
|
server.sendmail(msg["From"], [msg["To"]], msg.as_string())
|
||||||
print("📧 Mail envoyé.")
|
print("📧 Mail envoyé.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Erreur envoi mail : {e}")
|
print(f"Erreur envoi mail : {e}")
|
||||||
|
else:
|
||||||
|
print("🕖 Aucun mail envoyé (tout est OK et ce n’est pas l’heure du rapport).")
|
||||||
|
|||||||
Reference in New Issue
Block a user