diff --git a/api/endpoints/facts_collector.php b/api/endpoints/facts_collector.php index 0b36690..c81cd7f 100644 --- a/api/endpoints/facts_collector.php +++ b/api/endpoints/facts_collector.php @@ -80,26 +80,15 @@ function collect_all(): array { $results['system'] = 'error: ' . $e->getMessage(); } - // ── Network ─────────────────────────────────────────────────────────── + // ── Network — read from agent DB (agents push status, DO can't ping LAN IPs) ── try { - $watchlist = [ - 'gateway' => '10.48.200.1', - 'proxmox' => '10.48.200.90', - 'ollama' => '10.48.200.95', - 'fusionpbx' => '10.48.200.96', - 'ha' => '10.48.200.97', - 'do_server' => '165.22.1.228', - ]; - $online = 0; - $total = count($watchlist); - foreach ($watchlist as $name => $ip) { - exec('ping -c1 -W1 ' . escapeshellarg($ip) . ' > /dev/null 2>&1', $o, $code); - $up = ($code === 0); - if ($up) $online++; - KBEngine::storeFact('network', "host_{$name}", $up ? 'online' : 'offline', $ip, $ttl); - } - KBEngine::storeFact('network', 'online_count', $online, 'local', $ttl); - KBEngine::storeFact('network', 'total_count', $total, 'local', $ttl); + $rows = JarvisDB::query( + "SELECT status FROM registered_agents WHERE last_seen > DATE_SUB(NOW(), INTERVAL 5 MINUTE)" + ); + $online = count(array_filter($rows, fn($r) => $r['status'] === 'online')); + $total = count($rows); + KBEngine::storeFact('network', 'online_count', $online, 'local', $ttl); + KBEngine::storeFact('network', 'total_count', $total, 'local', $ttl); KBEngine::storeFact('network', 'gateway_status', $online > 0 ? 'online' : 'offline', 'local', $ttl); $results['network'] = "ok ({$online}/{$total} online)"; } catch (Exception $e) { @@ -160,7 +149,7 @@ function collect_all(): array { } else try { $ollamaHost = defined('OLLAMA_HOST') ? OLLAMA_HOST : 'http://10.48.200.95:11434'; $ch = curl_init($ollamaHost . '/api/tags'); - curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5]); + curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 3]); $resp = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); @@ -210,6 +199,7 @@ function collect_all(): array { $ch = curl_init($localUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 5, @@ -240,9 +230,10 @@ function pve_api_get(string $url, string $authHeader): array { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_HTTPHEADER => [$authHeader], CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_TIMEOUT => 8, + CURLOPT_TIMEOUT => 5, ]); $resp = curl_exec($ch); curl_close($ch);