Compare commits

..
Author SHA1 Message Date
ajnart a37e101bba fix(docker): normalize container CPU usage 2026-09-06 10:54:05 +02:00
4 changed files with 24 additions and 10 deletions

No files matched your search

@@ -39,8 +39,9 @@ export class UnraidIntegration extends Integration implements ISystemHealthMonit
const cpuUtilization = systemInfo.metrics.cpu.cpus.reduce((acc, val) => acc + val.percentTotal, 0); const cpuUtilization = systemInfo.metrics.cpu.cpus.reduce((acc, val) => acc + val.percentTotal, 0);
const cpuCount = systemInfo.info.cpu.cores; const cpuCount = systemInfo.info.cpu.cores;
const totalMemory = systemInfo.metrics.memory.total; // We use "info" object instead of the stats since this is the exact amount the kernel sees, which is what Unraid displays.
const usedMemory = Math.max(totalMemory - systemInfo.metrics.memory.available, 0); const totalMemory = systemInfo.info.memory.layout.reduce((acc, layout) => layout.size + acc, 0);
const usedMemory = totalMemory * (systemInfo.metrics.memory.percentTotal / 100);
const uptime = dayjs(systemInfo.info.os.uptime); const uptime = dayjs(systemInfo.info.os.uptime);
return { return {
@@ -135,6 +136,11 @@ export class UnraidIntegration extends Integration implements ISystemHealthMonit
brand, brand,
cores, cores,
threads threads
},
memory {
layout {
size
}
} }
} }
} }
@@ -60,6 +60,13 @@ export const unraidSystemInfoSchema = z.object({
cores: z.number(), cores: z.number(),
threads: z.number(), threads: z.number(),
}), }),
memory: z.object({
layout: z.array(
z.object({
size: z.number(),
}),
),
}),
}), }),
}); });
+5 -4
View File
@@ -481,13 +481,14 @@ export function calculateCpuUsage(stats: ContainerStats): number {
return 0; return 0;
} }
const numberOfCpus = stats.cpu_stats.online_cpus; const cpuDelta = stats.cpu_stats.cpu_usage.total_usage - (stats.precpu_stats?.cpu_usage?.total_usage ?? 0);
const usage = stats.cpu_stats.system_cpu_usage; const systemDelta = (stats.cpu_stats.system_cpu_usage ?? 0) - (stats.precpu_stats?.system_cpu_usage ?? 0);
if (!usage || usage === 0) {
if (systemDelta <= 0 || cpuDelta < 0) {
return 0; return 0;
} }
return (stats.cpu_stats.cpu_usage.total_usage / usage) * numberOfCpus * 100; return (cpuDelta / systemDelta) * 100;
} }
export function calculateMemoryUsage(stats: ContainerStats): number { export function calculateMemoryUsage(stats: ContainerStats): number {
@@ -72,16 +72,16 @@ describe("calculateCpuUsage", () => {
const stats = createStats({ const stats = createStats({
cpu_stats: { online_cpus: 4, cpu_usage: { total_usage: 2000 }, system_cpu_usage: 10000 }, cpu_stats: { online_cpus: 4, cpu_usage: { total_usage: 2000 }, system_cpu_usage: 10000 },
}); });
// (2000 / 10000) * 4 * 100 = 80 // (2000 / 10000) * 100 = 20
expect(calculateCpuUsage(stats)).toBe(80); expect(calculateCpuUsage(stats)).toBe(20);
}); });
test("should handle fractional CPU usage", () => { test("should handle fractional CPU usage", () => {
const stats = createStats({ const stats = createStats({
cpu_stats: { online_cpus: 2, cpu_usage: { total_usage: 500 }, system_cpu_usage: 100000 }, cpu_stats: { online_cpus: 2, cpu_usage: { total_usage: 500 }, system_cpu_usage: 100000 },
}); });
// (500 / 100000) * 2 * 100 = 1 // (500 / 100000) * 100 = 0.5
expect(calculateCpuUsage(stats)).toBe(1); expect(calculateCpuUsage(stats)).toBe(0.5);
}); });
}); });