Skip to content

Commit 0d3a396

Browse files
fix: count only physical disks in host Block I/O stats (Dokploy#5540)
* fix: count only physical disks in host Block I/O stats and skip partitioned md arrays and test the Block I/O sum
1 parent 560e790 commit 0d3a396

2 files changed

Lines changed: 119 additions & 7 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import {
2+
getHostSystemStats,
3+
isPhysicalDisk,
4+
} from "@dokploy/server/monitoring/utils";
5+
import { describe, expect, it, vi } from "vitest";
6+
7+
const GiB = 1024 ** 3;
8+
const diskStat = (device: string, readGiB: number, writeGiB: number) => ({
9+
device,
10+
readBytes: { toBytes: () => readGiB * GiB },
11+
writeBytes: { toBytes: () => writeGiB * GiB },
12+
});
13+
14+
vi.mock("node-os-utils", () => ({
15+
OSUtils: class {
16+
cpu = { usage: async () => ({ success: false }) };
17+
memory = { info: async () => ({ success: false }) };
18+
network = { overview: async () => ({ success: false }) };
19+
disk = {
20+
stats: async () => ({
21+
success: true,
22+
data: [
23+
diskStat("nvme0n1", 50, 100),
24+
diskStat("nvme0n1p1", 50, 100),
25+
diskStat("nvme1n1", 50, 100),
26+
diskStat("nvme1n1p1", 50, 100),
27+
diskStat("md0", 50, 100),
28+
diskStat("md0p1", 50, 100),
29+
diskStat("dm-0", 50, 100),
30+
diskStat("zram0", 10, 20),
31+
],
32+
}),
33+
};
34+
},
35+
}));
36+
37+
describe("isPhysicalDisk (#5513, #5385)", () => {
38+
it.each([
39+
"sda",
40+
"sdab",
41+
"hda",
42+
"vda",
43+
"xvda",
44+
"nvme0n1",
45+
"nvme1n1",
46+
"mmcblk0",
47+
])("counts whole disk %s", (device) => {
48+
expect(isPhysicalDisk(device)).toBe(true);
49+
});
50+
51+
it.each([
52+
"sda1",
53+
"sdab12",
54+
"hda1",
55+
"vda1",
56+
"xvda1",
57+
"nvme0n1p1",
58+
"nvme1n1p4",
59+
"mmcblk0p1",
60+
])("skips partition %s", (device) => {
61+
expect(isPhysicalDisk(device)).toBe(false);
62+
});
63+
64+
it.each(["md0", "md127", "md0p1", "md_d0p1", "dm-0", "dm-12"])(
65+
"skips stacked device %s",
66+
(device) => {
67+
expect(isPhysicalDisk(device)).toBe(false);
68+
},
69+
);
70+
71+
it.each(["loop0", "ram0", "zram0", "sr0", "fd0"])(
72+
"skips virtual device %s",
73+
(device) => {
74+
expect(isPhysicalDisk(device)).toBe(false);
75+
},
76+
);
77+
78+
it("counts each write once on an mdadm RAID1 host", () => {
79+
const devices = [
80+
"nvme0n1",
81+
"nvme0n1p1",
82+
"nvme0n1p2",
83+
"nvme0n1p3",
84+
"nvme0n1p4",
85+
"nvme1n1",
86+
"nvme1n1p1",
87+
"nvme1n1p2",
88+
"nvme1n1p3",
89+
"nvme1n1p4",
90+
"md0",
91+
"md1",
92+
"md2",
93+
"md3",
94+
];
95+
expect(devices.filter(isPhysicalDisk)).toEqual(["nvme0n1", "nvme1n1"]);
96+
});
97+
});
98+
99+
describe("getHostSystemStats Block I/O (#5513, #5385)", () => {
100+
it("sums only whole physical disks", async () => {
101+
const stats = await getHostSystemStats();
102+
expect(stats.BlockIO).toBe("100.00GiB / 200.00GiB");
103+
});
104+
});

‎packages/server/src/monitoring/utils.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ export const recordAdvancedStats = async (
5959
}
6060
};
6161

62+
// Filter out virtual devices (loop, ram, sr, etc.) - only include real disk devices
63+
const NON_PHYSICAL_DEVICE_PATTERNS = [
64+
/^(loop|ram|zram)/,
65+
/^(sr|fd)\d+$/,
66+
// Partitions and md/dm devices repeat I/O already counted on the underlying disk.
67+
/^(sd|hd|vd|xvd)[a-z]+\d+$/,
68+
/^(nvme\d+n\d+|mmcblk\d+)p\d+$/,
69+
/^(md|dm-)/,
70+
];
71+
72+
export const isPhysicalDisk = (device: string) =>
73+
!NON_PHYSICAL_DEVICE_PATTERNS.some((pattern) => pattern.test(device));
74+
6275
/**
6376
* Get host system statistics using node-os-utils
6477
* This is used when monitoring "dokploy" to show host stats instead of container stats
@@ -99,14 +112,9 @@ export const getHostSystemStats = async (): Promise<Container> => {
99112
let blockWriteBytes = 0;
100113
const diskStats = await osutils.disk.stats();
101114
if (diskStats.success && diskStats.data.length > 0) {
102-
// Filter out virtual devices (loop, ram, sr, etc.) - only include real disk devices
103-
const excludePatterns = [/^loop/, /^ram/, /^sr\d+$/, /^fd\d+$/];
104115
for (const stat of diskStats.data) {
105-
// Skip virtual devices
106-
if (
107-
stat.device &&
108-
excludePatterns.some((pattern) => pattern.test(stat.device))
109-
) {
116+
// Skip virtual, partition and stacked devices
117+
if (stat.device && !isPhysicalDisk(stat.device)) {
110118
continue;
111119
}
112120
// readBytes and writeBytes are DataSize objects with .toBytes() method

0 commit comments

Comments
 (0)