Files
scripts-bash/servers/linux/monitoring/bin/monitoring-update.php
2026-03-18 08:27:05 +01:00

129 lines
3.9 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/**
* Moteur de mise à jour - Version Simplifiée et Autonome
* Pilotage du script Bash + Initialisation des Configs + Cron
*/
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";
$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);
}
// 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; // Affichage en temps réel à l'écran
if (strpos($line, '[ERR]') !== false) {
log_error("update_process_error", trim($line));
}
}
}
$exit_code = pclose($handle);
} else {
$exit_code = 1;
}
if ($exit_code === 0) {
echo "\e[1m--- Finalisation des configurations ---\e[0m\n";
// 2. Initialisation des fichiers .local manquants
ensure_local_configs();
// 3. Vérification du Crontab
ensure_crontab_entries();
log_info("update_finished", "Mise à jour réussie");
echo "\e[32m[OK]\e[0m Système à jour et configuré.\n";
} else {
log_error("update_failed", "Le script de synchronisation a échoué", ["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_BASE_DIR;
$conf_dir = $MONITORING_BASE_DIR . '/conf';
$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 = $conf_dir . '/' . $src;
$dst_path = $conf_dir . '/' . $dst;
if (!file_exists($dst_path) && file_exists($src_path)) {
if (copy($src_path, $dst_path)) {
chmod($dst_path, 0600);
log_notice("config_auto_init", "Création auto de $dst");
echo "\e[32m[OK]\e[0m Fichier créé : $dst\n";
}
}
}
}
/**
* Assure la présence des tâches dans le crontab de root
*/
function ensure_crontab_entries() {
global $MONITORING_BASE_DIR;
$required_jobs = [
"*/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",
"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));
$updated = false;
foreach ($required_jobs as $job) {
$parts = explode(' ', $job);
$script_path = $parts[5]; // Chemin du script
$found = false;
foreach ($lines as $line) {
if (strpos($line, $script_path) !== false) {
$found = true;
break;
}
}
if (!$found) {
$lines[] = $job;
$updated = true;
echo "\e[32m[OK]\e[0m Ajout au cron : " . basename($script_path) . "\n";
}
}
if ($updated) {
$tmp_cron = tempnam(sys_get_temp_dir(), 'cron');
file_put_contents($tmp_cron, implode("\n", $lines) . "\n");
exec("crontab " . escapeshellarg($tmp_cron));
unlink($tmp_cron);
}
}
exit_with_status();