/** * Dashboard Beszel Simple - Version sans graphiques avec rafraîchissement automatique * Fonctionnalités : * - Données en temps réel sans graphiques * - Rafraîchissement automatique configurable * - Options persistantes * - Interface simple et rapide */ // === CONFIGURATION === $baseUrl = 'URL'; $username = 'mail'; $password = 'rpass'; // === CONFIGURATION DES COLLECTIONS === $collections = [ 'metrics' => 'system_stats', 'alerts' => 'alerts', 'systems' => 'systems' ]; // === RÉCUPÉRATION DES PRÉFÉRENCES === $refreshInterval = $_GET['refresh'] ?? $_COOKIE['beszel_refresh'] ?? '30'; $autoRefresh = $_GET['auto'] ?? $_COOKIE['beszel_auto'] ?? 'true'; $theme = $_GET['theme'] ?? $_COOKIE['beszel_theme'] ?? 'light'; $viewMode = $_GET['view'] ?? $_COOKIE['beszel_view'] ?? 'cards'; // Forcer le rafraîchissement automatique si pas défini if (!isset($_COOKIE['beszel_auto'])) { $autoRefresh = 'true'; } // Sauvegarder les préférences if (isset($_GET['refresh'])) setcookie('beszel_refresh', $refreshInterval, time() + (365 * 24 * 60 * 60)); if (isset($_GET['auto'])) setcookie('beszel_auto', $autoRefresh, time() + (365 * 24 * 60 * 60)); if (isset($_GET['theme'])) setcookie('beszel_theme', $theme, time() + (365 * 24 * 60 * 60)); if (isset($_GET['view'])) setcookie('beszel_view', $viewMode, time() + (365 * 24 * 60 * 60)); // === FONCTIONS UTILITAIRES === function getToken() { global $baseUrl, $username, $password; $ch = curl_init($baseUrl . '/api/collections/users/auth-with-password'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'identity' => $username, 'password' => $password ]) ]); $resp = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code !== 200) die("Erreur auth ($code): $resp"); $data = json_decode($resp, true); return $data['token'] ?? null; } function apiGet($endpoint, $token) { global $baseUrl; $ch = curl_init($baseUrl . $endpoint); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer $token", "Content-Type: application/json" ] ]); $resp = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code !== 200) { error_log("Erreur API ($endpoint) : $code\n$resp"); return ['items' => []]; } return json_decode($resp, true); } function formatBytes($bytes, $precision = 2) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) { $bytes /= 1024; } return round($bytes, $precision) . ' ' . $units[$i]; } function getStatusColor($status) { switch ($status) { case 'up': return '#10b981'; case 'down': return '#ef4444'; case 'paused': return '#f59e0b'; default: return '#6b7280'; } } function getStatusIcon($status) { switch ($status) { case 'up': return '🟢'; case 'down': return '🔴'; case 'paused': return '⏸️'; default: return '⚪'; } } function getCpuColorClass($cpu) { if ($cpu < 30) return 'cpu-low'; if ($cpu < 70) return 'cpu-medium'; return 'cpu-high'; } function getLoadColorClass($load) { if ($load < 1) return 'load-low'; if ($load < 3) return 'load-medium'; return 'load-high'; } function getTempColorClass($temp) { if ($temp < 40) return 'temp-low'; if ($temp < 70) return 'temp-medium'; return 'temp-high'; } function getDataFreshnessClass($lastUpdate) { $now = time(); $dataTime = strtotime($lastUpdate); $age = $now - $dataTime; if ($age < 60) return 'data-fresh'; // < 1 minute if ($age < 300) return 'data-recent'; // < 5 minutes return 'data-old'; // > 5 minutes } function getProgressColorClass($value, $type) { switch ($type) { case 'cpu': if ($value < 30) return 'cpu-low'; if ($value < 70) return 'cpu-medium'; return 'cpu-high'; case 'memory': if ($value < 30) return 'memory-low'; if ($value < 70) return 'memory-medium'; return 'memory-high'; case 'load': if ($value < 1) return 'load-low'; if ($value < 3) return 'load-medium'; return 'load-high'; case 'temp': if ($value < 40) return 'temp-low'; if ($value < 70) return 'temp-medium'; return 'temp-high'; case 'disk': if ($value < 30) return 'disk-low'; if ($value < 70) return 'disk-medium'; return 'disk-high'; case 'gpu': if ($value < 30) return 'gpu-low'; if ($value < 70) return 'gpu-medium'; return 'gpu-high'; default: return 'cpu-low'; } } function getMetricColorClass($value, $type) { switch ($type) { case 'cpu': if ($value < 30) return 'cpu-low'; if ($value < 70) return 'cpu-medium'; return 'cpu-high'; case 'memory': if ($value < 30) return 'memory-low'; if ($value < 70) return 'memory-medium'; return 'memory-high'; case 'load': if ($value < 1) return 'load-low'; if ($value < 3) return 'load-medium'; return 'load-high'; case 'temp': if ($value < 40) return 'temp-low'; if ($value < 70) return 'temp-medium'; return 'temp-high'; case 'disk': if ($value < 30) return 'disk-low'; if ($value < 70) return 'disk-medium'; return 'disk-high'; case 'gpu': if ($value < 30) return 'gpu-low'; if ($value < 70) return 'gpu-medium'; return 'gpu-high'; default: return 'cpu-low'; } } // === RÉCUPÉRATION DES DONNÉES === $token = getToken(); if (!$token) { die("❌ Impossible d'obtenir le token d'authentification."); } $data = apiGet('/api/collections/systems/records?perPage=50&expand=latest_metrics', $token); $systems = $data['items'] ?? []; // Récupération des alertes $alertsData = apiGet('/api/collections/alerts/records?sort=-created&perPage=20', $token); $alerts = $alertsData['items'] ?? []; // === ENVOI MQTT VERS HOME ASSISTANT === $mqttHost = "192.168.1.200"; // IP de ton Home Assistant $mqttUser = "mqtt32mqtt32"; // identifiants MQTT $mqttPass = "mqtt32"; // Boucle sur chaque système foreach ($systems as $sys) { $name = preg_replace('/[^a-zA-Z0-9_-]/', '_', strtolower($sys['name'] ?? 'unknown')); $info = $sys['info'] ?? []; $payload = [ 'cpu' => $info['cpu'] ?? 0, 'ram' => $info['mp'] ?? 0, 'load' => $info['la'][0] ?? 0, 'temp' => $info['t'] ?? 0, 'disk' => $info['dp'] ?? 0, 'gpu' => $info['g'] ?? 0, 'status' => $sys['status'] ?? 'unknown', 'time' => date('c') ]; $json = json_encode($payload); // Envoi sur un topic dédié au système exec("mosquitto_pub -h $mqttHost -u $mqttUser -P $mqttPass -t beszel/$name -m '$json'"); } ?>