suppression des fantomes sur les anciennes installation
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Moteur de mise à jour - Version Supervisor
|
* Moteur de mise à jour - Version Supervisor
|
||||||
* Pilotage du script Bash + Initialisation des Configs + Cron
|
* Pilotage du script Bash + Initialisation des Configs + Cron + Ménage
|
||||||
* Copyright (C) 2026 Cédric Abonnel
|
* Copyright (C) 2026 Cédric Abonnel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -21,8 +21,7 @@ if (!file_exists($install_script)) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Exécution du moteur de synchronisation Bash (il gère téléchargement et purge)
|
// 1. Exécution du moteur de synchronisation Bash
|
||||||
// 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');
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ if ($handle) {
|
|||||||
while (!feof($handle)) {
|
while (!feof($handle)) {
|
||||||
$line = fgets($handle);
|
$line = fgets($handle);
|
||||||
if ($line) {
|
if ($line) {
|
||||||
echo $line; // Affichage direct à l'écran (avec les couleurs du Bash)
|
echo $line;
|
||||||
if (strpos($line, '[ERR]') !== false) {
|
if (strpos($line, '[ERR]') !== false) {
|
||||||
log_error("update_bash_error", trim($line));
|
log_error("update_bash_error", trim($line));
|
||||||
}
|
}
|
||||||
@@ -44,15 +43,10 @@ if ($handle) {
|
|||||||
if ($exit_code === 0) {
|
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
|
|
||||||
// Cela garantit que le système peut tourner même sans config manuelle préalable
|
|
||||||
ensure_local_configs();
|
ensure_local_configs();
|
||||||
|
ensure_crontab_entries(); // Logique de nettoyage incluse ici
|
||||||
|
|
||||||
// 3. Vérification du Crontab
|
log_info("update_finished", "Mise à jour et nettoyage crontab réussis");
|
||||||
// Utilise la Regex pour éviter les doublons avec tes anciennes versions (tirets vs underscores)
|
|
||||||
ensure_crontab_entries();
|
|
||||||
|
|
||||||
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 Bash a retourné une erreur", ["code" => $exit_code]);
|
log_error("update_failed", "Le script Bash a retourné une erreur", ["code" => $exit_code]);
|
||||||
@@ -65,22 +59,17 @@ if ($exit_code === 0) {
|
|||||||
*/
|
*/
|
||||||
function ensure_local_configs() {
|
function ensure_local_configs() {
|
||||||
global $MONITORING_CONF_DIR;
|
global $MONITORING_CONF_DIR;
|
||||||
|
|
||||||
// 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',
|
||||||
'autoupdate.conf.php' => 'autoupdate.local.conf.php'
|
'autoupdate.conf.php' => 'autoupdate.local.conf.php'
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($configs as $src => $dst) {
|
foreach ($configs as $src => $dst) {
|
||||||
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
$src_path = $MONITORING_CONF_DIR . '/' . $src;
|
||||||
$dst_path = $MONITORING_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); // Protection des secrets
|
chmod($dst_path, 0600);
|
||||||
log_notice("config_auto_init", "Création auto de $dst");
|
|
||||||
echo "\e[32m[OK]\e[0m Fichier config créé : $dst\n";
|
echo "\e[32m[OK]\e[0m Fichier config créé : $dst\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,55 +77,82 @@ function ensure_local_configs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assure la présence des tâches dans le crontab de root
|
* Assure la présence des tâches correctes et SUPPRIME les anciennes
|
||||||
*/
|
*/
|
||||||
function ensure_crontab_entries() {
|
function ensure_crontab_entries() {
|
||||||
global $MONITORING_BASE_DIR;
|
global $MONITORING_BASE_DIR;
|
||||||
|
|
||||||
// Notre "Vérité" pour le planning du serveur
|
// 1. Définition de la cible (La SEULE vérité pour le monitoring)
|
||||||
$required_jobs = [
|
$required_jobs = [
|
||||||
"*/5 * * * * php {$MONITORING_BASE_DIR}/bin/check_disk.php > /dev/null 2>&1",
|
"*/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",
|
"*/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"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 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") ?: "";
|
$current_cron = shell_exec("crontab -l 2>/dev/null") ?: "";
|
||||||
$lines = explode("\n", trim($current_cron));
|
$lines = explode("\n", trim($current_cron));
|
||||||
$updated = false;
|
$new_lines = [];
|
||||||
|
$has_changed = false;
|
||||||
|
|
||||||
|
// --- PHASE A : Nettoyage ---
|
||||||
|
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";
|
||||||
|
$keep = false;
|
||||||
|
$has_changed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($keep) {
|
||||||
|
$new_lines[] = $trim_line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- PHASE B : Insertion des tâches manquantes ---
|
||||||
foreach ($required_jobs as $job) {
|
foreach ($required_jobs as $job) {
|
||||||
// On cherche le nom du script dans la ligne pour éviter les doublons
|
// Extraction du nom du script pour vérifier s'il existe déjà
|
||||||
// (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_path = $matches[0] ?? $job;
|
||||||
$script_pattern = $matches[0];
|
|
||||||
|
|
||||||
$found = false;
|
$found = false;
|
||||||
foreach ($lines as $line) {
|
foreach ($new_lines as $line) {
|
||||||
if (strpos($line, $script_pattern) !== false) {
|
if (strpos($line, $script_path) !== false) {
|
||||||
$found = true;
|
$found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$found) {
|
if (!$found) {
|
||||||
$lines[] = $job;
|
$new_lines[] = $job;
|
||||||
$updated = true;
|
$has_changed = true;
|
||||||
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_pattern) . "\n";
|
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_path) . "\n";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($updated) {
|
// --- PHASE C : Application ---
|
||||||
// Filtrage des lignes vides et reconstruction propre
|
if ($has_changed) {
|
||||||
$content = implode("\n", array_filter($lines, 'trim')) . "\n";
|
$content = implode("\n", array_filter($new_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 de root mis à jour.\n";
|
echo "\e[32m[OK]\e[0m Crontab de root synchronisé.\n";
|
||||||
} else {
|
} else {
|
||||||
echo "[INFO] Crontab déjà à jour.\n";
|
echo "[INFO] Crontab déjà propre.\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ ead10d3be3aac48c6406a734dee1bddf9a8abb1e21de102ce72fa92fdecbaf22 755 bin/check_s
|
|||||||
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
|
||||||
de19c7cddbfbfca82813479d57f156d22c7f9f3733f18fec8b19515ded00dd7b 755 bin/monitoring-update.php
|
8cf4518b2d7628dedde25af84b93ee2a99340029cafdc8b9924a4d23ebc7d8f5 755 bin/monitoring-update.php
|
||||||
dc70c1184da4aa32eebdeaee57cfed23e91397c94a6243e0ac8664968078f0c7 644 conf/alert-engine.conf.php
|
dc70c1184da4aa32eebdeaee57cfed23e91397c94a6243e0ac8664968078f0c7 644 conf/alert-engine.conf.php
|
||||||
8c40d1c177a40d47c72ba8aab757ca37faa06e64d5fa80e2ba59d9637f62c59e 644 conf/monitoring.conf.php
|
8c40d1c177a40d47c72ba8aab757ca37faa06e64d5fa80e2ba59d9637f62c59e 644 conf/monitoring.conf.php
|
||||||
9bb7f5438edc5fb6a5b899ee21be2a5a559eb0697a028a4e991fc82362eaa460 644 lib/monitoring-lib.php
|
9bb7f5438edc5fb6a5b899ee21be2a5a559eb0697a028a4e991fc82362eaa460 644 lib/monitoring-lib.php
|
||||||
|
|||||||
Reference in New Issue
Block a user