getting more cpu data

This commit is contained in:
2025-08-11 20:05:14 +02:00
parent 462c5f065c
commit 8a49166718

View File

@@ -1,6 +1,5 @@
use anyhow::Result; use anyhow::Result;
use std::error::Error; use std::error::Error;
//use std::result::Result;
use sysinfo::System; use sysinfo::System;
#[derive(Debug)] #[derive(Debug)]
@@ -9,13 +8,17 @@ pub struct CpuInfo {
pub cores: Option<i32>, pub cores: Option<i32>,
pub current_load: Option<f64>, pub current_load: Option<f64>,
pub current_temp: Option<f64>, pub current_temp: Option<f64>,
pub uptime: Option<f64>,
pub host_name: Option<String>,
} }
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> { pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
let mut sys = System::new(); let mut sys = System::new_all();
sys.refresh_cpu_all();
let cpus = sys.cpus(); let cpus = sys.cpus();
let uptime = System::uptime() as f64;
let host_name = System::host_name().unwrap_or_else(|| "unknown".to_string());
Ok(CpuInfo { Ok(CpuInfo {
name: Some( name: Some(
cpus.first() cpus.first()
@@ -25,6 +28,8 @@ pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
cores: Some(cpus.len() as i32), cores: Some(cpus.len() as i32),
current_load: get_cpu_load(&mut sys).await.ok(), current_load: get_cpu_load(&mut sys).await.ok(),
current_temp: get_cpu_temp().await.ok(), current_temp: get_cpu_temp().await.ok(),
uptime: Some(uptime),
host_name: Some(host_name),
}) })
} }