Files
watcheragent/WatcherAgent/src/hardware/disk.rs
2025-08-10 13:10:26 +02:00

154 lines
4.4 KiB
Rust

use std::error::Error;
use anyhow::Result;
use sysinfo::DiskUsage;
use sysinfo::{Component, Components, Disk, Disks, System};
#[derive(Debug)]
pub struct DiskInfo {
pub total: Option<f64>,
pub used: Option<f64>,
pub free: Option<f64>,
}
pub async fn get_disk_info() -> Result<DiskInfo> {
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;
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() {
// Ignoriere kleine Systempartitionen
println!(
"Disk_Name: {:?}, Disk_Kind: {}, Total: {}, Available: {}",
disk.name(),
disk.kind(),
disk.total_space(),
disk.available_space(),
);
println!("[{:?}] {:?}", disk.name(), disk.mount_point());
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();
count += 1;
}
}
let components = Components::new_with_refreshed_list();
for component in &components {
if let Some(temperature) = component.temperature() {
println!(
"Component_Label: {}, Temperature: {}°C",
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;
}
}
}
total_size as f64 // in Bytes
} else {
0.0
}
}
#[cfg(not(target_os = "linux"))]
{
0.0
}
};
let usage = if total_size > 0.0 {
(total_used as f64 / total_size as f64) * 100.0
} else {
0.0
};
Ok((
total_size,
total_used as f64,
total_available as f64,
usage as f64,
)) // Disk-Temp bleibt 0.0 ohne spezielle Hardware
}
pub fn _get_disk_temp_for_component(component: &Component) -> Option<f64> {
component.temperature().map(|temp| temp as f64)
}
pub fn _get_disk_load_for_disk(disk: &Disk) -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
let usage: DiskUsage = disk.usage();
// Assuming DiskUsage has these methods:
let total_written_bytes = usage.total_written_bytes as f64;
let written_bytes = usage.written_bytes as f64;
let total_read_bytes = usage.total_read_bytes as f64;
let read_bytes = usage.read_bytes as f64;
Ok((
total_written_bytes,
written_bytes,
total_read_bytes,
read_bytes,
))
}