Add homelab_server_boot_time_seconds to /metrics
When each agent-reported server last started (last report minus uptime), so uptime is time() - the metric and a reboot shows as it moving. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
41fbbf3467
commit
48aea747bc
3 files changed
+8
No files matched your search
@@ -259,6 +259,7 @@ All gauges. The same table is checked by a test against what `/metrics` really s
|
|||||||
| `homelab_info` | `environment` | Always 1; says which installation this is. |
|
| `homelab_info` | `environment` | Always 1; says which installation this is. |
|
||||||
| `homelab_server_up` | `server` | 1 if the agent has reported within the offline threshold (Settings → Notifications, or the server's own limit if it has one), else 0. Servers that never reported are left out. |
|
| `homelab_server_up` | `server` | 1 if the agent has reported within the offline threshold (Settings → Notifications, or the server's own limit if it has one), else 0. Servers that never reported are left out. |
|
||||||
| `homelab_server_last_seen_timestamp_seconds` | `server` | When the agent last reported. |
|
| `homelab_server_last_seen_timestamp_seconds` | `server` | When the agent last reported. |
|
||||||
|
| `homelab_server_boot_time_seconds` | `server` | When the server last started, as a Unix time, worked out from the uptime its agent reports (Linux agent 8, Windows agent 7 and later). How long it has been up is `time() - homelab_server_boot_time_seconds`; the value only moves when the server reboots. |
|
||||||
| `homelab_server_cpu_load_percent` | `server` | CPU load as the agent reports it. |
|
| `homelab_server_cpu_load_percent` | `server` | CPU load as the agent reports it. |
|
||||||
| `homelab_server_memory_used_percent` | `server` | Memory in use. |
|
| `homelab_server_memory_used_percent` | `server` | Memory in use. |
|
||||||
| `homelab_server_disk_used_percent` | `server`, `mount` | How full each disk is. |
|
| `homelab_server_disk_used_percent` | `server`, `mount` | How full each disk is. |
|
||||||
|
|||||||
@@ -509,6 +509,8 @@ export async function buildMetrics(now: Date = new Date()): Promise<string> {
|
|||||||
const seen = Date.parse(s.lastSeenAt);
|
const seen = Date.parse(s.lastSeenAt);
|
||||||
out.add("homelab_server_up", "gauge", "1 if the server's agent has reported within the offline threshold, else 0.", now.getTime() - seen <= offlineAfterMs ? 1 : 0, { server });
|
out.add("homelab_server_up", "gauge", "1 if the server's agent has reported within the offline threshold, else 0.", now.getTime() - seen <= offlineAfterMs ? 1 : 0, { server });
|
||||||
out.add("homelab_server_last_seen_timestamp_seconds", "gauge", "When the server's agent last reported, as a Unix time.", Math.floor(seen / 1000), { server });
|
out.add("homelab_server_last_seen_timestamp_seconds", "gauge", "When the server's agent last reported, as a Unix time.", Math.floor(seen / 1000), { server });
|
||||||
|
// As node_exporter does: when it started, not how long ago, so the figure holds still between reports (uptime is time() - this) and a reboot shows as it moving.
|
||||||
|
if (s.uptimeSeconds !== null) out.add("homelab_server_boot_time_seconds", "gauge", "When the server last started, as a Unix time (from the uptime its agent reports); uptime is time() minus this.", Math.floor(seen / 1000) - s.uptimeSeconds, { server });
|
||||||
if (s.cpuLoadPercent !== null) out.add("homelab_server_cpu_load_percent", "gauge", "CPU load as the agent reports it.", s.cpuLoadPercent, { server });
|
if (s.cpuLoadPercent !== null) out.add("homelab_server_cpu_load_percent", "gauge", "CPU load as the agent reports it.", s.cpuLoadPercent, { server });
|
||||||
if (s.memTotalBytes && s.memUsedBytes !== null) out.add("homelab_server_memory_used_percent", "gauge", "Memory in use.", (s.memUsedBytes / s.memTotalBytes) * 100, { server });
|
if (s.memTotalBytes && s.memUsedBytes !== null) out.add("homelab_server_memory_used_percent", "gauge", "Memory in use.", (s.memUsedBytes / s.memTotalBytes) * 100, { server });
|
||||||
let disks: { mount: string; sizeBytes: number; usedBytes: number }[] = [];
|
let disks: { mount: string; sizeBytes: number; usedBytes: number }[] = [];
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ describe("metrics and webhooks", () => {
|
|||||||
apiTokenPrefix: "a",
|
apiTokenPrefix: "a",
|
||||||
lastSeenAt: now.toISOString(),
|
lastSeenAt: now.toISOString(),
|
||||||
cpuLoadPercent: 12.5,
|
cpuLoadPercent: 12.5,
|
||||||
|
uptimeSeconds: 86_400,
|
||||||
memTotalBytes: 1000,
|
memTotalBytes: 1000,
|
||||||
memUsedBytes: 250,
|
memUsedBytes: 250,
|
||||||
disks: JSON.stringify([{ mount: "C:\\", sizeBytes: 100, usedBytes: 40 }]),
|
disks: JSON.stringify([{ mount: "C:\\", sizeBytes: 100, usedBytes: 40 }]),
|
||||||
@@ -161,6 +162,10 @@ describe("metrics and webhooks", () => {
|
|||||||
assert.ok(!text.includes('server="never"'), "a server that never reported has nothing to say");
|
assert.ok(!text.includes('server="never"'), "a server that never reported has nothing to say");
|
||||||
assert.match(text, /homelab_server_cpu_load_percent\{server="elsa \\"main\\""\} 12\.5/);
|
assert.match(text, /homelab_server_cpu_load_percent\{server="elsa \\"main\\""\} 12\.5/);
|
||||||
assert.match(text, /homelab_server_memory_used_percent\{server="elsa \\"main\\""\} 25/);
|
assert.match(text, /homelab_server_memory_used_percent\{server="elsa \\"main\\""\} 25/);
|
||||||
|
const boot = /homelab_server_boot_time_seconds\{server="elsa \\"main\\""\} (\d+)/.exec(text);
|
||||||
|
assert.ok(boot, "a server that says how long it has been up gets a boot time");
|
||||||
|
assert.ok(Math.abs(Number(boot[1]) - (Math.floor(Date.now() / 1000) - 86_400)) < 120, "the moment of its last report minus the uptime");
|
||||||
|
assert.ok(!text.includes('homelab_server_boot_time_seconds{server="anna"}'), "one that does not say gets none");
|
||||||
assert.match(text, /homelab_server_disk_used_percent\{server="elsa \\"main\\"",mount="C:\\\\"\} 40/, "a backslash in a mount is escaped");
|
assert.match(text, /homelab_server_disk_used_percent\{server="elsa \\"main\\"",mount="C:\\\\"\} 40/, "a backslash in a mount is escaped");
|
||||||
assert.match(text, /homelab_server_pending_updates\{server="elsa \\"main\\""\} 7/);
|
assert.match(text, /homelab_server_pending_updates\{server="elsa \\"main\\""\} 7/);
|
||||||
assert.match(text, /homelab_server_pending_security_updates\{server="elsa \\"main\\""\} 2/);
|
assert.match(text, /homelab_server_pending_security_updates\{server="elsa \\"main\\""\} 2/);
|
||||||
|
|||||||
Reference in new issue
Block a user