Files
watcheragent/WatcherAgent/src/hardware/memory.rs
2025-08-09 12:47:50 +02:00

28 lines
652 B
Rust

use std::error::Error;
use anyhow::Result;
use sysinfo::System;
#[derive(Debug)]
pub struct MemoryInfo {
pub total: Option<f64>,
pub used: Option<f64>,
pub free: Option<f64>,
}
pub async fn get_memory_info() -> Result<MemoryInfo> {
let mut sys = System::new();
sys.refresh_memory();
Ok(MemoryInfo {
total: Some(sys.total_memory() as f64),
used: Some(sys.used_memory() as f64),
free: Some(sys.free_memory() as f64),
})
}
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error>> {
sys.refresh_memory();
Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0)
}