journalisation des fichiers déployés

This commit is contained in:
2026-03-20 08:49:42 +01:00
parent 5e3b5ee741
commit 4e7119297c
2 changed files with 48 additions and 34 deletions

View File

@@ -11,6 +11,8 @@ LOG_DIR="/var/log/monitoring"
STATE_DIR="/var/lib/monitoring" STATE_DIR="/var/lib/monitoring"
LOCK_DIR="/var/lock/monitoring" LOCK_DIR="/var/lock/monitoring"
TMP_DIR="/tmp/monitoring-install" TMP_DIR="/tmp/monitoring-install"
# Journal des fichiers installés pour déinstallation/état des lieux
INSTALLED_LOG="${STATE_DIR}/installed-files.log"
UPDATE_BASE_URL="https://git.abonnel.fr/cedricAbonnel/scripts-bash/raw/branch/main/servers/linux/monitoring" UPDATE_BASE_URL="https://git.abonnel.fr/cedricAbonnel/scripts-bash/raw/branch/main/servers/linux/monitoring"
MANIFEST_URL="${UPDATE_BASE_URL}/manifest.txt" MANIFEST_URL="${UPDATE_BASE_URL}/manifest.txt"
@@ -37,9 +39,7 @@ install_deps() {
info "Vérification des dépendances système..." info "Vérification des dépendances système..."
if command -v apt-get >/dev/null 2>&1; then if command -v apt-get >/dev/null 2>&1; then
apt-get update -qq apt-get update -qq
apt-get install -y -qq curl coreutils findutils grep sed gawk util-linux ca-certificates php-cli > /dev/null apt-get install -y -qq curl coreutils findutils grep sed gawk util-linux ca-certificates php-cli php-curl php-common smartmontools > /dev/null
apt-get install -y -qq php-cli php-curl php-common > /dev/null
apt-get install -y -qq smartmontools > /dev/null
ok "Dépendances installées." ok "Dépendances installées."
fi fi
} }
@@ -47,6 +47,7 @@ install_deps() {
prepare_dirs() { prepare_dirs() {
info "Préparation de l'arborescence dans ${BASE_DIR}..." info "Préparation de l'arborescence dans ${BASE_DIR}..."
mkdir -p "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${CONF_DIR}" "${LOG_DIR}" "${STATE_DIR}" "${LOCK_DIR}" "${TMP_DIR}" mkdir -p "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${CONF_DIR}" "${LOG_DIR}" "${STATE_DIR}" "${LOCK_DIR}" "${TMP_DIR}"
touch "$INSTALLED_LOG"
} }
fetch_manifest() { fetch_manifest() {
@@ -65,17 +66,13 @@ validate_manifest() {
} }
download_and_install() { download_and_install() {
local expected_hash="$1" local expected_hash=$1 mode=$2 rel_path=$3
local mode="$2"
local rel_path="$3"
local dst="${BASE_DIR}/${rel_path}" local dst="${BASE_DIR}/${rel_path}"
if [ -f "$dst" ]; then if [ -f "$dst" ]; then
local current_hash local current_hash
current_hash=$(sha256sum "$dst" | awk '{print $1}') current_hash=$(sha256sum "$dst" | awk '{print $1}')
if [ "$current_hash" == "$expected_hash" ]; then [ "$current_hash" == "$expected_hash" ] && return 0
return 0 # Déjà à jour
fi
info "Mise à jour : $rel_path" info "Mise à jour : $rel_path"
else else
info "Installation : $rel_path" info "Installation : $rel_path"
@@ -103,23 +100,45 @@ download_and_install() {
chmod "$mode" "$dst" chmod "$mode" "$dst"
} }
# --- NOUVEAUTÉ : Gestion du journal et purge propre ---
update_installed_log() {
# On sauvegarde la liste des chemins relatifs du manifeste validé dans le journal permanent
awk '{print $3}' "${TMP_DIR}/manifest-valid.txt" > "$INSTALLED_LOG"
ok "Journal des fichiers déployés mis à jour ($INSTALLED_LOG)."
}
purge_obsolete_files() { purge_obsolete_files() {
info "Analyse des fichiers obsolètes (Synchronisation avec Git)..." info "Analyse des fichiers obsolètes (Synchronisation avec le journal)..."
# On scanne bin, lib et conf
# On compare ce qui était installé (journal) avec ce qui est dans le nouveau manifeste
if [ ! -s "$INSTALLED_LOG" ]; then
warn "Journal vide, passage en mode scan classique."
# Fallback sur le scan de dossier si le journal n'existe pas encore
find "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${BASE_DIR}/conf" -type f 2>/dev/null | while read -r local_file; do find "${BASE_DIR}/bin" "${BASE_DIR}/lib" "${BASE_DIR}/conf" -type f 2>/dev/null | while read -r local_file; do
local rel_path="${local_file#$BASE_DIR/}" local rel_path="${local_file#$BASE_DIR/}"
[[ "$rel_path" == *".local."* ]] && continue
# PROTECTION : On ne touche jamais aux fichiers locaux personnels
if [[ "$rel_path" == *".local."* ]]; then
continue
fi
if ! grep -qw "$rel_path" "${TMP_DIR}/manifest-valid.txt"; then if ! grep -qw "$rel_path" "${TMP_DIR}/manifest-valid.txt"; then
warn "Suppression : $rel_path (absent du dépôt)" warn "Suppression : $rel_path"
rm -f "$local_file" rm -f "$local_file"
fi fi
done done
return
fi
# Mode Journal : On lit l'ancien journal pour voir ce qui doit disparaître
while read -r old_file; do
# Si le fichier du journal n'est plus dans le nouveau manifeste
if ! grep -qw "$old_file" "${TMP_DIR}/manifest-valid.txt"; then
if [ -f "${BASE_DIR}/$old_file" ]; then
# Protection ultime des fichiers .local (au cas où ils auraient été loggués par erreur)
if [[ "$old_file" != *".local."* ]]; then
warn "Suppression du fichier obsolète : $old_file"
rm -f "${BASE_DIR}/$old_file"
fi
fi
fi
done < "$INSTALLED_LOG"
} }
# --- Main --- # --- Main ---
@@ -143,8 +162,9 @@ main() {
done < "${TMP_DIR}/manifest-valid.txt" done < "${TMP_DIR}/manifest-valid.txt"
echo "--------------------------------------------------" echo "--------------------------------------------------"
info "Phase 2 : Nettoyage" info "Phase 2 : Nettoyage et Journalisation"
purge_obsolete_files purge_obsolete_files
update_installed_log
rm -rf "${TMP_DIR}" rm -rf "${TMP_DIR}"
echo "--------------------------------------------------" echo "--------------------------------------------------"
@@ -155,18 +175,12 @@ main() {
orig_conf="${CONF_DIR}/monitoring.conf.php" orig_conf="${CONF_DIR}/monitoring.conf.php"
if [ -f "$local_conf" ] && [ -f "$orig_conf" ]; then if [ -f "$local_conf" ] && [ -f "$orig_conf" ]; then
# On compare les hashs des deux fichiers if [ "$(sha256sum "$local_conf" | awk '{print $1}')" == "$(sha256sum "$orig_conf" | awk '{print $1}')" ]; then
hash_local=$(sha256sum "$local_conf" | awk '{print $1}')
hash_orig=$(sha256sum "$orig_conf" | awk '{print $1}')
if [ "$hash_local" == "$hash_orig" ]; then
echo -e "\n\e[33m[ATTENTION]\e[0m Votre fichier de configuration est identique à l'original." echo -e "\n\e[33m[ATTENTION]\e[0m Votre fichier de configuration est identique à l'original."
warn "Pensez à éditer ${local_conf} pour configurer vos tokens ntfy/mail." warn "Pensez à éditer ${local_conf}."
else else
ok "Configuration locale détectée et personnalisée." ok "Configuration locale personnalisée détectée."
fi fi
else
warn "Fichier de configuration local absent. Relancez monitoring-update.php pour l'initialiser."
fi fi
} }

View File

@@ -1,7 +1,7 @@
5b4ea784d2cbe73f6e829e35f23b0b4dbe12df55cc1abc8eba6602da36c724ef 755 bin/alert-engine.php 5b4ea784d2cbe73f6e829e35f23b0b4dbe12df55cc1abc8eba6602da36c724ef 755 bin/alert-engine.php
fdcea6720186795538f48c08b99103b320273dbdd0ea5246a2da9d81a1eecc6c 755 bin/check_disk.sh fdcea6720186795538f48c08b99103b320273dbdd0ea5246a2da9d81a1eecc6c 755 bin/check_disk.sh
ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_smart.sh ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_smart.sh
9cf677bb0a598adb5f5311690ad9667979e5bbc4c8aa372a0fc140bf710c8762 755 bin/install-monitoring.sh 8f95824b568b5de7dbdc2d6ab87fc6fd8076dcb8ad20de3e72a53391e97f8484 755 bin/install-monitoring.sh
97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php 97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php
02bd43ed2a9b92acc013274c716e6bc50120a8103ccf3d9c4e6f345a0b22d6a0 755 bin/monitoring.php 02bd43ed2a9b92acc013274c716e6bc50120a8103ccf3d9c4e6f345a0b22d6a0 755 bin/monitoring.php
97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php 97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php