From 52f3c2ee6a38eaa548cdd293ae477c8bf9023bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drix?= Date: Sun, 8 Mar 2026 22:59:20 +0100 Subject: [PATCH] modification envoi mail --- servers/linux/config_adminSys.sh | 66 ++++++++++++++++++++++---------- servers/linux/sys_check.sh | 18 +++++++++ 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/servers/linux/config_adminSys.sh b/servers/linux/config_adminSys.sh index c126c0f..953a3da 100755 --- a/servers/linux/config_adminSys.sh +++ b/servers/linux/config_adminSys.sh @@ -10,16 +10,46 @@ fi # --- 1. CONFIGURATION DYNAMIQUE --- HOSTNAME=$(hostname) -SMTP_HOST="mail.acemail.fr" -SMTP_PORT="587" -SMTP_USER="srv@a5l.fr" -DEST_EMAIL="cedric+${HOSTNAME}@abonnel.fr" + +MSMTP_CONF="/etc/msmtprc" + +# Valeurs "Fallback" (anonymisées) +DEFAULT_SMTP_HOST="smtp.exemple.fr" +DEFAULT_SMTP_PORT="587" +DEFAULT_SMTP_USER="alerte@exemple.fr" +DEFAULT_DEST_EMAIL="admin+${HOSTNAME}@exemple.fr" SUBJECT_PREFIX="[$HOSTNAME]" +# Tentative d'extraction des valeurs actuelles si le fichier existe +if [ -f "$MSMTP_CONF" ]; then + CURRENT_HOST=$(grep -E "^host[[:space:]]+" "$MSMTP_CONF" | awk '{print $2}') + CURRENT_PORT=$(grep -E "^port[[:space:]]+" "$MSMTP_CONF" | awk '{print $2}') + CURRENT_USER=$(grep -E "^user[[:space:]]+" "$MSMTP_CONF" | awk '{print $2}') + + # Si on a trouvé des infos, on les utilise comme nouvelles valeurs par défaut + [ -n "$CURRENT_HOST" ] && DEFAULT_SMTP_HOST="$CURRENT_HOST" + [ -n "$CURRENT_PORT" ] && DEFAULT_SMTP_PORT="$CURRENT_PORT" + [ -n "$CURRENT_USER" ] && DEFAULT_SMTP_USER="$CURRENT_USER" +fi + echo "==========================================================" echo " VÉRIFICATION SMTP & DÉPLOIEMENT - ${HOSTNAME}" echo "==========================================================" +# Fonction pour demander une valeur avec une valeur par défaut +prompt_value() { + local var_name=$1 + local prompt_text=$2 + local default_value=$3 + read -p "$prompt_text [$default_value] : " input + eval "$var_name=\"${input:-$default_value}\"" +} + +# Collecte interactive +prompt_value "SMTP_HOST" "Serveur SMTP" "$DEFAULT_SMTP_HOST" +prompt_value "SMTP_PORT" "Port SMTP" "$DEFAULT_SMTP_PORT" +prompt_value "SMTP_USER" "Utilisateur SMTP" "$DEFAULT_SMTP_USER" +prompt_value "DEST_EMAIL" "Email de destination" "$DEFAULT_DEST_EMAIL" # --- 2. INSTALLATION INITIALE --- DEBIAN_FRONTEND=noninteractive apt update @@ -29,22 +59,17 @@ DEBIAN_FRONTEND=noninteractive apt install -y curl bc # --- 3. RÉCUPÉRATION ET TEST DU MOT DE PASSE SMTP --- -# Ici, PASS_FILE_ORI pointe probablement vers /etc/msmtprc ou une sauvegarde -PASS_FILE_ORI="/etc/msmtprc" AUTH_OK=false while [ "$AUTH_OK" = false ]; do # 1. Tentative d'extraction depuis le fichier de config existant - if [ -f "$PASS_FILE_ORI" ]; then - # On cherche la ligne commençant par 'password', on ignore les espaces/tabulations, - # et on récupère la deuxième colonne. - SMTP_PASS=$(grep -E "^password[[:space:]]+" "$PASS_FILE_ORI" | awk '{print $2}') + if [ -f "$MSMTP_CONF" ] && [ "$SMTP_USER" = "$CURRENT_USER" ]; then + SMTP_PASS=$(grep -E "^password[[:space:]]+" "$MSMTP_CONF" | awk '{print $2}') fi if [ -n "$SMTP_PASS" ]; then - echo "📄 Mot de passe extrait depuis $PASS_FILE_ORI." + echo "📄 Mot de passe existant récupéré pour $SMTP_USER." else - # 2. Sinon, saisie manuelle si le fichier n'existe pas ou ne contient pas de password echo -n "🔑 Mot de passe SMTP non trouvé. Entrez le pour ${SMTP_USER} : " read -s SMTP_PASS echo "" @@ -53,7 +78,8 @@ while [ "$AUTH_OK" = false ]; do echo "⏳ Vérification de la connexion SMTP..." # Création config temporaire pour le test - cat > /tmp/.msmtp_test < "$TMP_TEST" </dev/null + echo "Test de configuration" | msmtp --file="$TMP_TEST" -t "$DEST_EMAIL" 2>/dev/null if [ $? -eq 0 ]; then echo "✅ Authentification SMTP réussie !" AUTH_OK=true - rm /tmp/.msmtp_test + rm -f "$TMP_TEST" else echo "❌ Échec de l'authentification SMTP." # Si le password extrait était mauvais, on vide la variable pour forcer la saisie au prochain tour - if [ -f "$PASS_FILE_ORI" ]; then - echo "⚠️ Le mot de passe extrait de $PASS_FILE_ORI est incorrect." + if [ -f "$MSMTP_CONF" ]; then + echo "⚠️ Le mot de passe extrait de $MSMTP_CONF est incorrect." SMTP_PASS="" - PASS_FILE_ORI="/dev/null" # On "désactive" la lecture fichier pour ce tour + MSMTP_CONF="/dev/null" # On "désactive" la lecture fichier pour ce tour fi - rm /tmp/.msmtp_test + rm -f "$TMP_TEST" fi done diff --git a/servers/linux/sys_check.sh b/servers/linux/sys_check.sh index f7631b6..9558909 100644 --- a/servers/linux/sys_check.sh +++ b/servers/linux/sys_check.sh @@ -111,6 +111,9 @@ if [ -n "$SWAP_TOTAL" ] && [ "$SWAP_TOTAL" -gt 0 ]; then fi fi +add_to_report "---" + + # 3. CHECK CHARGE CPU CPU_LOAD=$(uptime | awk -F'load average:' '{ print $2 }' | cut -d',' -f1 | tr -d ' ' | tr ',' '.') if (( $(echo "$CPU_LOAD > $LOAD_THRESHOLD" | bc -l) )); then @@ -126,6 +129,9 @@ for SVC in "ssh" "fail2ban"; do fi done +add_to_report "---" + + # ------------------------------------------------ # 5. NOMBRE DE PROCESSUS # ------------------------------------------------ @@ -136,6 +142,9 @@ if [ "$PROC_COUNT" -gt "$PROC_THRESHOLD" ]; then ALERT=true fi +add_to_report "---" + + # ------------------------------------------------ # 6. ESPACE /tmp # ------------------------------------------------ @@ -146,6 +155,10 @@ if [ "$TMP_USAGE" -gt "$TMP_THRESHOLD" ]; then ALERT=true fi +add_to_report "---" + + + # ------------------------------------------------ # 7. PROCESSUS ZOMBIES # ------------------------------------------------ @@ -174,6 +187,7 @@ if (( DELETED_COUNT > 0 )); then ALERT=true fi +add_to_report "---" # ------------------------------------------------ @@ -186,6 +200,8 @@ if [ "$CONN_COUNT" -gt "$CONN_THRESHOLD" ]; then ALERT=true fi +add_to_report "---" + # ------------------------------------------------ # 10. ERREURS CRITIQUES (Dernière heure) @@ -202,6 +218,8 @@ if (( LOG_COUNT > 0 )); then ALERT=true fi +add_to_report "---" + # ------------------------------------------------ # ENVOI MAIL