From 763bc0ba4807fb26ee011962e76dedbf7d2d270c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9drix?= Date: Tue, 17 Mar 2026 07:46:24 +0100 Subject: [PATCH] modification des channels --- servers/linux/monitoring/bin/alert-engine.php | 76 +++++++++++++++---- .../monitoring/conf/alert-engine.conf.php | 21 +++-- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/servers/linux/monitoring/bin/alert-engine.php b/servers/linux/monitoring/bin/alert-engine.php index 6c80ec2..d1a0658 100755 --- a/servers/linux/monitoring/bin/alert-engine.php +++ b/servers/linux/monitoring/bin/alert-engine.php @@ -9,7 +9,7 @@ require_once __DIR__ . '/../lib/monitoring-lib.php'; // --- Initialisation de la configuration spécifique --- -// On charge les fichiers de conf s'ils existent (format PHP attendu) +// On charge les fichiers de conf s'ils existent foreach (["/opt/monitoring/conf/alert-engine.conf.php", "/opt/monitoring/conf/alert-engine.conf.local.php"] as $conf) { if (file_exists($conf)) { $extra_conf = include $conf; @@ -30,50 +30,98 @@ ensure_parent_dir($STATE_FILE); ensure_parent_dir($DEDUP_FILE); /** - * Nettoyage du fichier de déduplication (entrées expirées) + * Nettoyage du fichier de déduplication + * Supprime les entrées plus vieilles que la fenêtre de déduplication ($DEDUP_WINDOW) */ function cleanup_dedup_file() { global $DEDUP_FILE, $DEDUP_WINDOW; - if (!file_exists($DEDUP_FILE)) return; + + // Si le fichier n'existe pas, rien à nettoyer + if (!file_exists($DEDUP_FILE)) { + return; + } $now = time(); - $lines = file($DEDUP_FILE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $kept = []; + $has_changed = false; + + // Lecture du fichier + $lines = file($DEDUP_FILE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if ($lines === false) { + return; + } foreach ($lines as $line) { $parts = explode('|', $line); - if (count($parts) >= 2 && ($now - (int)$parts[1]) <= $DEDUP_WINDOW) { - $kept[] = $line; + + // On vérifie si la ligne est valide et si le timestamp (index 1) est encore dans la fenêtre + if (count($parts) >= 2) { + $timestamp = (int)$parts[1]; + if (($now - $timestamp) <= (int)$DEDUP_WINDOW) { + $kept[] = $line; + } else { + $has_changed = true; // On a trouvé au moins une ligne à supprimer + } } } - file_put_contents($DEDUP_FILE, implode("\n", $kept) . (empty($kept) ? "" : "\n")); + + // On ne réécrit le fichier que si des lignes ont été supprimées + if ($has_changed) { + $content = implode("\n", $kept); + if (!empty($content)) { + $content .= "\n"; + } + + // LOCK_EX évite que deux instances n'écrivent en même temps + file_put_contents($DEDUP_FILE, $content, LOCK_EX); + } } + /** * Vérifie si une alerte doit être envoyée (Déduplication) + * La clé attendue est : "hostname|app|level|event" */ -function should_notify_dedup($key) { +function should_notify_dedup(string $key): bool { global $DEDUP_FILE, $DEDUP_WINDOW; - if (!file_exists($DEDUP_FILE)) return true; + + if (!file_exists($DEDUP_FILE)) { + return true; + } $now = time(); $last_ts = 0; - + $handle = fopen($DEDUP_FILE, 'r'); + if (!$handle) { + return true; // En cas d'erreur de lecture, on autorise l'alerte par sécurité + } + + // On parcourt le fichier while (($line = fgets($handle)) !== false) { - $p = explode('|', trim($line)); + $line = trim($line); + if (empty($line)) continue; + + $p = explode('|', $line); + + // Format du fichier : host|timestamp|app|level|event + // On reconstruit la clé de comparaison (sans le timestamp index 1) if (count($p) >= 5) { - $current_key = "{$p[0]}|{$p[2]}|{$p[3]}|{$p[4]}"; - if ($current_key === $key) { + $row_key = "{$p[0]}|{$p[2]}|{$p[3]}|{$p[4]}"; + + if ($row_key === $key) { $last_ts = (int)$p[1]; } } } fclose($handle); - return ($now - $last_ts) >= $DEDUP_WINDOW; + // Calcul de l'écart : vrai si on a dépassé la fenêtre ou si jamais vu (last_ts = 0) + return ($now - $last_ts) >= (int)$DEDUP_WINDOW; } + + /** * Envoi vers ntfy */ diff --git a/servers/linux/monitoring/conf/alert-engine.conf.php b/servers/linux/monitoring/conf/alert-engine.conf.php index 34dcd51..a4d50c2 100644 --- a/servers/linux/monitoring/conf/alert-engine.conf.php +++ b/servers/linux/monitoring/conf/alert-engine.conf.php @@ -42,16 +42,23 @@ return [ // --- Canaux par défaut selon le niveau --- 'DEFAULT_CHANNELS' => [ - 'WARNING' => 'ntfy', - 'ERROR' => 'ntfy,mail', - 'CRITICAL' => 'ntfy,mail', + 'INFO' => 'ntfy', + 'NOTICE' => 'ntfy', + 'WARNING' => 'ntfy', + 'ERROR' => 'ntfy,mail', + 'CRITICAL' => 'ntfy,mail', ], - // --- Tags ntfy par niveau --- + // --- Tags ntfy (Liste exhaustive par niveau) --- 'NTFY_TAGS' => [ - 'WARNING' => 'warning', - 'ERROR' => 'warning,rotating_light', - 'CRITICAL' => 'skull,warning', + 'INFO' => 'information_source', // ℹ️ + 'NOTICE' => 'bell', // 🔔 + 'SUCCESS' => 'white_check_mark', // ✅ + 'WARNING' => 'warning', // ⚠️ + 'ERROR' => 'rotating_light,warning', // 🚨 + 'CRITICAL' => 'skull,warning', // 💀 + 'EMERGENCY' => 'fire,sos,skull', // 🔥 + 'DEBUG' => 'gear', // ⚙️ ], // --- Règles spécifiques par événement ---