From f2e86046d45f14564aa7e964f59945b3b6bd271c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drix?= Date: Mon, 16 Mar 2026 15:15:43 +0100 Subject: [PATCH] update-config --- servers/linux/monitoring/README.md | 4 +- .../monitoring/bin/monitor-update-config.sh | 90 +++++++++++++++++++ .../linux/monitoring/bin/monitoring-update.sh | 20 ++--- 3 files changed, 102 insertions(+), 12 deletions(-) create mode 100755 servers/linux/monitoring/bin/monitor-update-config.sh diff --git a/servers/linux/monitoring/README.md b/servers/linux/monitoring/README.md index e4efd5c..98b8dae 100644 --- a/servers/linux/monitoring/README.md +++ b/servers/linux/monitoring/README.md @@ -35,7 +35,7 @@ curl -sSL https://git.abonnel.fr/cedricAbonnel/scripts-bash/raw/branch/main/serv --- -## ⚙️ Configuration (Que modifier ?) +## Configuration (Que modifier ?) Après l'installation, vous devez configurer vos accès pour recevoir les alertes. @@ -52,7 +52,7 @@ Modifiez les variables suivantes : ### 2. Seuils des sondes -Vous pouvez modifier les variables `WARNING` et `CRITICAL` directement dans les scripts du dossier `bin/` ou, mieux, les définir dans `/opt/monitoring/conf/monitoring.conf`. +Vous pouvez modifier les variables `WARNING` et `CRITICAL` directement dans les scripts du dossier `bin/` ou, mieux, les définir dans `/opt/monitoring/conf/monitoring.local.conf`. --- diff --git a/servers/linux/monitoring/bin/monitor-update-config.sh b/servers/linux/monitoring/bin/monitor-update-config.sh new file mode 100755 index 0000000..42d18ec --- /dev/null +++ b/servers/linux/monitoring/bin/monitor-update-config.sh @@ -0,0 +1,90 @@ +#!/bin/bash +# Copyright (C) 2026 Cédric Abonnel +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + + +set -u + +SCRIPT_NAME="$(basename "$0")" +. /opt/monitoring/lib/monitoring-lib.sh || exit 3 + +# On s'assure d'avoir les permissions root +if [ "${EUID}" -ne 0 ]; then + echo "Ce script doit être exécuté en root." >&2 + exit 1 +fi + +check_config_drift() { + local conf_dir="/opt/monitoring/conf" + local base_conf local_conf + local found_issue=false + + log_info "audit_start" "Début de l'audit des configurations" + + # Parcourir tous les fichiers .conf officiels + find "$conf_dir" -type f -name "*.conf" ! -name "*.local.conf" | while read -r base_conf; do + local_conf="${base_conf%.conf}.local.conf" + local file_name + file_name=$(basename "$base_conf") + + # 1. Si le .local.conf n'existe pas : on le crée proprement + if [ ! -f "$local_conf" ]; then + log_notice "audit_missing_local" "Création du fichier local manquant" "file=$file_name" + # On copie le template en commentant les valeurs par défaut pour inciter à la config + cp "$base_conf" "$local_conf" + chmod 600 "$local_conf" + continue + fi + + # 2. Si le .local.conf existe : on compare les clés (options) + local tmp_base tmp_local + tmp_base=$(mktemp) + tmp_local=$(mktemp) + + # Extraction des noms de variables uniquement (Clés) + grep -E '^[A-Za-z0-9_]+=' "$base_conf" | cut -d'=' -f1 | sort > "$tmp_base" + grep -E '^[A-Za-z0-9_]+=' "$local_conf" | cut -d'=' -f1 | sort > "$tmp_local" + + # Options présentes dans le .conf mais absentes du .local.conf + local missing + missing=$(comm -23 "$tmp_base" "$tmp_local" | tr '\n' ' ' | xargs) + + if [ -n "$missing" ]; then + log_warning "audit_keys_missing" "Nouvelles options disponibles à configurer" \ + "file=${file_name%.conf}.local.conf" "keys=$missing" + found_issue=true + fi + + # Options présentes dans le .local.conf mais qui n'existent plus dans le .conf (Obsolètes) + local obsolete + obsolete=$(comm -13 "$tmp_base" "$tmp_local" | tr '\n' ' ' | xargs) + + if [ -n "$obsolete" ]; then + log_info "audit_keys_obsolete" "Options locales obsolètes détectées" \ + "file=${file_name%.conf}.local.conf" "keys=$obsolete" + fi + + rm -f "$tmp_base" "$tmp_local" + done + + if [ "$found_issue" = false ]; then + log_info "audit_success" "Toutes les configurations locales sont à jour" + fi +} + +main() { + lock_or_exit "monitoring-audit" + check_config_drift +} + +main +exit_with_status \ No newline at end of file diff --git a/servers/linux/monitoring/bin/monitoring-update.sh b/servers/linux/monitoring/bin/monitoring-update.sh index e8f6ba7..d28a830 100755 --- a/servers/linux/monitoring/bin/monitoring-update.sh +++ b/servers/linux/monitoring/bin/monitoring-update.sh @@ -166,21 +166,21 @@ delete_extra_local_files() { comm -23 "$TMP_LOCAL_LIST" "$TMP_REMOTE_LIST" | while IFS= read -r rel_path; do [ -n "$rel_path" ] || continue - case "$rel_path" in - conf/alert-engine.local.conf|conf/autoupdate.local.conf|conf/monitoring.local.conf) - log_notice "delete_skipped" \ - "Suppression ignorée pour fichier local protégé" \ - "file=$rel_path" - continue - ;; - esac + # Protection globale de TOUS les fichiers .local.conf + if [[ "$rel_path" == *.local.conf ]]; then + log_notice "delete_skipped" \ + "Fichier local protégé (ignoré)" \ + "file=$rel_path" + continue + fi + # Sécurité supplémentaire pour ne pas supprimer les répertoires vitaux rm -f "${MONITORING_BASE_DIR}/${rel_path}" \ && log_notice "file_deleted" \ - "Fichier supprimé car absent du manifeste" \ + "Fichier obsolète supprimé" \ "file=$rel_path" \ || log_error "delete_failed" \ - "Impossible de supprimer le fichier local absent du manifeste" \ + "Échec suppression" \ "file=$rel_path" done }