Files
html/api/health-ollama.php
2026-04-16 02:28:32 +02:00

49 lines
1.6 KiB
PHP

<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
// S204 Ollama (local)
$s204_ok = false;
$s204_models = 0;
$ch = curl_init("http://127.0.0.1:11434/api/tags");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 3]);
$r = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code == 200 && $r) {
$d = json_decode($r, true);
$s204_models = count($d["models"] ?? []);
$s204_ok = true;
}
// S151 Ollama (via SSH tunnel or direct)
$s151_ok = false;
$s151_models = 0;
// Try direct first (S151 Ollama on default port 11434)
$ch2 = curl_init("http://151.80.235.110:11434/api/tags");
curl_setopt_array($ch2, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 3, CURLOPT_CONNECTTIMEOUT => 2]);
$r2 = curl_exec($ch2);
$code2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
curl_close($ch2);
if ($code2 == 200 && $r2) {
$d2 = json_decode($r2, true);
$s151_models = count($d2["models"] ?? []);
$s151_ok = true;
} else {
// Try via SSH
$ssh = trim(shell_exec("timeout 4 ssh -o ConnectTimeout=2 -o BatchMode=yes -o StrictHostKeyChecking=no ubuntu@151.80.235.110 \"curl -sf http://127.0.0.1:11434/api/tags 2>/dev/null\" 2>/dev/null"));
if ($ssh) {
$d3 = json_decode($ssh, true);
$s151_models = count($d3["models"] ?? []);
$s151_ok = true;
}
}
$status = "ok";
if (!$s204_ok) $status = "degraded";
echo json_encode([
"status" => $status,
"s204" => ["up" => $s204_ok, "models" => $s204_models, "port" => 11434],
"s151" => ["up" => $s151_ok, "models" => $s151_models, "port" => 11434],
]);