#!/usr/bin/env php $install_script]); echo "\e[31m[ERR]\e[0m Script d'installation introuvable : $install_script\n"; exit(1); } // 1. 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)); } } } $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); } /** * Initialise les fichiers .local.conf.php s'ils n'existent pas */ 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) { $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 = [ "*/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 --- 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) { // Extraction du nom du script pour vérifier s'il existe déjà preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches); $script_path = $matches[0] ?? $job; $found = false; foreach ($new_lines as $line) { if (strpos($line, $script_path) !== false) { $found = true; break; } } if (!$found) { $new_lines[] = $job; $has_changed = true; echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_path) . "\n"; } } // --- PHASE C : Application --- if ($has_changed) { $content = implode("\n", array_filter($new_lines, 'trim')) . "\n"; $tmp_cron = tempnam(sys_get_temp_dir(), 'cron'); 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"; } else { echo "[INFO] Crontab déjà propre.\n"; } } exit_with_status();