diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index 499479a..21b31b6 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -4,7 +4,7 @@ use reqwest::{Client, StatusCode}; use serde::{Deserialize, Serialize}; use std::{error::Error, fs, process::Command, time::Duration}; use sysinfo::{CpuExt, DiskExt, System, SystemExt}; -use tokio::time::{interval, sleep}; +use tokio::time::{interval, sleep, Instant}; // Data structures matching the C# DTOs #[derive(Serialize, Debug)] @@ -79,6 +79,22 @@ struct HardwareInfo { ip_address: String, } +struct NetworkState { + prev_rx: u64, + prev_tx: u64, + last_update: Instant, +} + +impl NetworkState { + fn new() -> Self { + Self { + prev_rx: 0, + prev_tx: 0, + last_update: Instant::now(), + } + } +} + impl HardwareInfo { async fn collect() -> Result> { let mut sys = System::new(); @@ -276,6 +292,7 @@ struct MetricsCollector { nvml: Option, server_id: i32, ip_address: String, + network_state: NetworkState, } impl MetricsCollector { @@ -285,6 +302,7 @@ impl MetricsCollector { nvml: Nvml::init().ok(), server_id, ip_address, + network_state: NetworkState::new(), } } @@ -320,7 +338,7 @@ impl MetricsCollector { let total_memory = self.sys.total_memory(); let used_memory = self.sys.used_memory(); let ram_load = (used_memory as f64 / total_memory as f64) * 100.0; - let ram_size = (total_memory as f64) / 1024.0 / 1024.0; + let ram_size = total_memory as f64; // in B // Disk let disk = self.sys.disks().first(); @@ -364,11 +382,7 @@ impl MetricsCollector { } } - let size_gb = if count > 0 { - (total_size as f64) / 1024.0 / 1024.0 / 1024.0 - } else { - 0.0 - }; + let size_b = if count > 0 { total_size as f64 } else { 0.0 }; let usage = if total_size > 0 { (total_used as f64 / total_size as f64) * 100.0 @@ -378,7 +392,7 @@ impl MetricsCollector { let avg_temp = if count > 0 { temp / count as f32 } else { 0.0 }; - (size_gb, usage, avg_temp) + (size_b, usage, avg_temp as f64) }; // GPU (NVIDIA) let (gpu_temp, gpu_load, vram_used, vram_total) = if let Some(nvml) = &self.nvml { @@ -391,13 +405,8 @@ impl MetricsCollector { .map(|u| u.gpu as f64) .unwrap_or(0.0); let mem = device.memory_info().ok(); - let used = mem - .clone() - .map(|m| (m.used as f64) / 1024.0 / 1024.0 / 1024.0) - .unwrap_or(0.0); // GB - let total = mem - .map(|m| (m.total as f64) / 1024.0 / 1024.0 / 1024.0) - .unwrap_or(0.0); // GB + let used = mem.clone().map(|m| (m.used as f64)).unwrap_or(0.0); // B + let total = mem.map(|m| (m.total as f64)).unwrap_or(0.0); // B (temp, load, used, total) } else { (0.0, 0.0, 0.0, 0.0) @@ -406,10 +415,30 @@ impl MetricsCollector { (0.0, 0.0, 0.0, 0.0) }; - // Network (convert bytes to bits) - let (net_in, net_out) = get_network_traffic().unwrap_or((0, 0)); - let net_in_bits = (net_in as f64) * 8.0; - let net_out_bits = (net_out as f64) * 8.0; + // Network metrics + let (current_rx, current_tx) = get_network_traffic().unwrap_or((0, 0)); + let elapsed_secs = self.network_state.last_update.elapsed().as_secs_f64(); + self.network_state.last_update = Instant::now(); + + // Calculate the difference since the last call + let net_in = if self.network_state.prev_rx > 0 && current_rx >= self.network_state.prev_rx { + ((current_rx - self.network_state.prev_rx) as f64 * 8.0) / elapsed_secs + // bits per second + } else { + 0.0 + }; + + let net_out = if self.network_state.prev_tx > 0 && current_tx >= self.network_state.prev_tx + { + ((current_tx - self.network_state.prev_tx) as f64 * 8.0) / elapsed_secs + // bits per second + } else { + 0.0 + }; + + // Store the current values for the next call + self.network_state.prev_rx = current_rx; + self.network_state.prev_tx = current_tx; MetricDto { server_id: self.server_id, @@ -428,9 +457,9 @@ impl MetricsCollector { ram_size, disk_size, disk_usage: disk_usage, - disk_temp: 0.0, // not supported - net_in: net_in_bits, - net_out: net_out_bits, + disk_temp: disk_temp, // not supported + net_in, + net_out, } } } @@ -591,7 +620,6 @@ fn get_disk_info() -> (f64, f64, f64) { #[cfg(target_os = "windows")] fn get_network_traffic() -> Option<(u64, u64)> { - use std::mem::size_of; use std::ptr::null_mut; use winapi::shared::ifmib::{MIB_IFROW, MIB_IFTABLE}; use winapi::um::iphlpapi::GetIfTable;