From af48990cf7d20df12349f4381edfd3da335758bf Mon Sep 17 00:00:00 2001 From: JeremyLARDENOIS Date: Tue, 22 Jul 2025 22:54:43 +0200 Subject: [PATCH] refactor(resources): implement ResourceThreshold struct --- node_notifier/src/main.rs | 118 +++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/node_notifier/src/main.rs b/node_notifier/src/main.rs index 2696b04..265774c 100644 --- a/node_notifier/src/main.rs +++ b/node_notifier/src/main.rs @@ -2,25 +2,49 @@ use discord_client::DiscordNotifier; use dotenvy::dotenv; use sysinfo::{Disks, System}; -fn get_cpu_usage(sys: &System) -> f32 { +struct ResourceThreshold { + name: String, + get_usage_fn: fn(&System, &Disks) -> f32, + threshold: f32, + value: f32, + messagef: String, +} + +impl ResourceThreshold { + fn check(&mut self, sys: &System, disks: &Disks) -> Option { + self.value = (self.get_usage_fn)(sys, disks); + if self.value > self.threshold { + Some( + format!( + "{}: {:.2}% - {}", + self.name, self.value, self.messagef + ) + ) + } else { + None + } + } +} + +fn get_cpu_usage(sys: &System, _: &Disks) -> f32 { sys.global_cpu_usage() } -fn get_memory_usage(sys: &System) -> f32 { +fn get_memory_usage(sys: &System, _: &Disks) -> f32 { let total_memory = sys.total_memory() as f32; let used_memory = sys.used_memory() as f32; used_memory / total_memory * 100.0 } -fn get_swap_usage(sys: &System) -> f32 { +fn get_swap_usage(sys: &System, _: &Disks) -> f32 { let total_swap = sys.total_swap() as f32; let used_swap = sys.used_swap() as f32; used_swap / total_swap * 100.0 } -fn get_disk_usage(disks: &Disks) -> f32 { +fn get_disk_usage(_: &System, disks: &Disks) -> f32 { let mut total_used_space = 0.0; let mut total_space = 0.0; @@ -40,6 +64,39 @@ fn get_disk_usage(disks: &Disks) -> f32 { (total_used_space / total_space) * 100.0 } +fn get_resource_thresholds() -> Vec { + vec![ + ResourceThreshold { + name: "CPU".to_string(), + get_usage_fn: get_cpu_usage, + threshold: 80.0, + value: 0.0, + messagef: "High CPU usage detected".to_string(), + }, + ResourceThreshold { + name: "Memory".to_string(), + get_usage_fn: get_memory_usage, + threshold: 80.0, + value: 0.0, + messagef: "High memory usage detected".to_string(), + }, + ResourceThreshold { + name: "Swap".to_string(), + get_usage_fn: get_swap_usage, + threshold: 80.0, + value: 0.0, + messagef: "High swap usage detected".to_string(), + }, + ResourceThreshold { + name: "Disk".to_string(), + get_usage_fn: get_disk_usage, + threshold: 80.0, + value: 0.0, + messagef: "High disk usage detected".to_string(), + }, + ] +} + fn main() { dotenv().ok(); @@ -54,51 +111,22 @@ fn main() { sys.refresh_all(); disks.refresh(true); - let cpu_usage = get_cpu_usage(&sys); - if cpu_usage > 80.0 { - notifier - .send_notification(&format!("High CPU usage detected: {cpu_usage:.2}%")) - .expect("Failed to send notification for high CPU usage"); - } + let mut thresholds = get_resource_thresholds(); - let memory_usage = get_memory_usage(&sys); - if memory_usage > 80.0 { - notifier - .send_notification(&format!("High memory usage detected: {memory_usage:.2}%")) - .expect("Failed to send notification for high memory usage"); + for threshold in &mut thresholds { + if let Some(message) = threshold.check(&sys, &disks) { + notifier.send_notification(&message).expect("Failed to send notification"); + } } - let swap_usage = get_swap_usage(&sys); - if swap_usage > 80.0 { - notifier - .send_notification(&format!("High swap usage detected: {swap_usage:.2}%")) - .expect("Failed to send notification for high swap usage"); - } - - let disk_usage = get_disk_usage(&disks); - if disk_usage > 80.0 { - notifier - .send_notification(&format!("High disk usage detected: {disk_usage:.2}%")) - .expect("Failed to send notification for high disk usage"); - } - - println!("System Information:"); - println!("CPU usage: {cpu_usage:.2}%"); - println!("Memory usage: {memory_usage:.2}%"); - println!("Swap usage: {swap_usage:.2}%"); - println!("Disk usage: {disk_usage:.2}%"); - // Send a final notification with all system information - let final_message = format!( - concat!( - "System Information:\n", - "CPU usage: {:.2}%\n", - "Memory usage: {:.2}%\n", - "Swap usage: {:.2}%\n", - "Disk usage: {:.2}%" - ), - cpu_usage, memory_usage, swap_usage, disk_usage - ); + let mut final_message = String::new(); + for threshold in thresholds { + final_message.push_str(&format!( + "{} usage: {:.2}%\n", + threshold.name, threshold.value + )); + } notifier .send_notification(&final_message)