simplificaiton config
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
# Configuration locale
|
# --- Configuration (Seuils par défaut) ---
|
||||||
WARNING=80
|
WARNING=80
|
||||||
CRITICAL=95
|
CRITICAL=95
|
||||||
MOUNTS=("/" "/var" "/home")
|
MOUNTS=("/" "/var" "/home")
|
||||||
@@ -29,36 +29,33 @@ fi
|
|||||||
for mount in "${MOUNTS[@]}"; do
|
for mount in "${MOUNTS[@]}"; do
|
||||||
if ! mountpoint -q "$mount"; then continue; fi
|
if ! mountpoint -q "$mount"; then continue; fi
|
||||||
|
|
||||||
|
# --- 1. Espace Disque ---
|
||||||
used_pct="$(df -P "$mount" 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}')"
|
used_pct="$(df -P "$mount" 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}')"
|
||||||
|
|
||||||
if [[ ! "$used_pct" =~ ^[0-9]+$ ]]; then
|
if [[ ! "$used_pct" =~ ^[0-9]+$ ]]; then
|
||||||
$LOG_BIN ERROR "check_failed" "Erreur lecture disque $mount."
|
$LOG_BIN ERROR "check_failed" "Erreur lecture disque $mount."
|
||||||
continue
|
else
|
||||||
|
if [ "$used_pct" -ge "$CRITICAL" ]; then
|
||||||
|
$LOG_BIN CRITICAL "disk_usage_critical" "Disque $mount critique : $used_pct% utilisé."
|
||||||
|
elif [ "$used_pct" -ge "$WARNING" ]; then
|
||||||
|
$LOG_BIN WARNING "disk_usage_high" "Disque $mount élevé : $used_pct% utilisé."
|
||||||
|
else
|
||||||
|
$LOG_BIN INFO "disk_ok" "Disque $mount OK : $used_pct% utilisé."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Logique de décision
|
# --- 2. Inodes (Déplacé à l'intérieur de la boucle) ---
|
||||||
if [ "$used_pct" -ge "$CRITICAL" ]; then
|
inode_pct="$(df -iP "$mount" 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}')"
|
||||||
$LOG_BIN CRITICAL "disk_usage_critical" "Disque $mount critique. $used_pct% utilisé."
|
|
||||||
|
|
||||||
$LOG_BIN WARNING "disk_usage_high" "Disque $mount élevé. $used_pct% utilisé."
|
if [[ ! "$inode_pct" =~ ^[0-9]+$ ]]; then
|
||||||
|
$LOG_BIN ERROR "check_failed" "Erreur lecture inodes $mount."
|
||||||
else
|
else
|
||||||
$LOG_BIN INFO "disk_ok" "Disque $mount OK. $used_pct% utilisé."
|
if [ "$inode_pct" -ge "$CRITICAL" ]; then
|
||||||
|
$LOG_BIN CRITICAL "inode_usage_critical" "Inodes $mount critiques ($inode_pct%)."
|
||||||
|
elif [ "$inode_pct" -ge "$WARNING" ]; then
|
||||||
|
$LOG_BIN WARNING "inode_usage_high" "Inodes $mount élevés ($inode_pct%)."
|
||||||
|
else
|
||||||
|
$LOG_BIN INFO "inode_ok" "Inodes $mount OK ($inode_pct%)."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# --- 2. Étude des Inodes ---
|
|
||||||
# df -i récupère l'utilisation des inodes
|
|
||||||
inode_pct="$(df -iP "$mount" 2>/dev/null | awk 'NR==2 {gsub("%","",$5); print $5}')"
|
|
||||||
|
|
||||||
if [[ ! "$inode_pct" =~ ^[0-9]+$ ]]; then
|
|
||||||
$LOG_BIN ERROR "check_failed" "Erreur lecture inodes $mount."
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$inode_pct" -ge "$CRITICAL" ]; then
|
|
||||||
$LOG_BIN CRITICAL "inode_usage_critical" "Inodes $mount critiques ($inode_pct% utilisé)."
|
|
||||||
elif [ "$inode_pct" -ge "$WARNING" ]; then
|
|
||||||
$LOG_BIN WARNING "inode_usage_high" "Inodes $mount élevés ($inode_pct% utilisé)."
|
|
||||||
else
|
|
||||||
$LOG_BIN INFO "inode_ok" "Inodes $mount OK ($inode_pct% utilisé)."
|
|
||||||
fi
|
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Moteur de mise à jour - Version Simplifiée et Autonome
|
* Moteur de mise à jour - Version Supervisor
|
||||||
* Pilotage du script Bash + Initialisation des Configs + Cron
|
* Pilotage du script Bash + Initialisation des Configs + Cron
|
||||||
|
* Copyright (C) 2026 Cédric Abonnel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once __DIR__ . '/../lib/monitoring-lib.php';
|
require_once __DIR__ . '/../lib/monitoring-lib.php';
|
||||||
|
|
||||||
// Sécurité : Un seul update à la fois
|
// Sécurité : Un seul update à la fois
|
||||||
@@ -19,7 +21,8 @@ if (!file_exists($install_script)) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Exécution du moteur de synchronisation Bash
|
// 1. Exécution du moteur de synchronisation Bash (il gère téléchargement et purge)
|
||||||
|
// On utilise popen pour lire la sortie en temps réel à l'écran
|
||||||
$command = "bash " . escapeshellarg($install_script) . " --auto 2>&1";
|
$command = "bash " . escapeshellarg($install_script) . " --auto 2>&1";
|
||||||
$handle = popen($command, 'r');
|
$handle = popen($command, 'r');
|
||||||
|
|
||||||
@@ -27,9 +30,9 @@ if ($handle) {
|
|||||||
while (!feof($handle)) {
|
while (!feof($handle)) {
|
||||||
$line = fgets($handle);
|
$line = fgets($handle);
|
||||||
if ($line) {
|
if ($line) {
|
||||||
echo $line; // Affichage en temps réel à l'écran
|
echo $line; // Affichage direct à l'écran (avec les couleurs du Bash)
|
||||||
if (strpos($line, '[ERR]') !== false) {
|
if (strpos($line, '[ERR]') !== false) {
|
||||||
log_error("update_process_error", trim($line));
|
log_error("update_bash_error", trim($line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,15 +45,17 @@ if ($exit_code === 0) {
|
|||||||
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
|
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
|
||||||
|
|
||||||
// 2. Initialisation des fichiers .local manquants
|
// 2. Initialisation des fichiers .local manquants
|
||||||
|
// Cela garantit que le système peut tourner même sans config manuelle préalable
|
||||||
ensure_local_configs();
|
ensure_local_configs();
|
||||||
|
|
||||||
// 3. Vérification du Crontab
|
// 3. Vérification du Crontab
|
||||||
|
// Utilise la Regex pour éviter les doublons avec tes anciennes versions (tirets vs underscores)
|
||||||
ensure_crontab_entries();
|
ensure_crontab_entries();
|
||||||
|
|
||||||
log_info("update_finished", "Mise à jour réussie");
|
log_info("update_finished", "Mise à jour et configuration réussies");
|
||||||
echo "\e[32m[OK]\e[0m Système à jour et configuré.\n";
|
echo "\e[32m[OK]\e[0m Système à jour et configuré.\n";
|
||||||
} else {
|
} else {
|
||||||
log_error("update_failed", "Le script de synchronisation a échoué", ["code" => $exit_code]);
|
log_error("update_failed", "Le script Bash a retourné une erreur", ["code" => $exit_code]);
|
||||||
echo "\e[31m[ERR]\e[0m Échec de la mise à jour.\n";
|
echo "\e[31m[ERR]\e[0m Échec de la mise à jour.\n";
|
||||||
exit($exit_code);
|
exit($exit_code);
|
||||||
}
|
}
|
||||||
@@ -59,9 +64,9 @@ if ($exit_code === 0) {
|
|||||||
* Initialise les fichiers .local.conf.php s'ils n'existent pas
|
* Initialise les fichiers .local.conf.php s'ils n'existent pas
|
||||||
*/
|
*/
|
||||||
function ensure_local_configs() {
|
function ensure_local_configs() {
|
||||||
global $MONITORING_BASE_DIR;
|
global $MONITORING_CONF_DIR;
|
||||||
$conf_dir = $MONITORING_BASE_DIR . '/conf';
|
|
||||||
|
|
||||||
|
// Mapping Source => Destination
|
||||||
$configs = [
|
$configs = [
|
||||||
'monitoring.conf.php' => 'monitoring.local.conf.php',
|
'monitoring.conf.php' => 'monitoring.local.conf.php',
|
||||||
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
|
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
|
||||||
@@ -69,14 +74,14 @@ function ensure_local_configs() {
|
|||||||
];
|
];
|
||||||
|
|
||||||
foreach ($configs as $src => $dst) {
|
foreach ($configs as $src => $dst) {
|
||||||
$src_path = $conf_dir . '/' . $src;
|
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
||||||
$dst_path = $conf_dir . '/' . $dst;
|
$dst_path = $MONITORING_CONF_DIR . '/' . $dst;
|
||||||
|
|
||||||
if (!file_exists($dst_path) && file_exists($src_path)) {
|
if (!file_exists($dst_path) && file_exists($src_path)) {
|
||||||
if (copy($src_path, $dst_path)) {
|
if (copy($src_path, $dst_path)) {
|
||||||
chmod($dst_path, 0600);
|
chmod($dst_path, 0600); // Protection des secrets
|
||||||
log_notice("config_auto_init", "Création auto de $dst");
|
log_notice("config_auto_init", "Création auto de $dst");
|
||||||
echo "\e[32m[OK]\e[0m Fichier créé : $dst\n";
|
echo "\e[32m[OK]\e[0m Fichier config créé : $dst\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,9 +93,10 @@ function ensure_local_configs() {
|
|||||||
function ensure_crontab_entries() {
|
function ensure_crontab_entries() {
|
||||||
global $MONITORING_BASE_DIR;
|
global $MONITORING_BASE_DIR;
|
||||||
|
|
||||||
|
// Notre "Vérité" pour le planning du serveur
|
||||||
$required_jobs = [
|
$required_jobs = [
|
||||||
"*/5 * * * * php {$MONITORING_BASE_DIR}/bin/check_disk.php > /dev/null 2>&1",
|
"*/5 * * * * php {$MONITORING_BASE_DIR}/bin/check_disk.php > /dev/null 2>&1",
|
||||||
"*/15 * * * * php {$MONITORING_BASE_DIR}/bin/check_smart.sh > /dev/null 2>&1",
|
"*/15 * * * * bash {$MONITORING_BASE_DIR}/bin/check_smart.sh > /dev/null 2>&1",
|
||||||
"10 3 * * * php {$MONITORING_BASE_DIR}/bin/monitoring-update.php > /dev/null 2>&1",
|
"10 3 * * * php {$MONITORING_BASE_DIR}/bin/monitoring-update.php > /dev/null 2>&1",
|
||||||
"* * * * * php {$MONITORING_BASE_DIR}/bin/alert-engine.php > /dev/null 2>&1"
|
"* * * * * php {$MONITORING_BASE_DIR}/bin/alert-engine.php > /dev/null 2>&1"
|
||||||
];
|
];
|
||||||
@@ -100,34 +106,35 @@ function ensure_crontab_entries() {
|
|||||||
$updated = false;
|
$updated = false;
|
||||||
|
|
||||||
foreach ($required_jobs as $job) {
|
foreach ($required_jobs as $job) {
|
||||||
// Extraction intelligente du nom du script (ex: check_disk.php)
|
// On cherche le nom du script dans la ligne pour éviter les doublons
|
||||||
// On cherche simplement si le chemin vers /bin/nom_du_script est dans le cron
|
// (ex: cherche "/bin/check_disk.php" dans la ligne du cron)
|
||||||
preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches);
|
if (preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches)) {
|
||||||
$script_name = $matches[0] ?? $job;
|
$script_pattern = $matches[0];
|
||||||
|
|
||||||
$found = false;
|
$found = false;
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
if (strpos($line, $script_name) !== false) {
|
if (strpos($line, $script_pattern) !== false) {
|
||||||
$found = true;
|
$found = true;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!$found) {
|
if (!$found) {
|
||||||
$lines[] = $job;
|
$lines[] = $job;
|
||||||
$updated = true;
|
$updated = true;
|
||||||
echo "\e[32m[OK]\e[0m Ajout au cron : $script_name\n";
|
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_pattern) . "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($updated) {
|
if ($updated) {
|
||||||
// On s'assure qu'il n'y a pas de lignes vides inutiles et on joint avec un saut de ligne final
|
// Filtrage des lignes vides et reconstruction propre
|
||||||
$content = implode("\n", array_filter($lines, 'trim')) . "\n";
|
$content = implode("\n", array_filter($lines, 'trim')) . "\n";
|
||||||
$tmp_cron = tempnam(sys_get_temp_dir(), 'cron');
|
$tmp_cron = tempnam(sys_get_temp_dir(), 'cron');
|
||||||
file_put_contents($tmp_cron, $content);
|
file_put_contents($tmp_cron, $content);
|
||||||
exec("crontab " . escapeshellarg($tmp_cron));
|
exec("crontab " . escapeshellarg($tmp_cron));
|
||||||
unlink($tmp_cron);
|
unlink($tmp_cron);
|
||||||
echo "\e[32m[OK]\e[0m Crontab mis à jour avec succès.\n";
|
echo "\e[32m[OK]\e[0m Crontab de root mis à jour.\n";
|
||||||
} else {
|
} else {
|
||||||
echo "[INFO] Crontab déjà à jour.\n";
|
echo "[INFO] Crontab déjà à jour.\n";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,40 +8,36 @@
|
|||||||
require_once __DIR__ . '/../lib/monitoring-lib.php';
|
require_once __DIR__ . '/../lib/monitoring-lib.php';
|
||||||
|
|
||||||
// --- Configuration ---
|
// --- Configuration ---
|
||||||
// On s'appuie sur le chargement de la lib, mais on surcharge si nécessaire
|
// Note : La lib a déjà chargé $CONFIG['UPDATE_BASE_URL'] etc. depuis monitoring.local.conf.php
|
||||||
$conf_file = "/opt/monitoring/conf/autoupdate.conf.php"; // Format PHP recommandé
|
// On ne charge les fichiers spécifiques que s'ils apportent des règles de mise à jour uniques.
|
||||||
// --- Chargement de la configuration spécifique ---
|
|
||||||
foreach (["/opt/monitoring/conf/autoupdate.conf.php", "/opt/monitoring/conf/autoupdate.local.conf.php"] as $conf) {
|
foreach (["/opt/monitoring/conf/autoupdate.conf.php", "/opt/monitoring/conf/autoupdate.local.conf.php"] as $conf) {
|
||||||
if (file_exists($conf)) {
|
if (file_exists($conf)) {
|
||||||
$extra_conf = include $conf;
|
$extra_conf = include $conf;
|
||||||
if (is_array($extra_conf)) {
|
if (is_array($extra_conf)) {
|
||||||
$CONFIG = array_replace_recursive($CONFIG, $extra_conf);
|
$CONFIG = array_replace_recursive($CONFIG, $extra_conf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables par défaut
|
// Variables par défaut (fallback si absent de la config globale)
|
||||||
$UPDATE_ENABLED = $CONFIG['UPDATE_ENABLED'] ?? true;
|
$UPDATE_ENABLED = $CONFIG['UPDATE_ENABLED'] ?? true;
|
||||||
$UPDATE_TMP_DIR = $CONFIG['UPDATE_TMP_DIR'] ?? '/tmp/monitoring-update';
|
$UPDATE_TMP_DIR = $CONFIG['UPDATE_TMP_DIR'] ?? '/tmp/monitoring-update';
|
||||||
$UPDATE_TIMEOUT_CONNECT = $CONFIG['UPDATE_TIMEOUT_CONNECT'] ?? 3;
|
|
||||||
$UPDATE_TIMEOUT_TOTAL = $CONFIG['UPDATE_TIMEOUT_TOTAL'] ?? 15;
|
$UPDATE_TIMEOUT_TOTAL = $CONFIG['UPDATE_TIMEOUT_TOTAL'] ?? 15;
|
||||||
$UPDATE_MANIFEST_URL = $CONFIG['UPDATE_MANIFEST_URL'] ?? '';
|
$UPDATE_MANIFEST_URL = $CONFIG['UPDATE_MANIFEST_URL'] ?? '';
|
||||||
$UPDATE_BASE_URL = $CONFIG['UPDATE_BASE_URL'] ?? '';
|
$UPDATE_BASE_URL = $CONFIG['UPDATE_BASE_URL'] ?? '';
|
||||||
$UPDATE_ALLOW_DELETE = $CONFIG['UPDATE_ALLOW_DELETE'] ?? false;
|
$UPDATE_ALLOW_DELETE = $CONFIG['UPDATE_ALLOW_DELETE'] ?? false;
|
||||||
$MONITORING_BASE_DIR = $CONFIG['MONITORING_BASE_DIR'] ?? '/opt/monitoring';
|
$MONITORING_BASE_DIR = $MONITORING_BASE_DIR; // Provient de la lib
|
||||||
|
|
||||||
// --- Initialisation ---
|
// --- Initialisation ---
|
||||||
lock_or_exit("monitoring-update");
|
lock_or_exit("monitoring-update");
|
||||||
|
|
||||||
if (!$UPDATE_ENABLED) {
|
if (!$UPDATE_ENABLED) {
|
||||||
log_notice("update_disabled", "Mise à jour désactivée par configuration");
|
log_notice("update_disabled", "Mise à jour désactivée");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_dir($UPDATE_TMP_DIR)) {
|
if (!is_dir($UPDATE_TMP_DIR)) {
|
||||||
if (!mkdir($UPDATE_TMP_DIR, 0755, true)) {
|
mkdir($UPDATE_TMP_DIR, 0755, true);
|
||||||
fail_internal("Impossible de créer le répertoire temporaire: $UPDATE_TMP_DIR");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,13 +47,16 @@ function fetch_manifest($url) {
|
|||||||
global $UPDATE_TIMEOUT_TOTAL;
|
global $UPDATE_TIMEOUT_TOTAL;
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt_array($ch, [
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, $UPDATE_TIMEOUT_TOTAL);
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
curl_setopt($ch, CURLOPT_FAILONERROR, true);
|
CURLOPT_TIMEOUT => $UPDATE_TIMEOUT_TOTAL,
|
||||||
|
CURLOPT_FAILONERROR => true,
|
||||||
|
CURLOPT_FOLLOWLOCATION => true
|
||||||
|
]);
|
||||||
|
|
||||||
$content = curl_exec($ch);
|
$content = curl_exec($ch);
|
||||||
if (curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
log_error("manifest_download_failed", "Impossible de télécharger le manifeste", ["url" => $url, "error" => curl_error($ch)]);
|
log_error("manifest_download_failed", "Échec téléchargement manifeste", ["url" => $url, "err" => curl_error($ch)]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
@@ -66,30 +65,15 @@ function fetch_manifest($url) {
|
|||||||
$lines = explode("\n", trim($content));
|
$lines = explode("\n", trim($content));
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
$line = trim($line);
|
if (preg_match('/^([0-9a-fA-F]{64})\s+(644|755)\s+((bin|lib|conf)\/[A-Za-z0-9._\/-]+)$/', trim($line), $matches)) {
|
||||||
if (empty($line)) continue;
|
$manifest_entries[] = ['hash' => $matches[1], 'mode' => $matches[2], 'path' => $matches[3]];
|
||||||
|
|
||||||
// Validation format: hash(64) mode(3) path
|
|
||||||
if (preg_match('/^([0-9a-fA-F]{64})\s+(644|755)\s+((bin|lib|conf)\/[A-Za-z0-9._\/-]+)$/', $line, $matches)) {
|
|
||||||
$manifest_entries[] = [
|
|
||||||
'hash' => $matches[1],
|
|
||||||
'mode' => $matches[2],
|
|
||||||
'path' => $matches[3]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($manifest_entries)) {
|
|
||||||
log_error("manifest_invalid", "Le manifeste distant est invalide ou vide", ["url" => $url]);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
log_info("manifest_downloaded", "Manifeste téléchargé", ["url" => $url]);
|
|
||||||
return $manifest_entries;
|
return $manifest_entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Met à jour un fichier spécifique
|
* Met à jour un fichier
|
||||||
*/
|
*/
|
||||||
function update_one_file($entry) {
|
function update_one_file($entry) {
|
||||||
global $MONITORING_BASE_DIR, $UPDATE_BASE_URL, $UPDATE_TMP_DIR, $UPDATE_TIMEOUT_TOTAL;
|
global $MONITORING_BASE_DIR, $UPDATE_BASE_URL, $UPDATE_TMP_DIR, $UPDATE_TIMEOUT_TOTAL;
|
||||||
@@ -99,129 +83,86 @@ function update_one_file($entry) {
|
|||||||
$remote_url = rtrim($UPDATE_BASE_URL, '/') . '/' . $rel_path;
|
$remote_url = rtrim($UPDATE_BASE_URL, '/') . '/' . $rel_path;
|
||||||
$expected_hash = strtolower($entry['hash']);
|
$expected_hash = strtolower($entry['hash']);
|
||||||
|
|
||||||
// Calcul du hash local actuel
|
if (file_exists($target_file) && hash_file('sha256', $target_file) === $expected_hash) {
|
||||||
$local_hash = file_exists($target_file) ? hash_file('sha256', $target_file) : "";
|
|
||||||
|
|
||||||
if ($local_hash === $expected_hash) {
|
|
||||||
log_debug("update_not_needed", "Fichier déjà à jour", ["file" => $rel_path]);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Téléchargement
|
|
||||||
$tmp_file = $UPDATE_TMP_DIR . '/' . basename($rel_path) . '.' . uniqid();
|
$tmp_file = $UPDATE_TMP_DIR . '/' . basename($rel_path) . '.' . uniqid();
|
||||||
$ch = curl_init($remote_url);
|
$ch = curl_init($remote_url);
|
||||||
$fp = fopen($tmp_file, 'wb');
|
$fp = fopen($tmp_file, 'wb');
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
curl_setopt_array($ch, [
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, $UPDATE_TIMEOUT_TOTAL);
|
CURLOPT_FILE => $fp,
|
||||||
curl_setopt($ch, CURLOPT_FAILONERROR, true);
|
CURLOPT_TIMEOUT => $UPDATE_TIMEOUT_TOTAL,
|
||||||
|
CURLOPT_FAILONERROR => true,
|
||||||
|
CURLOPT_FOLLOWLOCATION => true
|
||||||
|
]);
|
||||||
|
|
||||||
$success = curl_exec($ch);
|
$success = curl_exec($ch);
|
||||||
$error = curl_error($ch);
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
|
|
||||||
if (!$success) {
|
if (!$success || hash_file('sha256', $tmp_file) !== $expected_hash) {
|
||||||
log_error("update_download_failed", "Téléchargement impossible", ["file" => $rel_path, "url" => $remote_url, "error" => $error]);
|
log_error("update_failed", "Fichier invalide ou corrompu", ["file" => $rel_path]);
|
||||||
@unlink($tmp_file);
|
@unlink($tmp_file);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vérification Hash
|
|
||||||
$downloaded_hash = hash_file('sha256', $tmp_file);
|
|
||||||
if ($downloaded_hash !== $expected_hash) {
|
|
||||||
log_error("update_hash_mismatch", "Hash téléchargé invalide", ["file" => $rel_path, "expected" => $expected_hash, "got" => $downloaded_hash]);
|
|
||||||
@unlink($tmp_file);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Installation
|
|
||||||
ensure_parent_dir($target_file);
|
ensure_parent_dir($target_file);
|
||||||
chmod($tmp_file, octdec($entry['mode']));
|
chmod($tmp_file, octdec($entry['mode']));
|
||||||
|
safe_mv($tmp_file, $target_file); // Utilise la fonction safe_mv de ta lib
|
||||||
|
|
||||||
if (!rename($tmp_file, $target_file)) {
|
log_notice("file_updated", "Mise à jour appliquée", ["file" => $rel_path]);
|
||||||
fail_internal("Échec du déplacement de $tmp_file vers $target_file");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($local_hash === "") {
|
|
||||||
log_notice("file_created", "Fichier créé depuis le manifeste", ["file" => $rel_path, "mode" => $entry['mode']]);
|
|
||||||
} else {
|
|
||||||
log_notice("update_applied", "Mise à jour appliquée", ["file" => $rel_path, "old_hash" => $local_hash, "new_hash" => $expected_hash]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supprime les fichiers locaux absents du manifeste
|
* Nettoyage
|
||||||
*/
|
*/
|
||||||
function delete_extra_files($remote_files) {
|
function delete_extra_files($remote_files) {
|
||||||
global $UPDATE_ALLOW_DELETE, $MONITORING_BASE_DIR;
|
global $UPDATE_ALLOW_DELETE, $MONITORING_BASE_DIR, $SCRIPT_PATH;
|
||||||
if (!$UPDATE_ALLOW_DELETE) return;
|
if (!$UPDATE_ALLOW_DELETE) return;
|
||||||
|
|
||||||
$directories = ['bin', 'lib', 'conf'];
|
foreach (['bin', 'lib', 'conf'] as $dir) {
|
||||||
foreach ($directories as $dir) {
|
$full_path = $MONITORING_BASE_DIR . '/' . $dir;
|
||||||
$full_path = $MONITORING_BASE_DIR . '/' . $dir;
|
if (!is_dir($full_path)) continue;
|
||||||
if (!is_dir($full_path)) continue;
|
|
||||||
|
|
||||||
$iterator = new RecursiveIteratorIterator(
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($full_path, RecursiveDirectoryIterator::SKIP_DOTS));
|
||||||
new RecursiveDirectoryIterator($full_path, RecursiveDirectoryIterator::SKIP_DOTS)
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($iterator as $file) {
|
foreach ($iterator as $file) {
|
||||||
// On récupère le chemin relatif par rapport à la racine du monitoring
|
$path = $file->getPathname();
|
||||||
$rel_path = substr($file->getPathname(), strlen($MONITORING_BASE_DIR) + 1);
|
$rel_path = substr($path, strlen($MONITORING_BASE_DIR) + 1);
|
||||||
|
|
||||||
// 1. Protection : Si c'est dans le manifeste distant, on ne touche à rien
|
// PROTECTIONS
|
||||||
if (in_array($rel_path, $remote_files)) {
|
if (in_array($rel_path, $remote_files)) continue;
|
||||||
continue;
|
if (str_contains($rel_path, '.local.')) continue; // Protection fichiers locaux
|
||||||
}
|
if ($path === $SCRIPT_PATH) continue; // Ne pas se suicider
|
||||||
|
|
||||||
// 2. Protection générique : On n'efface JAMAIS les fichiers de configuration locale
|
if (@unlink($path)) {
|
||||||
// Cela couvre : *.local.conf.php, *.local.conf, et même *.local.php par sécurité
|
log_notice("file_deleted", "Fichier obsolète supprimé", ["file" => $rel_path]);
|
||||||
if (str_ends_with($rel_path, '.local.conf.php') ||
|
}
|
||||||
str_ends_with($rel_path, '.local.conf') ||
|
}
|
||||||
str_ends_with($rel_path, '.local.php')) {
|
}
|
||||||
|
|
||||||
log_debug("delete_skipped", "Fichier local protégé (ignoré)", ["file" => $rel_path]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Suppression si le fichier est obsolète et non protégé
|
|
||||||
if (@unlink($file->getPathname())) {
|
|
||||||
log_notice("file_deleted", "Fichier obsolète supprimé", ["file" => $rel_path]);
|
|
||||||
} else {
|
|
||||||
log_error("delete_failed", "Impossible de supprimer le fichier local", ["file" => $rel_path]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Main ---
|
// --- Main ---
|
||||||
|
|
||||||
$manifest = fetch_manifest($UPDATE_MANIFEST_URL);
|
$manifest = fetch_manifest($UPDATE_MANIFEST_URL);
|
||||||
if ($manifest === false) exit(2);
|
if (!$manifest) exit(2);
|
||||||
|
|
||||||
$total = count($manifest);
|
|
||||||
$updated = 0;
|
|
||||||
$failed = 0;
|
|
||||||
$remote_paths = [];
|
$remote_paths = [];
|
||||||
|
$updated = 0; $failed = 0;
|
||||||
|
|
||||||
foreach ($manifest as $entry) {
|
foreach ($manifest as $entry) {
|
||||||
$remote_paths[] = $entry['path'];
|
$remote_paths[] = $entry['path'];
|
||||||
if (update_one_file($entry)) {
|
update_one_file($entry) ? $updated++ : $failed++;
|
||||||
$updated++;
|
|
||||||
} else {
|
|
||||||
$failed++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete_extra_files($remote_paths);
|
delete_extra_files($remote_paths);
|
||||||
|
|
||||||
if ($failed > 0) {
|
if ($failed > 0) {
|
||||||
log_warning("update_finished_with_errors", "Mise à jour terminée avec erreurs", ["total" => $total, "updated" => $updated, "failed" => $failed]);
|
log_warning("update_partial", "Mise à jour terminée avec erreurs", ["failed" => $failed]);
|
||||||
} else {
|
} else {
|
||||||
log_info("update_finished", "Mise à jour terminée", ["total" => $total, "updated" => $updated]);
|
log_info("update_ok", "Mise à jour terminée avec succès");
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_with_status();
|
exit_with_status();
|
||||||
@@ -24,12 +24,6 @@ return [
|
|||||||
'ALERT_MAIL_SUBJECT_PREFIX' => '[monitoring]',
|
'ALERT_MAIL_SUBJECT_PREFIX' => '[monitoring]',
|
||||||
'DEST' => 'admin@example.com', // N'oubliez pas de définir le destinataire
|
'DEST' => 'admin@example.com', // N'oubliez pas de définir le destinataire
|
||||||
|
|
||||||
// --- Configuration ntfy ---
|
|
||||||
'NTFY_SERVER' => 'https://ntfy.sh',
|
|
||||||
'NTFY_TOPIC' => 'TPOSOB84sBJ6HTZ7',
|
|
||||||
'NTFY_TOKEN' => '',
|
|
||||||
'NTFY_CLICK_URL' => '',
|
|
||||||
|
|
||||||
// --- Déduplication ---
|
// --- Déduplication ---
|
||||||
'ALERT_DEDUP_WINDOW' => 3600, // en secondes
|
'ALERT_DEDUP_WINDOW' => 3600, // en secondes
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,16 @@ $CONFIG = [
|
|||||||
'LOG_LEVEL' => 'INFO'
|
'LOG_LEVEL' => 'INFO'
|
||||||
];
|
];
|
||||||
|
|
||||||
if (file_exists($MONITORING_CONF_DIR . '/monitoring.conf.php')) {
|
// 1. On charge la configuration GLOBALE (La vérité est ici)
|
||||||
$global_conf = include $MONITORING_CONF_DIR . '/monitoring.conf.php';
|
$global_conf = $MONITORING_CONF_DIR . "/monitoring.local.conf.php";
|
||||||
if (is_array($global_conf)) {
|
if (file_exists($global_conf)) {
|
||||||
$CONFIG = array_merge($CONFIG, $global_conf);
|
$CONFIG = array_replace_recursive($CONFIG, include $global_conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. On charge ensuite la config spécifique au script (si besoin de surcharger)
|
||||||
|
// $specific_conf est défini par le script qui appelle la lib
|
||||||
|
if (isset($specific_conf) && file_exists($specific_conf)) {
|
||||||
|
$CONFIG = array_replace_recursive($CONFIG, include $specific_conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables d'exécution
|
// Variables d'exécution
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
5b4ea784d2cbe73f6e829e35f23b0b4dbe12df55cc1abc8eba6602da36c724ef 755 bin/alert-engine.php
|
5b4ea784d2cbe73f6e829e35f23b0b4dbe12df55cc1abc8eba6602da36c724ef 755 bin/alert-engine.php
|
||||||
d3d80aab061d7bef8c3830eac2198f10b98270f8b48517b6538ddf766c8dcda7 755 bin/check_disk.sh
|
fdcea6720186795538f48c08b99103b320273dbdd0ea5246a2da9d81a1eecc6c 755 bin/check_disk.sh
|
||||||
ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_smart.sh
|
ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_smart.sh
|
||||||
c5443fceb7aa821b1164a073bd694e8a8b0dbc3950405548fd525091f6b1c50b 755 bin/install-monitoring.sh
|
c5443fceb7aa821b1164a073bd694e8a8b0dbc3950405548fd525091f6b1c50b 755 bin/install-monitoring.sh
|
||||||
97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php
|
97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php
|
||||||
ea5a5d55bb877ae88da6e1cd1b798026a1de1d9845dc42af4b19685ad6a128c6 755 bin/monitoring.php
|
02bd43ed2a9b92acc013274c716e6bc50120a8103ccf3d9c4e6f345a0b22d6a0 755 bin/monitoring.php
|
||||||
97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php
|
97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php
|
||||||
17e4a31d5f82833a3fdb16a46050cd61e69250d6278d0b5f1f8bf16d8671aedf 755 bin/monitoring-update.php
|
de19c7cddbfbfca82813479d57f156d22c7f9f3733f18fec8b19515ded00dd7b 755 bin/monitoring-update.php
|
||||||
83db39c8d0cfd6f6e9d3cc5b961a67db29dc73666304a91e0d4a6d5831c623cb 644 conf/alert-engine.conf
|
dc70c1184da4aa32eebdeaee57cfed23e91397c94a6243e0ac8664968078f0c7 644 conf/alert-engine.conf.php
|
||||||
69fc1e3506aec7ad3e5d9fbc74587ab4e6381f3e6840f3e38c526f4752858bd4 644 conf/alert-engine.conf.php
|
|
||||||
caaa8f6031d66bc43a897ac2804124ce2050a64523734195d5505ae863836bf4 644 conf/monitoring.conf
|
|
||||||
8c40d1c177a40d47c72ba8aab757ca37faa06e64d5fa80e2ba59d9637f62c59e 644 conf/monitoring.conf.php
|
8c40d1c177a40d47c72ba8aab757ca37faa06e64d5fa80e2ba59d9637f62c59e 644 conf/monitoring.conf.php
|
||||||
c3794b4d69c772b87d3a61c2f94a3c8bf504257f7d466ee6ce160cd79766365e 644 lib/monitoring-lib.php
|
9bb7f5438edc5fb6a5b899ee21be2a5a559eb0697a028a4e991fc82362eaa460 644 lib/monitoring-lib.php
|
||||||
|
|||||||
Reference in New Issue
Block a user