added error handling for client version print

This commit is contained in:
2025-09-27 23:48:02 +02:00
parent dbe87fedb6
commit bf6f89c954
3 changed files with 125 additions and 89 deletions

View File

@@ -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<f64>,
pub used: Option<f64>,
pub free: Option<f64>,
pub total_size: Option<f64>,
pub total_used: Option<f64>,
pub total_available: Option<f64>,
pub total_usage: Option<f64>,
pub detailed_info: Vec<DiskInfoDetailed>,
}
pub async fn get_disk_info() -> Result<DiskInfo> {
pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>> {
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<dyn Error>> {
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<dyn Error>> {
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::<u64>(), parts[1].parse::<u64>())
{
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<dyn Error + Send + Sync>> {
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::<u64>(),
parts[1].parse::<u64>(),
parts[2].parse::<u64>(),
) {
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<dyn Error + Send + Sync>> {
Ok((0.0, 0.0, 0.0, 0.0))
}
pub fn _get_disk_temp_for_component(component: &Component) -> Option<f64> {

View File

@@ -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(),

View File

@@ -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,