fixed test with winapi
This commit is contained in:
@@ -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 nvml_wrapper::Nvml;
|
||||
use reqwest::{Client, StatusCode};
|
||||
@@ -6,6 +9,16 @@ use std::{error::Error, fs, process::Command, time::Duration};
|
||||
use sysinfo::{Components, Disks, System};
|
||||
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
|
||||
#[derive(Serialize, Debug)]
|
||||
struct RegistrationDto {
|
||||
@@ -417,6 +430,7 @@ fn get_cpu_temp() -> Option<f32> {
|
||||
println!("Attempting to get CPU temperature...");
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
println!("Attempting to get CPU temperature on Linux...");
|
||||
let mut sys = System::new_all();
|
||||
//let components = Components::new_with_refreshed_list();
|
||||
sys.refresh_all();
|
||||
@@ -432,90 +446,79 @@ fn get_cpu_temp() -> Option<f32> {
|
||||
|
||||
Some(0.0)
|
||||
}
|
||||
|
||||
#[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,
|
||||
};
|
||||
println!("Attempting to get CPU temperature on Windows...");
|
||||
|
||||
fn get_cpu_temp() -> Option<f32> {
|
||||
println!("Attempting to get CPU temperature on Windows...");
|
||||
unsafe {
|
||||
let mut query = ptr::null_mut();
|
||||
let mut counter = ptr::null_mut();
|
||||
|
||||
unsafe {
|
||||
let mut query = ptr::null_mut();
|
||||
let mut counter = ptr::null_mut();
|
||||
// Open a query
|
||||
if PdhOpenQueryW(ptr::null(), 0, &mut query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to open PDH query");
|
||||
return None;
|
||||
}
|
||||
|
||||
// Open a query
|
||||
if PdhOpenQueryW(ptr::null(), 0, &mut query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to open PDH query");
|
||||
return None;
|
||||
}
|
||||
// Create the counter path (this is system-dependent and may need adjustment)
|
||||
let counter_path: Vec<u16> = OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
|
||||
// Create the counter path (this is system-dependent and may need adjustment)
|
||||
let counter_path: Vec<u16> =
|
||||
OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
// Add counter
|
||||
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter) != ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to add PDH counter");
|
||||
pdh_close_query(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
// Add counter
|
||||
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter)
|
||||
!= ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to add PDH counter");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
// Collect data
|
||||
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to collect query data");
|
||||
pdh_close_query(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
// Collect data
|
||||
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to collect query data");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
// Get formatted value
|
||||
let mut value = PDH_FMT_COUNTERVALUE {
|
||||
CStatus: 0,
|
||||
u: std::mem::zeroed(),
|
||||
};
|
||||
|
||||
// Get formatted value
|
||||
let mut value = PDH_FMT_COUNTERVALUE {
|
||||
CStatus: 0,
|
||||
u: std::mem::zeroed(),
|
||||
};
|
||||
if PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, ptr::null_mut(), &mut value)
|
||||
!= ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to get formatted counter value");
|
||||
pdh_close_query(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
if PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, ptr::null_mut(), &mut value)
|
||||
!= ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to get formatted counter value");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
pdh_close_query(query);
|
||||
|
||||
PdhCloseQuery(query);
|
||||
|
||||
if value.CStatus == ERROR_SUCCESS as u32 {
|
||||
Some(*value.u.doubleValue() as f32)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
if value.CStatus == ERROR_SUCCESS as u32 {
|
||||
return Some(*value.u.doubleValue() as f32);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to close the PDH query
|
||||
unsafe fn PdhCloseQuery(query: *mut winapi::ctypes::c_void) {
|
||||
use winapi::um::pdh::PdhCloseQuery;
|
||||
PdhCloseQuery(query);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to close the PDH query
|
||||
unsafe fn pdh_close_query(query: *mut winapi::ctypes::c_void) {
|
||||
use winapi::um::pdh::PdhCloseQuery;
|
||||
PdhCloseQuery(query);
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||
{
|
||||
// Fallback for unsupported OS
|
||||
println!("CPU temperature retrieval not supported on this OS.");
|
||||
None
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user