diff --git a/WatcherAgent/Cargo.toml b/WatcherAgent/Cargo.toml index c69a106..183f1b9 100644 --- a/WatcherAgent/Cargo.toml +++ b/WatcherAgent/Cargo.toml @@ -12,13 +12,13 @@ serde_json = "1.0" tokio = { version = "1.37", features = ["full"] } local-ip-address = "0.5" reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "cookies", "rustls-tls"] } -sysinfo = "0.29" +sysinfo = "0.35" metrics = "0.24.2" chrono = "0.4" nvml-wrapper = "0.10" [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = ["winuser", "pdh", "pdhmsg"] } +winapi = { version = "0.3.9", features = ["winuser", "pdh"] } [target.'cfg(unix)'.dependencies] glob = "0.3" \ No newline at end of file diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index 5ac2111..9526260 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -97,17 +97,18 @@ impl NetworkState { impl HardwareInfo { async fn collect() -> Result> { - let mut sys = System::new(); - sys.refresh_cpu(); + let mut sys = System::new_all(); + sys.refresh_cpu_all(); sys.refresh_memory(); let cpus = sys.cpus(); - let cpu_type = cpus - .get(0) - .map(|c| c.brand().to_string()) - .unwrap_or("Unknown CPU".to_string()); + let cpu_type = if !cpus.is_empty() { + cpus[0].brand().to_string() + } else { + "Unknown CPU".to_string() + }; let cpu_cores = cpus.len() as i32; - let ram_gb = (sys.total_memory() as f64); // in Bytes + let ram_bytes = sys.total_memory() as f64; let gpu_type = Self::detect_gpu_name(); let ip_address = local_ip_address::local_ip()?.to_string(); @@ -115,7 +116,7 @@ impl HardwareInfo { cpu_type, cpu_cores, gpu_type, - ram_size: ram_gb, + ram_size: ram_bytes, ip_address, }) } @@ -330,23 +331,20 @@ impl MetricsCollector { fn collect_metrics(&mut self) -> MetricDto { self.sys.refresh_all(); - // CPU - let cpu_load = self.sys.global_cpu_info().cpu_usage() as f64; + // CPU - updated for sysinfo 0.35 + let cpu_load = self.sys.global_cpu_usage() as f64; let cpu_temp = get_cpu_temp().unwrap_or(0.0) as f64; - // RAM - 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; // in B + // RAM - updated for sysinfo 0.35 + let total_memory = self.sys.total_memory() as f64; + let used_memory = self.sys.used_memory() as f64; + let ram_load = (used_memory / total_memory) * 100.0; + let ram_size = total_memory; - // Disk - let disk = self.sys.disks().first(); - // In collect_metrics(): + // Disk - updated for sysinfo 0.35 let (disk_size, disk_usage, disk_temp) = { let mut total_size = 0u64; let mut total_used = 0u64; - let mut temp = 0.0; let mut count = 0; for disk in self.sys.disks() { @@ -390,7 +388,11 @@ impl MetricsCollector { 0.0 }; - let avg_temp = if count > 0 { temp / count as f32 } else { 0.0 }; + let avg_temp = if count > 0 { + disk_temp / count as f32 + } else { + 0.0 + }; (size_b, usage, avg_temp as f64) }; @@ -465,103 +467,112 @@ impl MetricsCollector { } fn get_cpu_temp() -> Option { - #[cfg(target_os = "linux")] - { - // Versuche mehrere Methoden der Reihe nach - // 1. sensors-Befehl - if let Ok(output) = Command::new("sensors").output() { - let stdout = String::from_utf8_lossy(&output.stdout); - for line in stdout.lines() { - if line.contains("Package id") || line.contains("Tdie") || line.contains("CPU Temp") - { - if let Some(temp_str) = line - .split('+') - .nth(1) - .and_then(|s| s.split_whitespace().next()) - { - if let Ok(temp) = temp_str.replace("°C", "").parse::() { - return Some(temp); - } - } - } - } - } + let sys = System::new_all(); - // 2. Sysfs (Intel/AMD) - if let Ok(content) = fs::read_to_string("/sys/class/thermal/thermal_zone0/temp") { - if let Ok(temp) = content.trim().parse::() { - return Some(temp / 1000.0); - } - } - - // 3. Alternative Sysfs-Pfade - let paths = [ - "/sys/class/hwmon/hwmon*/temp1_input", - "/sys/class/hwmon/hwmon*/device/temp1_input", - ]; - - for path_pattern in &paths { - if let Ok(paths) = glob::glob(path_pattern) { - for path in paths.flatten() { - if let Ok(content) = fs::read_to_string(&path) { - if let Ok(temp) = content.trim().parse::() { - return Some(temp / 1000.0); - } - } - } - } - } - } - - #[cfg(target_os = "windows")] - fn get_cpu_temp() -> Option { - use winapi::um::pdh::{ - PdhAddCounter, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQuery, - PDH_FMT_DOUBLE, - }; - use winapi::um::pdhmsg::PDH_FMT_COUNTERVALUE; - - unsafe { - let mut query_handle = 0 as u64; - if PdhOpenQuery(std::ptr::null_mut(), 0, &mut query_handle) != 0 { - return None; - } - - let mut counter_handle = 0 as u64; - if PdhAddCounter( - query_handle, - "\\Processor Information(_Total)\\Temperature", - 0, - &mut counter_handle, - ) != 0 - { - return None; - } - - if PdhCollectQueryData(query_handle) != 0 { - return None; - } - - let mut counter_value = PDH_FMT_COUNTERVALUE { - CStatus: 0, - union: PDH_FMT_DOUBLE { doubleValue: 0.0 }, - }; - if PdhGetFormattedCounterValue( - counter_handle, - PDH_FMT_DOUBLE, - std::ptr::null_mut(), - &mut counter_value, - ) != 0 - { - return None; - } - - Some(counter_value.union.doubleValue as f32) - } + // Try to get CPU temperature from sysinfo first + if let Some(temp) = sys.cpus().first().and_then(|cpu| cpu.get_temperature()) { + return Some(temp as f32); } None } +/* +#[cfg(target_os = "linux")] +{ + // Versuche mehrere Methoden der Reihe nach + // 1. sensors-Befehl + if let Ok(output) = Command::new("sensors").output() { + let stdout = String::from_utf8_lossy(&output.stdout); + for line in stdout.lines() { + if line.contains("Package id") || line.contains("Tdie") || line.contains("CPU Temp") + { + if let Some(temp_str) = line + .split('+') + .nth(1) + .and_then(|s| s.split_whitespace().next()) + { + if let Ok(temp) = temp_str.replace("°C", "").parse::() { + return Some(temp); + } + } + } + } + } + + // 2. Sysfs (Intel/AMD) + if let Ok(content) = fs::read_to_string("/sys/class/thermal/thermal_zone0/temp") { + if let Ok(temp) = content.trim().parse::() { + return Some(temp / 1000.0); + } + } + + // 3. Alternative Sysfs-Pfade + let paths = [ + "/sys/class/hwmon/hwmontemp1_input", + "/sys/class/hwmon/hwmondevice/temp1_input", + ]; + + for path_pattern in &paths { + if let Ok(paths) = glob::glob(path_pattern) { + for path in paths.flatten() { + if let Ok(content) = fs::read_to_string(&path) { + if let Ok(temp) = content.trim().parse::() { + return Some(temp / 1000.0); + } + } + } + } + } +} + +#[cfg(target_os = "windows")] +fn get_cpu_temp() -> Option { + use winapi::um::pdh::{ + PdhAddCounter, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQuery, + PDH_FMT_DOUBLE, + }; + use winapi::um::pdhmsg::PDH_FMT_COUNTERVALUE; + + unsafe { + let mut query_handle = 0 as u64; + if PdhOpenQuery(std::ptr::null_mut(), 0, &mut query_handle) != 0 { + return None; + } + + let mut counter_handle = 0 as u64; + if PdhAddCounter( + query_handle, + "\\Processor Information(_Total)\\Temperature", + 0, + &mut counter_handle, + ) != 0 + { + return None; + } + + if PdhCollectQueryData(query_handle) != 0 { + return None; + } + + let mut counter_value = PDH_FMT_COUNTERVALUE { + CStatus: 0, + union: PDH_FMT_DOUBLE { doubleValue: 0.0 }, + }; + if PdhGetFormattedCounterValue( + counter_handle, + PDH_FMT_DOUBLE, + std::ptr::null_mut(), + &mut counter_value, + ) != 0 + { + return None; + } + + Some(counter_value.union.doubleValue as f32) + } +} + +None*/ fn get_disk_info() -> (f64, f64, f64) { let mut sys = System::new();