modification des channels
This commit is contained in:
@@ -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) {
|
||||
|
||||
// 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
|
||||
*/
|
||||
|
||||
@@ -42,16 +42,23 @@ return [
|
||||
|
||||
// --- Canaux par défaut selon le niveau ---
|
||||
'DEFAULT_CHANNELS' => [
|
||||
'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 ---
|
||||
|
||||
Reference in New Issue
Block a user