historique des changements ajouté
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* Moteur de mise à jour - Version Supervisor
|
||||
* Moteur de mise à jour
|
||||
* Pilotage du script Bash + Initialisation des Configs + Cron + Ménage
|
||||
* Copyright (C) 2026 Cédric Abonnel
|
||||
*/
|
||||
@@ -13,6 +13,14 @@ lock_or_exit("monitoring-update");
|
||||
|
||||
echo "\e[1m--- Début de la mise à jour système ---\e[0m\n";
|
||||
|
||||
// 1. Avant l'update, on mémorise la liste des fichiers actuellement installés (L'AVANT)
|
||||
$installed_log = $CONFIG['INSTALLED_LOG'] ?? '/var/lib/monitoring/installed-files.log';
|
||||
$old_installed_files = [];
|
||||
if (file_exists($installed_log)) {
|
||||
$old_installed_files = file($installed_log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
}
|
||||
|
||||
// 2. Exécution du moteur de synchronisation Bash
|
||||
$install_script = __DIR__ . '/install-monitoring.sh';
|
||||
|
||||
if (!file_exists($install_script)) {
|
||||
@@ -21,112 +29,107 @@ if (!file_exists($install_script)) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 1. Exécution du moteur de synchronisation Bash
|
||||
// Exécution du moteur de synchronisation Bash
|
||||
$command = "bash " . escapeshellarg($install_script) . " --auto 2>&1";
|
||||
$handle = popen($command, 'r');
|
||||
|
||||
if ($handle) {
|
||||
while (!feof($handle)) {
|
||||
$line = fgets($handle);
|
||||
if ($line) {
|
||||
echo $line;
|
||||
if (strpos($line, '[ERR]') !== false) {
|
||||
log_error("update_bash_error", trim($line));
|
||||
}
|
||||
}
|
||||
if ($line) echo $line;
|
||||
}
|
||||
$exit_code = pclose($handle);
|
||||
} else {
|
||||
$exit_code = 1;
|
||||
}
|
||||
|
||||
if ($exit_code === 0) {
|
||||
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
|
||||
|
||||
ensure_local_configs();
|
||||
ensure_crontab_entries(); // Logique de nettoyage incluse ici
|
||||
|
||||
log_info("update_finished", "Mise à jour et nettoyage crontab réussis");
|
||||
echo "\e[32m[OK]\e[0m Système à jour et configuré.\n";
|
||||
} else {
|
||||
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";
|
||||
exit($exit_code);
|
||||
if ($exit_code !== 0) {
|
||||
log_error("update_failed", "Le script Bash a échoué");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 3. Après l'update, on récupère la nouvelle liste (L'APRÈS)
|
||||
$new_installed_files = [];
|
||||
if (file_exists($installed_log)) {
|
||||
$new_installed_files = file($installed_log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
}
|
||||
|
||||
// Détermination des fichiers qui ont été SUPPRIMÉS lors de cette mise à jour (dynamique)
|
||||
$deleted_files = array_diff($old_installed_files, $new_installed_files);
|
||||
|
||||
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
|
||||
ensure_local_configs();
|
||||
ensure_crontab_entries($deleted_files);
|
||||
|
||||
echo "\e[32m[OK]\e[0m Système à jour.\n";
|
||||
|
||||
/**
|
||||
* Initialise les fichiers .local.conf.php s'ils n'existent pas
|
||||
* Nettoyage et Mise à jour du Crontab
|
||||
* @param array $deleted_files Liste des chemins de fichiers supprimés du déploiement
|
||||
*/
|
||||
function ensure_local_configs() {
|
||||
global $MONITORING_CONF_DIR;
|
||||
$configs = [
|
||||
'monitoring.conf.php' => 'monitoring.local.conf.php',
|
||||
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
|
||||
'autoupdate.conf.php' => 'autoupdate.local.conf.php'
|
||||
function ensure_crontab_entries($deleted_files) {
|
||||
global $CONFIG, $MONITORING_BASE_DIR;
|
||||
|
||||
// --- CONFIGURATION DU MÉNAGE STATIQUE ---
|
||||
// On ajoute ici les vieux chemins orphelins à éjecter impérativement
|
||||
$static_cleanup_patterns = [
|
||||
'/usr/local/bin/sys_check.sh',
|
||||
'bin/check_disk.php', // Ancienne erreur de nommage
|
||||
'bin/check-disk.sh' // Ancienne erreur de séparateur
|
||||
];
|
||||
foreach ($configs as $src => $dst) {
|
||||
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
||||
$dst_path = $MONITORING_CONF_DIR . '/' . $dst;
|
||||
if (!file_exists($dst_path) && file_exists($src_path)) {
|
||||
if (copy($src_path, $dst_path)) {
|
||||
chmod($dst_path, 0600);
|
||||
echo "\e[32m[OK]\e[0m Fichier config créé : $dst\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assure la présence des tâches correctes et SUPPRIME les anciennes
|
||||
*/
|
||||
function ensure_crontab_entries() {
|
||||
global $MONITORING_BASE_DIR;
|
||||
|
||||
// 1. Définition de la cible (La SEULE vérité pour le monitoring)
|
||||
$required_jobs = [
|
||||
// Préparation des jobs requis
|
||||
$required_jobs = array_map(function($job) use ($MONITORING_BASE_DIR) {
|
||||
return str_replace('{BASE_DIR}', $MONITORING_BASE_DIR, $job);
|
||||
}, $CONFIG['CRON_JOBS'] ?? [
|
||||
"*/5 * * * * bash {$MONITORING_BASE_DIR}/bin/check_disk.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",
|
||||
"* * * * * php {$MONITORING_BASE_DIR}/bin/alert-engine.php > /dev/null 2>&1"
|
||||
];
|
||||
|
||||
// 2. Liste des motifs à supprimer (Anciennes erreurs ou scripts obsolètes)
|
||||
$patterns_to_remove = [
|
||||
"/usr/local/bin/sys_check.sh", // Script obsolète
|
||||
"bin/check_disk.php", // Mauvaise extension
|
||||
"bin/check-disk.sh" // Mauvais séparateur
|
||||
];
|
||||
]);
|
||||
|
||||
$current_cron = shell_exec("crontab -l 2>/dev/null") ?: "";
|
||||
$lines = explode("\n", trim($current_cron));
|
||||
$new_lines = [];
|
||||
$has_changed = false;
|
||||
|
||||
// --- PHASE A : Nettoyage ---
|
||||
// --- PHASE A : Nettoyage (Dynamique + Statique) ---
|
||||
foreach ($lines as $line) {
|
||||
$trim_line = trim($line);
|
||||
if (empty($trim_line)) continue;
|
||||
|
||||
$keep = true;
|
||||
foreach ($patterns_to_remove as $pattern) {
|
||||
if (strpos($trim_line, $pattern) !== false) {
|
||||
echo "\e[33m[CLEAN]\e[0m Suppression de l'ancienne tâche : $pattern\n";
|
||||
|
||||
// 1. Nettoyage Dynamique (basé sur le log Git)
|
||||
foreach ($deleted_files as $deleted_path) {
|
||||
if (strpos($trim_line, $deleted_path) !== false) {
|
||||
echo "\e[33m[CLEAN]\e[0m Script supprimé du déploiement : " . basename($deleted_path) . "\n";
|
||||
$keep = false;
|
||||
$has_changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Nettoyage Statique (basé sur ta liste forcée)
|
||||
if ($keep) {
|
||||
$new_lines[] = $trim_line;
|
||||
foreach ($static_cleanup_patterns as $pattern) {
|
||||
if (strpos($trim_line, $pattern) !== false) {
|
||||
echo "\e[33m[CLEAN]\e[0m Suppression du résidu historique : $pattern\n";
|
||||
$keep = false;
|
||||
$has_changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($keep) $new_lines[] = $trim_line;
|
||||
}
|
||||
|
||||
// --- PHASE B : Insertion des tâches manquantes ---
|
||||
// --- PHASE B : Ajout des nouveaux jobs ---
|
||||
foreach ($required_jobs as $job) {
|
||||
// Extraction du nom du script pour vérifier s'il existe déjà
|
||||
// Extraction du chemin du script pour éviter les doublons
|
||||
preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches);
|
||||
$script_path = $matches[0] ?? $job;
|
||||
$script_path = $matches[0] ?? "";
|
||||
|
||||
$found = false;
|
||||
foreach ($new_lines as $line) {
|
||||
@@ -136,7 +139,7 @@ function ensure_crontab_entries() {
|
||||
}
|
||||
}
|
||||
|
||||
if (!$found) {
|
||||
if (!$found && !empty($script_path)) {
|
||||
$new_lines[] = $job;
|
||||
$has_changed = true;
|
||||
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_path) . "\n";
|
||||
@@ -150,10 +153,27 @@ function ensure_crontab_entries() {
|
||||
file_put_contents($tmp_cron, $content);
|
||||
exec("crontab " . escapeshellarg($tmp_cron));
|
||||
unlink($tmp_cron);
|
||||
echo "\e[32m[OK]\e[0m Crontab de root synchronisé.\n";
|
||||
echo "\e[32m[OK]\e[0m Crontab synchronisé.\n";
|
||||
} else {
|
||||
echo "[INFO] Crontab déjà propre.\n";
|
||||
echo "[INFO] Crontab déjà à jour.\n";
|
||||
}
|
||||
}
|
||||
|
||||
exit_with_status();
|
||||
function ensure_local_configs() {
|
||||
global $MONITORING_CONF_DIR;
|
||||
$configs = [
|
||||
'monitoring.conf.php' => 'monitoring.local.conf.php',
|
||||
'alert-engine.conf.php' => 'alert-engine.conf.local.php',
|
||||
'autoupdate.conf.php' => 'autoupdate.local.conf.php'
|
||||
];
|
||||
foreach ($configs as $src => $dst) {
|
||||
$dst_path = $MONITORING_CONF_DIR . '/' . $dst;
|
||||
if (!file_exists($dst_path)) {
|
||||
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
||||
if (file_exists($src_path)) {
|
||||
copy($src_path, $dst_path);
|
||||
chmod($dst_path, 0600);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,4 +51,13 @@ return [
|
||||
|
||||
// --- Logs ---
|
||||
'LOG_LEVEL' => 'INFO', // DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL
|
||||
|
||||
'STATE_DIR' => '/var/lib/monitoring',
|
||||
'INSTALLED_LOG' => '/var/lib/monitoring/installed-files.log',
|
||||
'CRON_JOBS' => [
|
||||
"*/5 * * * * bash {BASE_DIR}/bin/check_disk.sh > /dev/null 2>&1",
|
||||
"*/15 * * * * bash {BASE_DIR}/bin/check_smart.sh > /dev/null 2>&1",
|
||||
"10 3 * * * php {BASE_DIR}/bin/monitoring-update.php > /dev/null 2>&1",
|
||||
"* * * * * php {BASE_DIR}/bin/alert-engine.php > /dev/null 2>&1"
|
||||
],
|
||||
];
|
||||
@@ -5,7 +5,7 @@ ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_s
|
||||
97a91b13b0776acb3326010821ffcc163e96a97e3c326ea77f11efdb7baf159a 755 bin/log-cli.php
|
||||
02bd43ed2a9b92acc013274c716e6bc50120a8103ccf3d9c4e6f345a0b22d6a0 755 bin/monitoring.php
|
||||
97d407d75a26bd2ebbb86a2e5f8dab8b24639e8a9164f42bd554ba7728ab8cb5 755 bin/monitoring-update-config.php
|
||||
8cf4518b2d7628dedde25af84b93ee2a99340029cafdc8b9924a4d23ebc7d8f5 755 bin/monitoring-update.php
|
||||
910a7c3a4423fb5456233d0c6cbcfc7f511c94947580972c42286762142e5ce6 755 bin/monitoring-update.php
|
||||
dc70c1184da4aa32eebdeaee57cfed23e91397c94a6243e0ac8664968078f0c7 644 conf/alert-engine.conf.php
|
||||
8c40d1c177a40d47c72ba8aab757ca37faa06e64d5fa80e2ba59d9637f62c59e 644 conf/monitoring.conf.php
|
||||
324038d28f24f3f4d1f6def73752ff703d4ce8b532a663c6628611923748b1f5 644 conf/monitoring.conf.php
|
||||
9bb7f5438edc5fb6a5b899ee21be2a5a559eb0697a028a4e991fc82362eaa460 644 lib/monitoring-lib.php
|
||||
|
||||
Reference in New Issue
Block a user