added alternatives for windows cputemp detection

This commit is contained in:
2025-07-31 11:28:58 +02:00
parent 36f6fb3566
commit 8a4df64d0c
2 changed files with 78 additions and 15 deletions

View File

@@ -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()`
*/

View File

@@ -437,35 +437,77 @@ impl MetricsCollector {
fn get_cpu_temp() -> Option<f32> {
#[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::<f32>().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::<f32>() {
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::<f32>() {
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::<f32>() {
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::<f32>().ok();
}
}
}
// Fallback: Standard WMI
let output = Command::new("wmic")
.args(&["cpu", "get", "Temperature", "/Value"])
.output()
@@ -474,9 +516,7 @@ fn get_cpu_temp() -> Option<f32> {
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::<f32>() {
return Some(temp); // Returns in Celsius
}
return line.replace("Temperature=", "").trim().parse::<f32>().ok();
}
}
}