diff --git a/WatcherAgent/src/api.rs b/WatcherAgent/src/api.rs index baf7fe6..1c8df20 100644 --- a/WatcherAgent/src/api.rs +++ b/WatcherAgent/src/api.rs @@ -1,7 +1,7 @@ use std::time::Duration; use crate::hardware::HardwareInfo; -use crate::models::{HardwareDto, HeartbeatDto, IdResponse, MetricDto, RegistrationDto}; +use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto}; use anyhow::Result; use reqwest::{Client, StatusCode}; use std::error::Error; diff --git a/WatcherAgent/src/hardware/memory.rs b/WatcherAgent/src/hardware/memory.rs index 65be545..6a5222e 100644 --- a/WatcherAgent/src/hardware/memory.rs +++ b/WatcherAgent/src/hardware/memory.rs @@ -19,7 +19,7 @@ pub async fn get_memory_info() -> Result { }) } -pub fn get_memory_usage(sys: &mut System) -> f64 { +pub fn _get_memory_usage(sys: &mut System) -> f64 { sys.refresh_memory(); (sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0 } diff --git a/WatcherAgent/src/hardware/mod.rs b/WatcherAgent/src/hardware/mod.rs index 72fe525..f629912 100644 --- a/WatcherAgent/src/hardware/mod.rs +++ b/WatcherAgent/src/hardware/mod.rs @@ -5,7 +5,7 @@ mod cpu; mod disk; mod gpu; mod memory; -mod network; +pub(crate) mod network; pub use cpu::get_cpu_info; pub use disk::get_disk_info; @@ -20,16 +20,19 @@ pub struct HardwareInfo { pub memory: memory::MemoryInfo, pub disk: disk::DiskInfo, pub network: network::NetworkInfo, + network_monitor: network::NetworkMonitor, } impl HardwareInfo { pub async fn collect() -> anyhow::Result> { + 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: get_network_info().await?, + network: get_network_info(&mut network_monitor).await?, + network_monitor, }) } } diff --git a/WatcherAgent/src/hardware/network.rs b/WatcherAgent/src/hardware/network.rs index 2fb3066..c7c1e6e 100644 --- a/WatcherAgent/src/hardware/network.rs +++ b/WatcherAgent/src/hardware/network.rs @@ -5,10 +5,11 @@ use std::time::Instant; #[derive(Debug)] pub struct NetworkInfo { pub interfaces: Option>, - pub rx_bytes: Option, - pub tx_bytes: Option, + pub rx_rate: Option, + pub tx_rate: Option, } +#[derive(Debug)] pub struct NetworkMonitor { prev_rx: u64, prev_tx: u64, @@ -24,7 +25,7 @@ impl NetworkMonitor { } } - pub fn get_usage(&mut self) -> Result<(f64, f64), Box> { + pub fn update_usage(&mut self) -> Result<(f64, f64), Box> { let (current_rx, current_tx) = get_network_bytes()?; let elapsed = self.last_update.elapsed().as_secs_f64(); self.last_update = Instant::now(); @@ -48,12 +49,12 @@ impl NetworkMonitor { } } -pub async fn get_network_info() -> Result> { - let (rx, tx) = get_network_bytes()?; +pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result> { + let (rx_rate, tx_rate) = monitor.update_usage()?; Ok(NetworkInfo { interfaces: Some(get_network_interfaces()), - rx_bytes: Some(rx as f64), - tx_bytes: Some(tx as f64), + rx_rate: Some(rx_rate as f64), + tx_rate: Some(tx_rate as f64), }) } diff --git a/WatcherAgent/src/metrics.rs b/WatcherAgent/src/metrics.rs index b5ccbb1..a19502c 100644 --- a/WatcherAgent/src/metrics.rs +++ b/WatcherAgent/src/metrics.rs @@ -2,10 +2,12 @@ use std::error::Error; use std::time::Duration; use crate::api; +use crate::hardware::network::NetworkMonitor; use crate::hardware::HardwareInfo; use crate::models::MetricDto; pub struct Collector { + network_monitor: NetworkMonitor, server_id: i32, ip_address: String, } @@ -13,6 +15,7 @@ pub struct Collector { impl Collector { pub fn new(server_id: i32, ip_address: String) -> Self { Self { + network_monitor: NetworkMonitor::new(), server_id, ip_address, } @@ -28,6 +31,8 @@ impl Collector { pub async fn collect(&mut self) -> Result> { let hardware = HardwareInfo::collect().await?; + // Collect network usage + let (_, _) = self.network_monitor.update_usage().unwrap_or((0.0, 0.0)); Ok(MetricDto { server_id: self.server_id, @@ -43,8 +48,8 @@ impl Collector { disk_size: hardware.disk.total.unwrap_or_default(), disk_usage: hardware.disk.used.unwrap_or_default(), disk_temp: 0.0, // not supported - net_rx: hardware.network.rx_bytes.unwrap_or_default(), - net_tx: hardware.network.tx_bytes.unwrap_or_default(), + 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 56880ca..32ab76c 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -1,5 +1,4 @@ use serde::{Deserialize, Serialize}; -use tokio::time::Instant; // Data structures matching the C# DTOs #[derive(Serialize, Debug)]