Initialisation depuis le code en production
This commit is contained in:
45
scripts/supervisor_watchdog.py
Executable file
45
scripts/supervisor_watchdog.py
Executable 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("sudo /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"] = "supervisor@domo91.fr"
|
||||
msg["To"] = "services@domo91.fr"
|
||||
|
||||
try:
|
||||
with smtplib.SMTP_SSL("smtp.mail.ovh.net", 465) as server:
|
||||
server.login("services@domo91.fr", "6ZiCsVtSf9@nEHv@$^0")
|
||||
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 n’est pas l’heure du rapport).")
|
||||
Reference in New Issue
Block a user