🔥 Suppression du fichier inutile Alerte_journalière.py

This commit is contained in:
2025-04-13 14:48:52 +02:00
parent 5d653e0bc2
commit 5b1ce58ef4

View File

@@ -1,53 +1,45 @@
import subprocess import subprocess
import smtplib import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
from datetime import datetime
# --- Config mail --- # Récupère tous les services via supervisorctl
EMAIL_FROM = "alertes_saclay@domo91.fr" try:
EMAIL_PASSWORD = "Kdpke674y23Feq^H" output = subprocess.getoutput("/usr/bin/supervisorctl status") # adapte le chemin si besoin
EMAIL_TO = ["services@domo91.fr"] except Exception as e:
SMTP_SERVER = "smtp.mail.ovh.net" output = f"Erreur récupération supervisorctl: {e}"
SMTP_PORT = 465
# --- Lister tous les processus --- # Liste tous les états
output = subprocess.getoutput("supervisorctl status") etat_services = []
lines = output.splitlines()
# --- Analyse des statuts ---
anomalies = [] anomalies = []
rapport = []
for line in lines: for line in output.splitlines():
parts = line.split() parts = line.split()
if len(parts) >= 2: if len(parts) >= 2:
nom = parts[0] nom, statut = parts[0], parts[1]
statut = parts[1] etat_services.append(f"{nom}{statut}")
rapport.append(f"{nom:25}{statut}")
if statut != "RUNNING": if statut != "RUNNING":
anomalies.append(f"{nom}{statut}") anomalies.append(f"{nom}{statut}")
# --- Mail à envoyer --- # Prépare le message
now = datetime.now().strftime("%Y-%m-%d %H:%M")
if anomalies: if anomalies:
sujet = f"⚠️ Alerte Supervisor - {now}" sujet = "⚠️ Alerte Supervisor"
corps = "🛑 Les services suivants ne sont pas en RUNNING :\n\n" + "\n".join(anomalies) intro = "🛑 Les services suivants ne sont pas en RUNNING :"
else: else:
sujet = f"✅ Rapport quotidien Supervisor - {now}" sujet = "✅ Rapport quotidien Supervisor"
corps = "Tous les services supervisés sont en RUNNING.\n\n" + "\n".join(rapport) intro = "Tous les services supervisés sont en RUNNING."
msg = MIMEText(corps) contenu = f"{intro}\n\n" + "\n".join(etat_services)
# Prépare et envoie le mail
msg = MIMEText(contenu)
msg["Subject"] = sujet msg["Subject"] = sujet
msg["From"] = EMAIL_FROM msg["From"] = "alertes_saclay@domo91.fr"
msg["To"] = ", ".join(EMAIL_TO) msg["To"] = "services@domo91.fr"
# --- Envoi ---
try: try:
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server: with smtplib.SMTP_SSL("smtp.mail.ovh.net", 465) as server:
server.login(EMAIL_FROM, EMAIL_PASSWORD) server.login("alertes_saclay@domo91.fr", "Kdpke674y23Feq^H")
server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) server.sendmail(msg["From"], [msg["To"]], msg.as_string())
print(f"📧 Mail envoyé : {sujet}") print("📧 Mail envoyé.")
except Exception as e: except Exception as e:
print(f"Erreur envoi mail : {e}") print(f"Erreur envoi mail : {e}")