From bf6f89c9546ab8c5c7f5e2bf0e9213a94b88525b Mon Sep 17 00:00:00 2001 From: donpat1to Date: Sat, 27 Sep 2025 23:48:02 +0200 Subject: [PATCH] added error handling for client version print --- WatcherAgent/src/hardware/disk.rs | 198 +++++++++++++++++------------- WatcherAgent/src/metrics.rs | 4 +- WatcherAgent/src/models.rs | 12 ++ 3 files changed, 125 insertions(+), 89 deletions(-) diff --git a/WatcherAgent/src/hardware/disk.rs b/WatcherAgent/src/hardware/disk.rs index 8bb4e8c..f0a440e 100644 --- a/WatcherAgent/src/hardware/disk.rs +++ b/WatcherAgent/src/hardware/disk.rs @@ -1,69 +1,55 @@ -use std::error::Error; +use crate::models::DiskInfoDetailed; +use std::error::Error; use anyhow::Result; use sysinfo::DiskUsage; -use sysinfo::{Component, Components, Disk, Disks, System}; +use sysinfo::{Component, Components, Disk, Disks}; +use serde::Serialize; -#[derive(Debug)] + +#[derive(Serialize, Debug)] pub struct DiskInfo { - pub total: Option, - pub used: Option, - pub free: Option, + pub total_size: Option, + pub total_used: Option, + pub total_available: Option, + pub total_usage: Option, + pub detailed_info: Vec, } -pub async fn get_disk_info() -> Result { +pub async fn get_disk_info() -> Result> { let disks = Disks::new_with_refreshed_list(); - let _disk_types = [ - sysinfo::DiskKind::HDD, - sysinfo::DiskKind::SSD, - sysinfo::DiskKind::Unknown(0), - ]; - - let (_, _, _, _) = get_disk_utitlization().unwrap(); - - let mut total = 0; - let mut used = 0; - + let mut detailed_info = Vec::new(); + + // Collect detailed disk information for disk in disks.list() { - if disk.total_space() > 100 * 1024 * 1024 { - // > 100MB - total += disk.total_space(); - used += disk.total_space() - disk.available_space(); - } - } - - Ok(DiskInfo { - total: Some(total as f64), - used: Some(used as f64), - free: Some((total - used) as f64), - }) -} - -pub fn get_disk_utitlization() -> Result<(f64, f64, f64, f64), Box> { - let mut sys = System::new(); - sys.refresh_all(); - let mut count = 0; - - let mut total_size = 0u64; - let mut total_used = 0u64; - let mut total_available = 0u64; - - let disks = Disks::new_with_refreshed_list(); - for disk in disks.list() { - // Only print disks with known kind if disk.kind() == sysinfo::DiskKind::Unknown(0) { continue; } + + let disk_used = disk.total_space() - disk.available_space(); + detailed_info.push(DiskInfoDetailed { + disk_name: disk.name().to_string_lossy().into_owned(), + disk_kind: format!("{:?}", disk.kind()), + disk_total_space: disk.total_space() as f64, + disk_available_space: disk.available_space() as f64, + disk_used_space: disk_used as f64, + disk_mount_point: disk.mount_point().to_string_lossy().into_owned(), + component_disk_label: String::new(), + component_disk_temperature: 0.0, + }); + println!( "Disk_Name: {:?}:\n---- Disk_Kind: {},\n---- Total: {},\n---- Available: {},\n---- Used: {}, \n---- Mount_Point: {:?}", disk.name(), disk.kind(), disk.total_space(), disk.available_space(), - disk.total_space() - disk.available_space(), + disk_used, disk.mount_point() ); } + + // Get component temperatures let components = Components::new_with_refreshed_list(); for component in &components { if let Some(temperature) = component.temperature() { @@ -72,59 +58,97 @@ pub fn get_disk_utitlization() -> Result<(f64, f64, f64, f64), Box> { component.label(), temperature ); - } - } - - // Berechnungen - let total_size = if count > 0 { - total_size as f64 // in Bytes - } else { - // Fallback: Versuche df unter Linux - println!("Fallback: Using 'df' command to get disk info."); - #[cfg(target_os = "linux")] - { - use std::process::Command; - 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::(), parts[1].parse::()) - { - total_size += size; - total_used += used; - count += 1; - } - } + + // Update detailed info with temperature data if it matches a disk component + for disk_info in &mut detailed_info { + if component.label().contains(&disk_info.disk_name) { + disk_info.component_disk_label = component.label().to_string(); + disk_info.component_disk_temperature = temperature; } - total_size as f64 // in Bytes - } else { - 0.0 } } - #[cfg(not(target_os = "linux"))] - { - 0.0 + } + + // Calculate totals (only disks > 100MB) + let (total_size, total_used, total_available) = calculate_disk_totals(&disks); + + let (total_size, total_used, total_available, total_usage) = if total_size > 0.0 { + (total_size, total_used, total_available, (total_used / total_size) * 100.0) + } else { + match get_disk_info_fallback() { + Ok(fallback_data) => fallback_data, + Err(_) => (0.0, 0.0, 0.0, 0.0), // Default values if fallback also fails } }; + + Ok(DiskInfo { + total_size: if total_size > 0.0 { Some(total_size) } else { None }, + total_used: if total_used > 0.0 { Some(total_used) } else { None }, + total_available: if total_available > 0.0 { Some(total_available) } else { None }, + total_usage: if total_usage > 0.0 { Some(total_usage) } else { None }, + detailed_info, + }) +} - let usage = if total_size > 0.0 { +fn calculate_disk_totals(disks: &Disks) -> (f64, f64, f64) { + let mut total_size = 0u64; + let mut total_used = 0u64; + let mut total_available = 0u64; + + for disk in disks.list() { + if disk.total_space() > 100 * 1024 * 1024 { // > 100MB + total_size += disk.total_space(); + total_available += disk.available_space(); + total_used += disk.total_space() - disk.available_space(); + } + } + + (total_size as f64, total_used as f64, total_available as f64) +} + +#[cfg(target_os = "linux")] +fn get_disk_info_fallback() -> Result<(f64, f64, f64, f64), Box> { + use std::process::Command; + + let output = Command::new("df") + .arg("-B1") + .arg("--output=size,used,avail") + .output()?; + + let stdout = String::from_utf8_lossy(&output.stdout); + let mut total_size = 0u64; + let mut total_used = 0u64; + let mut total_available = 0u64; + let mut count = 0; + + for line in stdout.lines().skip(1) { + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() >= 3 { + if let (Ok(size), Ok(used), Ok(avail)) = ( + parts[0].parse::(), + parts[1].parse::(), + parts[2].parse::(), + ) { + total_size += size; + total_used += used; + total_available += avail; + count += 1; + } + } + } + + let usage = if total_size > 0 { (total_used as f64 / total_size as f64) * 100.0 } else { 0.0 }; + + Ok((total_size as f64, total_used as f64, total_available as f64, usage)) +} - Ok(( - total_size, - total_used as f64, - total_available as f64, - usage as f64, - )) // Disk-Temp bleibt 0.0 ohne spezielle Hardware +#[cfg(not(target_os = "linux"))] +fn get_disk_info_fallback() -> Result<(f64, f64, f64, f64), Box> { + Ok((0.0, 0.0, 0.0, 0.0)) } pub fn _get_disk_temp_for_component(component: &Component) -> Option { diff --git a/WatcherAgent/src/metrics.rs b/WatcherAgent/src/metrics.rs index 8b346f8..ba3b896 100644 --- a/WatcherAgent/src/metrics.rs +++ b/WatcherAgent/src/metrics.rs @@ -62,8 +62,8 @@ impl Collector { gpu_vram_usage: hardware.gpu.vram_used.unwrap_or_default(), ram_load: hardware.memory.used.unwrap_or_default(), ram_size: hardware.memory.total.unwrap_or_default(), - disk_size: hardware.disk.total.unwrap_or_default(), - disk_usage: hardware.disk.used.unwrap_or_default(), + disk_size: hardware.disk.total_size.unwrap_or_default(), + disk_usage: hardware.disk.total_used.unwrap_or_default(), disk_temp: 0.0, // not supported net_rx: hardware.network.rx_rate.unwrap_or_default(), net_tx: hardware.network.tx_rate.unwrap_or_default(), diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index 2242069..3cad177 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -51,6 +51,18 @@ pub struct MetricDto { pub net_tx: f64, } +#[derive(Serialize, Debug)] +pub struct DiskInfoDetailed { + pub disk_name: String, + pub disk_kind: String, + pub disk_total_space: f64, + pub disk_available_space: f64, + pub disk_used_space: f64, + pub disk_mount_point: String, + pub component_disk_label: String, + pub component_disk_temperature: f32, +} + #[derive(Deserialize)] pub struct IdResponse { pub id: i32,