mirror of
https://github.com/m1ngsama/tracker.git
synced 2025-12-24 10:51:43 +00:00
feat: Add alert system with threshold monitoring
- 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
This commit is contained in:
parent
3af5a6182e
commit
4d87dccf62
2 changed files with 75 additions and 1 deletions
68
src/alert.rs
Normal file
68
src/alert.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
use crate::config::Config;
|
||||
use crate::logger::TrackerLogger;
|
||||
|
||||
pub struct AlertSystem {
|
||||
config: Config,
|
||||
logger: TrackerLogger,
|
||||
alert_history: Vec<Alert>,
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue