Files
watcheragent/WatcherAgent/src/hardware/mod.rs
2025-10-01 12:11:34 +02:00

71 lines
2.3 KiB
Rust

//use anyhow::Result;
use std::error::Error;
pub mod cpu;
pub mod disk;
pub mod gpu;
pub mod memory;
pub mod network;
pub use cpu::get_cpu_info;
pub use disk::get_disk_info;
pub use gpu::get_gpu_info;
pub use memory::get_memory_info;
pub use network::get_network_info;
pub use network::NetworkMonitor;
/// # Hardware Module
///
/// This module aggregates all hardware subsystems for WatcherAgent, providing unified collection and access to CPU, GPU, memory, disk, and network statistics.
///
/// ## Responsibilities
/// - **Subsystem Aggregation:** Combines all hardware modules into a single struct for easy access.
/// - **Unified Collection:** Provides a single async method to collect all hardware metrics at once.
///
/// Aggregated hardware statistics for the host system.
///
/// # Fields
/// - `cpu`: CPU statistics (see [`CpuInfo`])
/// - `gpu`: GPU statistics (see [`GpuInfo`])
/// - `memory`: Memory statistics (see [`MemoryInfo`])
/// - `disk`: Disk statistics (see [`DiskInfo`])
/// - `network`: Network statistics (see [`NetworkInfo`])
/// - `network_monitor`: Rolling monitor for network bandwidth
#[derive(Debug)]
pub struct HardwareInfo {
pub cpu: cpu::CpuInfo,
pub gpu: gpu::GpuInfo,
pub memory: memory::MemoryInfo,
pub disk: disk::DiskInfo,
pub network: network::NetworkInfo,
pub network_monitor: network::NetworkMonitor,
}
impl HardwareInfo {
/// Collects all hardware statistics asynchronously.
///
/// # Returns
/// * `Result<HardwareInfo, Box<dyn Error + Send + Sync>>` - Aggregated hardware statistics or error if any subsystem fails.
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
let mut network_monitor = network::NetworkMonitor::new();
Ok(Self {
cpu: get_cpu_info().await?,
gpu: get_gpu_info().await?,
memory: get_memory_info().await?,
disk: get_disk_info().await?,
network: match get_network_info(&mut network_monitor).await {
Ok(info) => info,
Err(e) => {
eprintln!("Error collecting network info: {}", e);
network::NetworkInfo {
interfaces: None,
rx_rate: None,
tx_rate: None,
}
}
},
network_monitor,
})
}
}