added alternatives for windows cputemp detection
This commit is contained in:
23
WatcherAgent/src/library.rs
Normal file
23
WatcherAgent/src/library.rs
Normal 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()`
|
||||||
|
*/
|
@@ -437,35 +437,77 @@ impl MetricsCollector {
|
|||||||
fn get_cpu_temp() -> Option<f32> {
|
fn get_cpu_temp() -> Option<f32> {
|
||||||
#[cfg(target_os = "linux")]
|
#[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() {
|
if let Ok(output) = Command::new("sensors").output() {
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
for line in stdout.lines() {
|
for line in stdout.lines() {
|
||||||
if line.to_lowercase().contains("package id")
|
if line.contains("Package id") || line.contains("Tdie") || line.contains("CPU Temp")
|
||||||
|| line.to_lowercase().contains("cpu temp")
|
|
||||||
{
|
{
|
||||||
if let Some(temp_str) = line.split_whitespace().find(|s| s.contains("°C")) {
|
if let Some(temp_str) = line
|
||||||
let number: String = temp_str
|
.split('+')
|
||||||
.chars()
|
.nth(1)
|
||||||
.filter(|c| c.is_ascii_digit() || *c == '.')
|
.and_then(|s| s.split_whitespace().next())
|
||||||
.collect();
|
{
|
||||||
return number.parse::<f32>().ok();
|
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(content) = fs::read_to_string("/sys/class/thermal/thermal_zone0/temp") {
|
||||||
if let Ok(temp) = content.trim().parse::<f32>() {
|
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")]
|
#[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")
|
let output = Command::new("wmic")
|
||||||
.args(&["cpu", "get", "Temperature", "/Value"])
|
.args(&["cpu", "get", "Temperature", "/Value"])
|
||||||
.output()
|
.output()
|
||||||
@@ -474,9 +516,7 @@ fn get_cpu_temp() -> Option<f32> {
|
|||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
for line in stdout.lines() {
|
for line in stdout.lines() {
|
||||||
if line.starts_with("Temperature=") {
|
if line.starts_with("Temperature=") {
|
||||||
if let Ok(temp) = line.replace("Temperature=", "").trim().parse::<f32>() {
|
return line.replace("Temperature=", "").trim().parse::<f32>().ok();
|
||||||
return Some(temp); // Returns in Celsius
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user