Remote cache: extend to monitor stats; vv_local_host_stats(), 2h bg refresh covers monitor+arrs pages

This commit is contained in:
Gmer4Lfe
2026-06-03 18:20:55 -04:00
parent 2223cf80f7
commit a55a3afad5
3 changed files with 91 additions and 10 deletions
+12
View File
@@ -613,6 +613,18 @@ function vv_remote_hosts_stats(): array {
$results = [];
foreach ($hostIds as $id) {
if (strtolower($id) === strtolower($myHost)) continue;
// Background cache written by remote_arr_cache_writer.sh every 2h — use it if present.
$bgCache = VV_CACHE_DIR . '/monitor_remote_' . strtolower($id) . '.json';
if (file_exists($bgCache)) {
$cached = json_decode(file_get_contents($bgCache), true);
if ($cached) {
$cached['cache_age'] = time() - (int)filemtime($bgCache);
$results[$id] = $cached;
continue;
}
}
// No background cache yet — fall back to live call (uses 30s inline cache).
$key = $vars[strtoupper($id) . '_UNRAID_API_KEY'] ?? '';
if (!$key) {
$results[$id] = ['available' => false, 'no_api_key' => true,
+51
View File
@@ -187,6 +187,57 @@ function vv_api_node_metrics(?array $d): array {
];
}
// ── Local host stats snapshot — same shape as one entry from vv_remote_hosts_stats() ─────────
// Called locally and via SSH by remote_arr_cache_writer.sh on remote hosts.
function vv_local_host_stats(): array {
$host = vv_detect_host();
$myId = strtoupper($host);
$vars = vv_conf_vars();
$name = $vars[$myId] ?? gethostname();
$api = vv_api_data();
$metrics = vv_api_node_metrics($api);
if (!$api) {
return ['available' => false, 'host_id' => $myId, 'hostname' => $name,
'no_api_key' => _vv_api_key_missing()];
}
$os = $api['info']['os'] ?? [];
$cpu = $api['info']['cpu'] ?? [];
$mem = $api['metrics']['memory'] ?? [];
$memPct = round((float)($mem['percentTotal'] ?? 0));
if ($memPct === 0) {
$tot = (float)($mem['total'] ?? 0); $avail = (float)($mem['available'] ?? 0);
$memPct = $tot > 0 ? (int)round(($tot - $avail) / $tot * 100) : 0;
}
$memTotalGb = isset($mem['total']) ? _vv_api_bytes_to_gb((float)$mem['total']) : 0;
$uptimeRaw = $os['uptime'] ?? '';
if (is_numeric($uptimeRaw)) {
$s = (int)$uptimeRaw;
$d = intdiv($s, 86400); $h = intdiv($s % 86400, 3600); $m = intdiv($s % 3600, 60);
$uptime = ($d ? "{$d}d " : '') . ($h ? "{$h}h " : '') . "{$m}m";
} else {
$s = 0; $uptime = $uptimeRaw ?: '—';
}
return array_merge([
'available' => true,
'host_id' => $myId,
'hostname' => $os['hostname'] ?? $name,
'version' => $os['release'] ?? '',
'uptime' => $uptime,
'uptime_sec' => $s,
'cpu_load' => $metrics['cpu_pct'] ?? 0,
'cpu_threads' => (int)($cpu['threads'] ?? 0),
'mem_total_gb' => $memTotalGb,
'mem_used_pct' => $memPct,
'array_state' => $api['array']['state'] ?? 'UNKNOWN',
], $metrics);
}
// ── Confirmed schema (Unraid 7.3, introspected 2026-05-31) ───────────────────
// Adding a new host: add HOSTn="hostname" to master.conf and HOSTn_UNRAID_API_KEY
// to hostn.conf, then run Deployment/deploy.sh. No schema work needed.