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 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,90 +446,79 @@ fn get_cpu_temp() -> Option<f32> {
|
|||||||
|
|
||||||
Some(0.0)
|
Some(0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
use std::ffi::OsStr;
|
println!("Attempting to get CPU temperature on Windows...");
|
||||||
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> {
|
unsafe {
|
||||||
println!("Attempting to get CPU temperature on Windows...");
|
let mut query = ptr::null_mut();
|
||||||
|
let mut counter = ptr::null_mut();
|
||||||
|
|
||||||
unsafe {
|
// Open a query
|
||||||
let mut query = ptr::null_mut();
|
if PdhOpenQueryW(ptr::null(), 0, &mut query) != ERROR_SUCCESS as i32 {
|
||||||
let mut counter = ptr::null_mut();
|
println!("Failed to open PDH query");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
// Open a query
|
// Create the counter path (this is system-dependent and may need adjustment)
|
||||||
if PdhOpenQueryW(ptr::null(), 0, &mut query) != ERROR_SUCCESS as i32 {
|
let counter_path: Vec<u16> = OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
|
||||||
println!("Failed to open PDH query");
|
.encode_wide()
|
||||||
return None;
|
.chain(Some(0))
|
||||||
}
|
.collect();
|
||||||
|
|
||||||
// Create the counter path (this is system-dependent and may need adjustment)
|
// Add counter
|
||||||
let counter_path: Vec<u16> =
|
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter) != ERROR_SUCCESS as i32
|
||||||
OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
|
{
|
||||||
.encode_wide()
|
println!("Failed to add PDH counter");
|
||||||
.chain(Some(0))
|
pdh_close_query(query);
|
||||||
.collect();
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
// Add counter
|
// Collect data
|
||||||
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter)
|
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
|
||||||
!= ERROR_SUCCESS as i32
|
println!("Failed to collect query data");
|
||||||
{
|
pdh_close_query(query);
|
||||||
println!("Failed to add PDH counter");
|
return None;
|
||||||
PdhCloseQuery(query);
|
}
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collect data
|
// Get formatted value
|
||||||
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
|
let mut value = PDH_FMT_COUNTERVALUE {
|
||||||
println!("Failed to collect query data");
|
CStatus: 0,
|
||||||
PdhCloseQuery(query);
|
u: std::mem::zeroed(),
|
||||||
return None;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// Get formatted value
|
if PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, ptr::null_mut(), &mut value)
|
||||||
let mut value = PDH_FMT_COUNTERVALUE {
|
!= ERROR_SUCCESS as i32
|
||||||
CStatus: 0,
|
{
|
||||||
u: std::mem::zeroed(),
|
println!("Failed to get formatted counter value");
|
||||||
};
|
pdh_close_query(query);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, ptr::null_mut(), &mut value)
|
pdh_close_query(query);
|
||||||
!= ERROR_SUCCESS as i32
|
|
||||||
{
|
|
||||||
println!("Failed to get formatted counter value");
|
|
||||||
PdhCloseQuery(query);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
PdhCloseQuery(query);
|
if value.CStatus == ERROR_SUCCESS as u32 {
|
||||||
|
return Some(*value.u.doubleValue() as f32);
|
||||||
if value.CStatus == ERROR_SUCCESS as u32 {
|
} else {
|
||||||
Some(*value.u.doubleValue() as f32)
|
return None;
|
||||||
} else {
|
|
||||||
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")))]
|
#[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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user