use sysinfo::System; use prometheus::{register_gauge, Gauge}; use std::sync::{Arc, OnceLock}; pub fn get_cpu_usage() -> &'static Gauge { static GAUGE: OnceLock = OnceLock::new(); GAUGE.get_or_init(|| { register_gauge!("cpu_usage", "CPU Usage").unwrap() }) } pub fn get_memory_used() -> &'static Gauge { static GAUGE: OnceLock = OnceLock::new(); GAUGE.get_or_init(|| { register_gauge!("memory_used", "Memory Used").unwrap() }) } pub fn get_memory_total() -> &'static Gauge { static GAUGE: OnceLock = OnceLock::new(); GAUGE.get_or_init(|| { register_gauge!("memory_total", "Memory Total").unwrap() }) } pub fn get_memory_usage() -> &'static Gauge { static GAUGE: OnceLock = OnceLock::new(); GAUGE.get_or_init(|| { register_gauge!("memory_usage", "Memory Usage").unwrap() }) } pub fn collect(system: &mut System) -> (f64, f64, f64, f64) { system.refresh_all(); let cpu_usage = system.global_cpu_info().cpu_usage() as f64; let memory_used = system.used_memory() as f64; let memory_total = system.total_memory() as f64; let memory_usage = memory_used / memory_total * 100.0; (cpu_usage, memory_used, memory_total, memory_usage) }