moving imports trough die gegend
This commit is contained in:
@@ -19,8 +19,10 @@ nvml-wrapper = "0.10"
|
|||||||
|
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror"] }
|
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||||
systemstat = "0.2.5"
|
systemstat = "0.2.5"
|
||||||
|
com = "0.2"
|
||||||
|
widestring = "0.5"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
glob = "0.3"
|
glob = "0.3"
|
@@ -420,10 +420,10 @@ impl MetricsCollector {
|
|||||||
|
|
||||||
fn get_cpu_temp() -> Option<f32> {
|
fn get_cpu_temp() -> Option<f32> {
|
||||||
println!("Attempting to get CPU temperature...");
|
println!("Attempting to get CPU temperature...");
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
// Versuche mehrere Methoden der Reihe nach
|
println!("");
|
||||||
// 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() {
|
||||||
@@ -472,123 +472,158 @@ fn get_cpu_temp() -> Option<f32> {
|
|||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
use systemstat::{Platform, System as SysStat};
|
// Method 1: Try WMI first (for modern hardware)
|
||||||
let sys = SysStat::new();
|
if let Some(temp) = get_cpu_temp_wmi() {
|
||||||
match sys.cpu_temp() {
|
return Some(temp);
|
||||||
Ok(temp) => return Some(temp),
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Fehler beim Lesen der CPU-Temperatur: {}", e);
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method 2: Fallback to Open Hardware Monitor (HTTP API)
|
||||||
|
if let Some(temp) = get_cpu_temp_ohm() {
|
||||||
|
return Some(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method 3: Fallback to CoreTemp (shared memory)
|
||||||
|
if let Some(temp) = get_cpu_temp_coretemp() {
|
||||||
|
return Some(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
eprintln!("All methods failed to read CPU temperature.");
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||||
{
|
{
|
||||||
// Fallback for unsupported OS
|
|
||||||
println!("CPU temperature retrieval not supported on this OS.");
|
println!("CPU temperature retrieval not supported on this OS.");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#[cfg(target_os = "windows")]
|
||||||
#[cfg(target_os = "linux")]
|
use com::runtime::init_runtime;
|
||||||
{
|
use com::sys::{CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED};
|
||||||
// Versuche mehrere Methoden der Reihe nach
|
use widestring::U16CString;
|
||||||
// 1. sensors-Befehl
|
use winapi::shared::wtypes::VT_I4;
|
||||||
if let Ok(output) = Command::new("sensors").output() {
|
use winapi::um::oaidl::VARIANT;
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
use winapi::um::{combaseapi, wbemcli};
|
||||||
for line in stdout.lines() {
|
|
||||||
if line.contains("Package id") || line.contains("Tdie") || line.contains("CPU Temp")
|
#[cfg(target_os = "windows")]
|
||||||
{
|
|
||||||
if let Some(temp_str) = line
|
fn get_cpu_temp_wmi() -> Option<f32> {
|
||||||
.split('+')
|
init_runtime().ok()?;
|
||||||
.nth(1)
|
|
||||||
.and_then(|s| s.split_whitespace().next())
|
unsafe {
|
||||||
{
|
let mut locator: *mut wbemcli::IWbemLocator = std::ptr::null_mut();
|
||||||
if let Ok(temp) = temp_str.replace("°C", "").parse::<f32>() {
|
let hr = combaseapi::CoCreateInstance(
|
||||||
return Some(temp);
|
&wbemcli::CLSID_WbemLocator,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
CLSCTX_INPROC_SERVER,
|
||||||
|
&wbemcli::IID_IWbemLocator,
|
||||||
|
&mut locator as *mut _ as *mut _,
|
||||||
|
);
|
||||||
|
|
||||||
|
if hr != 0 {
|
||||||
|
eprintln!("Failed to create WbemLocator (HRESULT: {})", hr);
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut services: *mut wbemcli::IWbemServices = std::ptr::null_mut();
|
||||||
|
let namespace = U16CString::from_str("root\\wmi").unwrap();
|
||||||
|
let hr = (*locator).ConnectServer(
|
||||||
|
namespace.as_ptr().cast_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
0,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
&mut services,
|
||||||
|
);
|
||||||
|
|
||||||
|
if hr != 0 {
|
||||||
|
eprintln!("Failed to connect to WMI (HRESULT: {})", hr);
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let query = U16CString::from_str("SELECT * FROM MSAcpi_ThermalZoneTemperature").unwrap();
|
||||||
|
let mut enumerator: *mut wbemcli::IEnumWbemClassObject = std::ptr::null_mut();
|
||||||
|
let hr = (*services).ExecQuery(
|
||||||
|
U16CString::from_str("WQL").unwrap().as_ptr().cast_mut(),
|
||||||
|
query.as_ptr().cast_mut(),
|
||||||
|
wbemcli::WBEM_FLAG_FORWARD_ONLY as i32,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
&mut enumerator,
|
||||||
|
);
|
||||||
|
|
||||||
|
if hr != 0 {
|
||||||
|
eprintln!("Failed to execute WMI query (HRESULT: {})", hr);
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut temp_celsius = None;
|
||||||
|
loop {
|
||||||
|
let mut obj: *mut wbemcli::IWbemClassObject = std::ptr::null_mut();
|
||||||
|
let mut returned = 0;
|
||||||
|
let hr = (*enumerator).Next(
|
||||||
|
wbemcli::WBEM_INFINITE.try_into().unwrap(),
|
||||||
|
1,
|
||||||
|
&mut obj,
|
||||||
|
&mut returned,
|
||||||
|
);
|
||||||
|
|
||||||
|
if hr != 0 || returned == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut variant = std::mem::zeroed::<VARIANT>();
|
||||||
|
let hr = (*obj).Get(
|
||||||
|
U16CString::from_str("CurrentTemperature").unwrap().as_ptr(),
|
||||||
|
0,
|
||||||
|
&mut variant,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if hr == 0 && variant.n1.n2().vt as u32 == VT_I4 {
|
||||||
|
let temp_kelvin = *variant.n1.n2().n3.intVal() as f32 / 10.0;
|
||||||
|
Some(temp_kelvin - 273.15);
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Sysfs (Intel/AMD)
|
temp_celsius
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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::<f32>() {
|
|
||||||
return Some(temp / 1000.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
fn get_cpu_temp() -> Option<f32> {
|
fn get_cpu_temp_ohm() -> Option<f32> {
|
||||||
use winapi::um::pdh::{
|
// Open Hardware Monitor HTTP API fallback
|
||||||
PdhAddCounter, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQuery,
|
let output = Command::new("curl")
|
||||||
PDH_FMT_DOUBLE,
|
.args(&["-s", "http://localhost:8085/data.json"])
|
||||||
};
|
.output()
|
||||||
use winapi::um::pdhmsg::PDH_FMT_COUNTERVALUE;
|
.ok()?;
|
||||||
|
|
||||||
unsafe {
|
let json: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
|
||||||
let mut query_handle = 0 as u64;
|
json["Children"][0]["Children"][0]["Children"]
|
||||||
if PdhOpenQuery(std::ptr::null_mut(), 0, &mut query_handle) != 0 {
|
.as_array()?
|
||||||
return None;
|
.iter()
|
||||||
|
.find(|s| s["Text"].as_str() == Some("CPU Package"))
|
||||||
|
.and_then(|s| s["Value"].as_f64())
|
||||||
|
.map(|t| t as f32)
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut counter_handle = 0 as u64;
|
#[cfg(target_os = "windows")]
|
||||||
if PdhAddCounter(
|
fn get_cpu_temp_coretemp() -> Option<f32> {
|
||||||
query_handle,
|
// CoreTemp shared memory fallback
|
||||||
"\\Processor Information(_Total)\\Temperature",
|
let output = Command::new("CoreTemp.exe").arg("/S").output().ok()?;
|
||||||
0,
|
|
||||||
&mut counter_handle,
|
|
||||||
) != 0
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if PdhCollectQueryData(query_handle) != 0 {
|
String::from_utf8_lossy(&output.stdout)
|
||||||
return None;
|
.lines()
|
||||||
|
.find(|line| line.contains("Core 0"))
|
||||||
|
.and_then(|line| line.split_whitespace().last())
|
||||||
|
.and_then(|s| s.replace("°C", "").parse::<f32>().ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
fn get_disk_info() -> (f64, f64, f64) {
|
||||||
let mut sys = System::new();
|
let mut sys = System::new();
|
||||||
sys.refresh_all();
|
sys.refresh_all();
|
||||||
@@ -614,6 +649,7 @@ fn get_disk_info() -> (f64, f64, f64) {
|
|||||||
total_size as f64 // in Bytes
|
total_size as f64 // in Bytes
|
||||||
} else {
|
} else {
|
||||||
// Fallback: Versuche df unter Linux
|
// Fallback: Versuche df unter Linux
|
||||||
|
println!("Fallback: Using 'df' command to get disk info.");
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
if let Ok(output) = Command::new("df")
|
if let Ok(output) = Command::new("df")
|
||||||
|
Reference in New Issue
Block a user