Files
micalis.net/status.php
T
2026-07-26 14:19:56 +02:00

96 lines
3.2 KiB
PHP

<?php
/**
* Status API pro MicalLab
* Kontroluje dostupnost služeb a vrací JSON
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
// Konfigurace služeb
$services = [
['id' => 'gitea', 'name' => 'Gitea', 'url' => 'https://gitea.micalis.net/', 'timeout' => 5],
['id' => 'nextcloud', 'name' => 'Nextcloud', 'url' => 'https://nextcloud.micalis.net/', 'timeout' => 5],
['id' => 'ai', 'name' => 'AI', 'url' => 'https://ai.micalis.net/', 'timeout' => 5],
['id' => 'jellyfin', 'name' => 'Jellyfin', 'url' => 'https://jellyfin.micalis.net/', 'timeout' => 5],
['id' => 'amp', 'name' => 'AMP Game Panel', 'url' => 'https://amp.micalis.net/', 'timeout' => 5],
['id' => 'fluxer', 'name' => 'Fluxer', 'url' => 'https://chat.micalis.net/', 'timeout' => 5],
['id' => 'grafana', 'name' => 'Grafana', 'url' => 'https://grafana.micalis.net/', 'timeout' => 5],
['id' => 'kasm', 'name' => 'Kasm', 'url' => 'https://lightning.micalis.net/', 'timeout' => 5],
];
$cacheFile = __DIR__ . '/status_cache.json';
$cacheTTL = 60;
// Zkus načíst z cache
if (file_exists($cacheFile)) {
$cache = json_decode(file_get_contents($cacheFile), true);
if ($cache && isset($cache['timestamp']) && (time() - $cache['timestamp']) < $cacheTTL) {
echo json_encode($cache['data']);
exit;
}
}
// Funkce pro kontrolu služby
function checkService($url, $timeout = 5) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
//CURLOPT_NOBODY => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3,
]);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
// Timeout
if ($error && stripos($error, 'timeout') !== false) {
return ['status' => 'offline', 'message' => 'Timeout'];
}
// Connection error
if ($error || $httpCode === 0) {
return ['status' => 'offline', 'message' => 'Connection failed'];
}
// HTTP error (4xx, 5xx)
if ($httpCode >= 400) {
return ['status' => 'offline', 'message' => "HTTP $httpCode"];
}
return ['status' => 'online', 'http_code' => $httpCode, 'message' => 'OK'];
}
// Zkontroluj všechny služby
$results = [];
foreach ($services as $service) {
$check = checkService($service['url'], $service['timeout']);
$results[] = [
'id' => $service['id'],
'name' => $service['name'],
'status' => $check['status'],
'message' => $check['message'],
];
}
$onlineCount = count(array_filter($results, fn($r) => $r['status'] === 'online'));
$response = [
'timestamp' => time(),
'overall' => [
'status' => $onlineCount === count($results) ? 'all_online' : ($onlineCount > 0 ? 'partial' : 'all_offline'),
'online' => $onlineCount,
'total' => count($results),
],
'services' => $results,
];
file_put_contents($cacheFile, json_encode(['timestamp' => time(), 'data' => $response]));
echo json_encode($response);