You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

use sysinfo::System;
use prometheus::{register_gauge, Gauge};
use std::sync::{Arc, OnceLock};
pub fn get_cpu_usage() -> &'static Gauge {
static GAUGE: OnceLock<Gauge> = OnceLock::new();
GAUGE.get_or_init(|| {
register_gauge!("cpu_usage", "CPU Usage").unwrap()
})
}
pub fn get_memory_used() -> &'static Gauge {
static GAUGE: OnceLock<Gauge> = OnceLock::new();
GAUGE.get_or_init(|| {
register_gauge!("memory_used", "Memory Used").unwrap()
})
}
pub fn get_memory_total() -> &'static Gauge {
static GAUGE: OnceLock<Gauge> = OnceLock::new();
GAUGE.get_or_init(|| {
register_gauge!("memory_total", "Memory Total").unwrap()
})
}
pub fn get_memory_usage() -> &'static Gauge {
static GAUGE: OnceLock<Gauge> = 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)
}