added disk detection for tests

This commit is contained in:
2025-07-31 11:31:57 +02:00
parent 8a4df64d0c
commit f2432e5a56

View File

@@ -524,6 +524,70 @@ fn get_cpu_temp() -> Option<f32> {
None
}
fn get_disk_info() -> (f64, f64, f64) {
let mut sys = System::new();
sys.refresh_disks();
sys.refresh_disks_list();
let mut total_size = 0u64;
let mut total_used = 0u64;
let mut count = 0;
for disk in sys.disks() {
// Ignoriere CD-ROMs und kleine Systempartitionen
if disk.total_space() > 100 * 1024 * 1024 {
// > 100MB
total_size += disk.total_space();
total_used += disk.total_space() - disk.available_space();
count += 1;
}
}
// Berechnungen
let size_gb = if count > 0 {
total_size as f64 / (1024.0 * 1024.0 * 1024.0)
} else {
// Fallback: Versuche df unter Linux
#[cfg(target_os = "linux")]
{
if let Ok(output) = Command::new("df")
.arg("-B1")
.arg("--output=size,used")
.output()
{
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines().skip(1) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() == 2 {
if let (Ok(size), Ok(used)) =
(parts[0].parse::<u64>(), parts[1].parse::<u64>())
{
total_size += size;
total_used += used;
count += 1;
}
}
}
total_size as f64 / (1024.0 * 1024.0 * 1024.0)
} else {
0.0
}
}
#[cfg(not(target_os = "linux"))]
{
0.0
}
};
let usage = if total_size > 0 {
(total_used as f64 / total_size as f64) * 100.0
} else {
0.0
};
(size_gb, usage, 0.0) // Disk-Temp bleibt 0.0 ohne spezielle Hardware
}
fn get_network_traffic() -> Option<(u64, u64)> {
#[cfg(target_os = "linux")]
{
@@ -602,3 +666,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
heartbeat_handle.await?;
Ok(())
}
#[tokio::test]
async fn test_metrics() {
println!("CPU Temp: {:?}", get_cpu_temp());
println!("Disk Info: {:?}", get_disk_info());
println!("Network: {:?}", get_network_traffic());
}