Skip to content

Commit aff9bfd

Browse files
authored
feat: Add disk usage percentage and warn on high usage (#57)
* fix: Log GID instead of UID in user GID tracing field The tracing statement for `user.gid` was reading from `user.uid` instead of `user.gid`, causing the wrong value to be reported. * refactor: Use idiomatic empty check for disk collection Replace `into_iter().next().is_none()` with `list().is_empty()` for clarity, and use `list().iter()` for the actual collection. * fix: Remove stale .source() call with unused result This was likely a debugging leftover — the error source chain is already captured via the `successors` iterator below. * fix: Fix typo "proess" -> "process" in error message * fix: Replace unwrap() calls with error logging in collection loop JSON serialization and file write can fail at runtime (e.g. disk full). Log the error and continue the loop instead of crashing, since this tool may run continuously for hours. * fix: Use tokio::time::sleep instead of std::thread::sleep std::thread::sleep blocks the entire tokio worker thread. Since main is already async, use the non-blocking alternative. * fix: Handle DNS resolver initialization failure gracefully In a container debugging tool, broken DNS config (/etc/resolv.conf) is a likely scenario to diagnose. Log the error and skip DNS lookups instead of panicking. * fix: Wrap network collector in ComponentResult for consistent error handling The network collector silently swallowed interface listing errors by returning empty data. Now it returns Result so the orchestrator wraps it in ComponentResult, matching the pattern used by other fallible collectors. Errors appear in JSON output instead of being silently lost. * refactor: Use BTreeMap for deterministic JSON key ordering HashMap produces non-deterministic JSON output, making it hard to diff containerdebug output across runs. BTreeMap sorts keys consistently. * feat: Add disk usage percentage and warn on high usage Add `usage_percent` field to disk collection output. When a disk exceeds 85% usage, log at WARN level instead of INFO so it stands out in log aggregation systems. * Apply review suggestion
1 parent 6f71d96 commit aff9bfd

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/system_information/disk.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ pub struct Disk {
55
pub name: String,
66
pub mount_point: String,
77
pub total_space: u64,
8+
pub used_space: u64,
89
pub available_space: u64,
10+
pub usage_percent: f64,
911
}
1012

1113
impl Disk {
@@ -21,19 +23,46 @@ impl Disk {
2123

2224
impl From<&sysinfo::Disk> for Disk {
2325
fn from(sysinfo_disk: &sysinfo::Disk) -> Self {
26+
let total_space = sysinfo_disk.total_space();
27+
let available_space = sysinfo_disk.available_space();
28+
// There shouldn't be negative used bytes. We prevent underflow, to not falsely report more
29+
// used than total space.
30+
let used_space = total_space.saturating_sub(available_space);
31+
let usage_percent = match used_space {
32+
0 => 0.0,
33+
used_space => used_space as f64 / total_space as f64 * 100.0,
34+
};
35+
2436
let disk = Disk {
2537
name: sysinfo_disk.name().to_string_lossy().into_owned(),
2638
mount_point: sysinfo_disk.mount_point().to_string_lossy().into_owned(),
27-
total_space: sysinfo_disk.total_space(),
28-
available_space: sysinfo_disk.available_space(),
39+
total_space,
40+
used_space,
41+
available_space,
42+
usage_percent,
2943
};
30-
tracing::info!(
31-
disk.mount_point,
32-
disk.name,
33-
disk.space.total = disk.total_space,
34-
disk.space.available = disk.available_space,
35-
"found disk"
36-
);
44+
45+
if usage_percent >= 85.0 {
46+
tracing::warn!(
47+
disk.mount_point,
48+
disk.name,
49+
disk.space.total = disk.total_space,
50+
disk.space.used = disk.used_space,
51+
disk.space.available = disk.available_space,
52+
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
53+
"disk usage high"
54+
);
55+
} else {
56+
tracing::info!(
57+
disk.mount_point,
58+
disk.name,
59+
disk.space.total = disk.total_space,
60+
disk.space.used = disk.used_space,
61+
disk.space.available = disk.available_space,
62+
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
63+
"found disk"
64+
);
65+
}
3766
disk
3867
}
3968
}

0 commit comments

Comments
 (0)