Mise à jour de la branche product avec les fichiers en production sur le VPS

This commit is contained in:
root
2025-05-07 08:15:12 +02:00
parent 1cc6a56dfa
commit 96ffdc7eb1
8 changed files with 123 additions and 39 deletions

45
supervisor_watchdog.py Executable file
View File

@@ -0,0 +1,45 @@
#!/home/debian/Gestion_sondes/myenv/bin/python
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).")