From 8a4df64d0c9a2248a9406357d8f6f41e0722d93d Mon Sep 17 00:00:00 2001 From: donpat1to Date: Thu, 31 Jul 2025 11:28:58 +0200 Subject: [PATCH] added alternatives for windows cputemp detection --- WatcherAgent/src/library.rs | 23 ++++++++++++ WatcherAgent/src/main.rs | 70 +++++++++++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 WatcherAgent/src/library.rs diff --git a/WatcherAgent/src/library.rs b/WatcherAgent/src/library.rs new file mode 100644 index 0000000..5145f0a --- /dev/null +++ b/WatcherAgent/src/library.rs @@ -0,0 +1,23 @@ +/* +$ rustc --crate-type=lib rary.rs +$ ls lib* +library.rlib + +// extern crate rary; // May be required for Rust 2015 edition or earlier + +fn main() { + rary::public_function(); + + // Error! `private_function` is private + //rary::private_function(); + + rary::indirect_access(); +} + +# Where library.rlib is the path to the compiled library, assumed that it's +# in the same directory here: +$ rustc executable.rs --extern rary=library.rlib && ./executable +called rary's `public_function()` +called rary's `indirect_access()`, that +> called rary's `private_function()` +*/ diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index 89af784..7bbf170 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -437,35 +437,77 @@ impl MetricsCollector { fn get_cpu_temp() -> Option { #[cfg(target_os = "linux")] { - // Linux: sensors command or sysfs + // 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.to_lowercase().contains("package id") - || line.to_lowercase().contains("cpu temp") + if line.contains("Package id") || line.contains("Tdie") || line.contains("CPU Temp") { - if let Some(temp_str) = line.split_whitespace().find(|s| s.contains("°C")) { - let number: String = temp_str - .chars() - .filter(|c| c.is_ascii_digit() || *c == '.') - .collect(); - return number.parse::().ok(); + 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); + } } } } } - // Fallback to sysfs (common path for Intel/AMD) + // 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); // Convert millidegrees to degrees + 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")] { - // Windows: WMI query + // Windows: OpenHardwareMonitor über WMI abfragen + let output = Command::new("wmic") + .args(&[ + "/namespace:\\root\\OpenHardwareMonitor", + "path", + "Sensor", + "get", + "Value,Name", + "/format:list", + ]) + .output() + .ok()?; + + let stdout = String::from_utf8_lossy(&output.stdout); + for line in stdout.lines() { + if line.contains("Name=CPU Package") && line.contains("Value=") { + if let Some(value) = line.split("Value=").nth(1) { + return value.trim().parse::().ok(); + } + } + } + + // Fallback: Standard WMI let output = Command::new("wmic") .args(&["cpu", "get", "Temperature", "/Value"]) .output() @@ -474,9 +516,7 @@ fn get_cpu_temp() -> Option { let stdout = String::from_utf8_lossy(&output.stdout); for line in stdout.lines() { if line.starts_with("Temperature=") { - if let Ok(temp) = line.replace("Temperature=", "").trim().parse::() { - return Some(temp); // Returns in Celsius - } + return line.replace("Temperature=", "").trim().parse::().ok(); } } }