fixed test with winapi

This commit is contained in:
2025-08-01 22:18:33 +02:00
parent 93bbba3054
commit 74d02e1472

View File

@@ -1,3 +1,6 @@
/// WatcherAgent - A Rust-based system monitoring agent
/// This agent collects hardware metrics and sends them to a backend server.
/// It supports CPU, GPU, RAM, disk, and network metrics.
//use chrono::Utc; //use chrono::Utc;
use nvml_wrapper::Nvml; use nvml_wrapper::Nvml;
use reqwest::{Client, StatusCode}; use reqwest::{Client, StatusCode};
@@ -6,6 +9,16 @@ use std::{error::Error, fs, process::Command, time::Duration};
use sysinfo::{Components, Disks, System}; use sysinfo::{Components, Disks, System};
use tokio::time::{interval, sleep, Instant}; use tokio::time::{interval, sleep, Instant};
// Windows specific imports
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use winapi::shared::winerror::ERROR_SUCCESS;
use winapi::um::pdh::PDH_FMT_COUNTERVALUE;
use winapi::um::pdh::{
PdhAddCounterW, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQueryW, PDH_FMT_DOUBLE,
};
// Data structures matching the C# DTOs // Data structures matching the C# DTOs
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
struct RegistrationDto { struct RegistrationDto {
@@ -417,6 +430,7 @@ 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")]
{ {
println!("Attempting to get CPU temperature on Linux...");
let mut sys = System::new_all(); let mut sys = System::new_all();
//let components = Components::new_with_refreshed_list(); //let components = Components::new_with_refreshed_list();
sys.refresh_all(); sys.refresh_all();
@@ -432,19 +446,9 @@ fn get_cpu_temp() -> Option<f32> {
Some(0.0) Some(0.0)
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use winapi::shared::winerror::ERROR_SUCCESS;
use winapi::um::pdh::PDH_FMT_COUNTERVALUE;
use winapi::um::pdh::{
PdhAddCounterW, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQueryW,
PDH_FMT_DOUBLE,
};
fn get_cpu_temp() -> Option<f32> {
println!("Attempting to get CPU temperature on Windows..."); println!("Attempting to get CPU temperature on Windows...");
unsafe { unsafe {
@@ -458,25 +462,23 @@ fn get_cpu_temp() -> Option<f32> {
} }
// Create the counter path (this is system-dependent and may need adjustment) // Create the counter path (this is system-dependent and may need adjustment)
let counter_path: Vec<u16> = let counter_path: Vec<u16> = OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
.encode_wide() .encode_wide()
.chain(Some(0)) .chain(Some(0))
.collect(); .collect();
// Add counter // Add counter
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter) if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter) != ERROR_SUCCESS as i32
!= ERROR_SUCCESS as i32
{ {
println!("Failed to add PDH counter"); println!("Failed to add PDH counter");
PdhCloseQuery(query); pdh_close_query(query);
return None; return None;
} }
// Collect data // Collect data
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 { if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
println!("Failed to collect query data"); println!("Failed to collect query data");
PdhCloseQuery(query); pdh_close_query(query);
return None; return None;
} }
@@ -490,32 +492,33 @@ fn get_cpu_temp() -> Option<f32> {
!= ERROR_SUCCESS as i32 != ERROR_SUCCESS as i32
{ {
println!("Failed to get formatted counter value"); println!("Failed to get formatted counter value");
PdhCloseQuery(query); pdh_close_query(query);
return None; return None;
} }
PdhCloseQuery(query); pdh_close_query(query);
if value.CStatus == ERROR_SUCCESS as u32 { if value.CStatus == ERROR_SUCCESS as u32 {
Some(*value.u.doubleValue() as f32) return Some(*value.u.doubleValue() as f32);
} else { } else {
None return None;
} }
} }
} }
// Helper function to close the PDH query // Helper function to close the PDH query
unsafe fn PdhCloseQuery(query: *mut winapi::ctypes::c_void) { unsafe fn pdh_close_query(query: *mut winapi::ctypes::c_void) {
use winapi::um::pdh::PdhCloseQuery; use winapi::um::pdh::PdhCloseQuery;
PdhCloseQuery(query); PdhCloseQuery(query);
} }
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))] #[cfg(not(any(target_os = "linux", target_os = "windows")))]
{ {
// Fallback for unsupported OS // Fallback for unsupported OS
println!("CPU temperature retrieval not supported on this OS."); println!("CPU temperature retrieval not supported on this OS.");
None None
} }
None None
} }