From 4d87dccf62f81560d152ae5264c17b58ed778c72 Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Fri, 5 Dec 2025 10:00:00 +0800 Subject: [PATCH] feat: Add alert system with threshold monitoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create AlertSystem for configurable threshold checks - Define Alert struct for alert history tracking - Implement check_cpu_alert() for CPU threshold violations - Implement check_memory_alert() for memory threshold violations - Implement check_disk_alert() for disk threshold violations - Add trigger_alert() to record and display alerts - Maintain alert history for later analysis - Integrate alert system into SystemMonitor - Add visual alert indicators with ⚠️ emoji --- src/alert.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/monitor.rs | 8 +++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/alert.rs diff --git a/src/alert.rs b/src/alert.rs new file mode 100644 index 0000000..f62f6ba --- /dev/null +++ b/src/alert.rs @@ -0,0 +1,68 @@ +use crate::config::Config; +use crate::logger::TrackerLogger; + +pub struct AlertSystem { + config: Config, + logger: TrackerLogger, + alert_history: Vec, +} + +#[derive(Debug, Clone)] +pub struct Alert { + pub alert_type: String, + pub message: String, +} + +impl AlertSystem { + pub fn new(config: Config) -> Self { + AlertSystem { + config, + logger: TrackerLogger::default(), + alert_history: Vec::new(), + } + } + + pub fn check_cpu_alert(&mut self, cpu_percent: f32) -> bool { + let threshold = self.config.alert_thresholds.cpu_percent; + if cpu_percent > threshold { + let message = format!("CPU usage is {:.2}% (threshold: {:.2}%)", cpu_percent, threshold); + self.trigger_alert("CPU", &message); + return true; + } + false + } + + pub fn check_memory_alert(&mut self, memory_percent: f32) -> bool { + let threshold = self.config.alert_thresholds.memory_percent; + if memory_percent > threshold { + let message = format!("Memory usage is {:.2}% (threshold: {:.2}%)", memory_percent, threshold); + self.trigger_alert("Memory", &message); + return true; + } + false + } + + pub fn check_disk_alert(&mut self, disk_percent: f32) -> bool { + let threshold = self.config.alert_thresholds.disk_percent; + if disk_percent > threshold { + let message = format!("Disk usage is {:.2}% (threshold: {:.2}%)", disk_percent, threshold); + self.trigger_alert("Disk", &message); + return true; + } + false + } + + fn trigger_alert(&mut self, alert_type: &str, message: &str) { + let alert = Alert { + alert_type: alert_type.to_string(), + message: message.to_string(), + }; + self.alert_history.push(alert.clone()); + self.logger.log_alert(&format!("{}: {}", alert_type, message)); + println!("\n⚠️ ALERT: {}", message); + } + + pub fn get_alert_history(&self) -> &[Alert] { + &self.alert_history + } +} diff --git a/src/monitor.rs b/src/monitor.rs index 29c9b0c..f9be865 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -1,5 +1,6 @@ use crate::config::Config; use crate::process::ProcessMonitor; +use crate::alert::AlertSystem; use crate::logger::TrackerLogger; use sysinfo::{System, Disks, Networks}; use chrono::Local; @@ -10,17 +11,19 @@ pub struct SystemMonitor { disks: Disks, networks: Networks, process_monitor: ProcessMonitor, + alert_system: AlertSystem, logger: TrackerLogger, } impl SystemMonitor { pub fn new(config: Config) -> Self { SystemMonitor { - config, + config: config.clone(), sys: System::new_all(), disks: Disks::new_with_refreshed_list(), networks: Networks::new_with_refreshed_list(), process_monitor: ProcessMonitor::new(), + alert_system: AlertSystem::new(config), logger: TrackerLogger::default(), } } @@ -109,6 +112,7 @@ impl SystemMonitor { let cpu_usage = self.get_cpu_usage(); println!("CPU Usage: {:.2}%", cpu_usage); self.logger.log_stats("CPU", &format!("{:.2}%", cpu_usage)); + self.alert_system.check_cpu_alert(cpu_usage); } if self.config.display.show_memory { @@ -119,6 +123,7 @@ impl SystemMonitor { mem.total as f64 / (1024_f64.powi(3)) ); self.logger.log_stats("Memory", &format!("{:.2}%", mem.percent)); + self.alert_system.check_memory_alert(mem.percent); } if self.config.display.show_disk { @@ -129,6 +134,7 @@ impl SystemMonitor { disk.total as f64 / (1024_f64.powi(3)) ); self.logger.log_stats("Disk", &format!("{:.2}%", disk.percent)); + self.alert_system.check_disk_alert(disk.percent); } if self.config.display.show_network {