Files
nano_metrics/agent/src/metrics/disk.rs
T
Gilles Soulier 243c97d71b fix: disque via statvfs() — valeurs identiques à df
Remplace sysinfo::Disks par un appel direct à libc::statvfs("/").
- used  = (f_blocks − f_bfree) × f_frsize  → correspond à df "Utilisé"
- free  = f_bavail × f_frsize              → correspond à df "Dispo"
- total = f_blocks × f_frsize

Avant (sysinfo) : used comptait les blocs réservés root → surestimation de ~3-4 Go.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 20:21:58 +02:00

37 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::mem::MaybeUninit;
/// Retourne (used, free, total) en octets pour le système de fichiers racine "/".
/// Utilise statvfs() directement pour correspondre exactement aux chiffres de `df` :
/// - total = f_blocks × f_frsize
/// - used = (f_blocks f_bfree) × f_frsize (blocs effectivement écrits)
/// - free = f_bavail × f_frsize (disponible pour utilisateurs non-root)
pub fn get() -> (u64, u64, u64) {
let path = b"/\0";
let mut stat = MaybeUninit::<libc::statvfs>::uninit();
let ret = unsafe { libc::statvfs(path.as_ptr() as *const libc::c_char, stat.as_mut_ptr()) };
if ret != 0 {
return (0, 0, 0);
}
let stat = unsafe { stat.assume_init() };
let bsize = stat.f_frsize as u64;
let total = stat.f_blocks.saturating_mul(bsize);
let used = stat.f_blocks.saturating_sub(stat.f_bfree).saturating_mul(bsize);
let free = stat.f_bavail.saturating_mul(bsize);
(used, free, total)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_disk_coherent() {
let (used, free, total) = get();
eprintln!("résultat statvfs : used={used} free={free} total={total}");
if total > 0 {
assert!(used + free <= total + 1024 * 1024, "used + free > total");
assert!(total > 0, "total doit être > 0");
}
}
}