-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.rs
More file actions
68 lines (63 loc) · 2.21 KB
/
disk.rs
File metadata and controls
68 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct Disk {
pub name: String,
pub mount_point: String,
pub total_space: u64,
pub used_space: u64,
pub available_space: u64,
pub usage_percent: f64,
}
impl Disk {
#[tracing::instrument(name = "Disk::collect_all")]
pub fn collect_all() -> Vec<Self> {
let disks = sysinfo::Disks::new_with_refreshed_list();
if disks.list().is_empty() {
tracing::info!("no disks found");
}
disks.list().iter().map(Self::from).collect()
}
}
impl From<&sysinfo::Disk> for Disk {
fn from(sysinfo_disk: &sysinfo::Disk) -> Self {
let total_space = sysinfo_disk.total_space();
let available_space = sysinfo_disk.available_space();
// There shouldn't be negative used bytes. We prevent underflow, to not falsely report more
// used than total space.
let used_space = total_space.saturating_sub(available_space);
let usage_percent = match used_space {
0 => 0.0,
used_space => used_space as f64 / total_space as f64 * 100.0,
};
let disk = Disk {
name: sysinfo_disk.name().to_string_lossy().into_owned(),
mount_point: sysinfo_disk.mount_point().to_string_lossy().into_owned(),
total_space,
used_space,
available_space,
usage_percent,
};
if usage_percent >= 85.0 {
tracing::warn!(
disk.mount_point,
disk.name,
disk.space.total = disk.total_space,
disk.space.used = disk.used_space,
disk.space.available = disk.available_space,
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
"disk usage high"
);
} else {
tracing::info!(
disk.mount_point,
disk.name,
disk.space.total = disk.total_space,
disk.space.used = disk.used_space,
disk.space.available = disk.available_space,
disk.space.usage_percent = format!("{:.1}%", disk.usage_percent),
"found disk"
);
}
disk
}
}