Files
scripts-bash/servers/linux/monitoring/bin/monitoring-update.php

179 lines
6.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/**
* Moteur de mise à jour
* Pilotage du script Bash + Initialisation des Configs + Cron + Ménage
* Copyright (C) 2026 Cédric Abonnel
*/
require_once __DIR__ . '/../lib/monitoring-lib.php';
// Sécurité : Un seul update à la fois
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)) {
log_error("update_script_missing", "Script d'installation introuvable", ["path" => $install_script]);
echo "\e[31m[ERR]\e[0m Script d'installation introuvable : $install_script\n";
exit(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;
}
$exit_code = pclose($handle);
} else {
$exit_code = 1;
}
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";
/**
* Nettoyage et Mise à jour du Crontab
* @param array $deleted_files Liste des chemins de fichiers supprimés du déploiement
*/
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
];
// 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"
]);
$current_cron = shell_exec("crontab -l 2>/dev/null") ?: "";
$lines = explode("\n", trim($current_cron));
$new_lines = [];
$has_changed = false;
// --- PHASE A : Nettoyage (Dynamique + Statique) ---
foreach ($lines as $line) {
$trim_line = trim($line);
if (empty($trim_line)) continue;
$keep = true;
// 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) {
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 : Ajout des nouveaux jobs ---
foreach ($required_jobs as $job) {
// Extraction du chemin du script pour éviter les doublons
preg_match('/\/bin\/([a-z0-9_-]+\.(php|sh))/i', $job, $matches);
$script_path = $matches[0] ?? "";
$found = false;
foreach ($new_lines as $line) {
if (strpos($line, $script_path) !== false) {
$found = true;
break;
}
}
if (!$found && !empty($script_path)) {
$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 synchronisé.\n";
} else {
echo "[INFO] Crontab déjà à jour.\n";
}
}
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);
}
}
}
}