use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
static EVENT_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
fn generate_event_id() -> String {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0);
let counter = EVENT_ID_COUNTER.fetch_add(1, Ordering::SeqCst);
format!("evt-{}-{}", timestamp, counter)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventSeverity {
Info,
Warning,
Error,
Critical,
}
impl std::fmt::Display for EventSeverity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Info => write!(f, "info"),
Self::Warning => write!(f, "warning"),
Self::Error => write!(f, "error"),
Self::Critical => write!(f, "critical"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventCategory {
Cpu,
Gpu,
Memory,
Disk,
Network,
Power,
Temperature,
Fan,
Process,
System,
Hardware,
Service,
Security,
Custom,
}
impl std::fmt::Display for EventCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Cpu => write!(f, "cpu"),
Self::Gpu => write!(f, "gpu"),
Self::Memory => write!(f, "memory"),
Self::Disk => write!(f, "disk"),
Self::Network => write!(f, "network"),
Self::Power => write!(f, "power"),
Self::Temperature => write!(f, "temperature"),
Self::Fan => write!(f, "fan"),
Self::Process => write!(f, "process"),
Self::System => write!(f, "system"),
Self::Hardware => write!(f, "hardware"),
Self::Service => write!(f, "service"),
Self::Security => write!(f, "security"),
Self::Custom => write!(f, "custom"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemEvent {
pub id: String,
pub timestamp: u64,
pub category: EventCategory,
pub severity: EventSeverity,
pub event_type: String,
pub message: String,
pub source: String,
pub metadata: HashMap<String, serde_json::Value>,
pub previous_value: Option<serde_json::Value>,
pub current_value: Option<serde_json::Value>,
pub acknowledged: bool,
}
impl SystemEvent {
pub fn new(
category: EventCategory,
severity: EventSeverity,
event_type: impl Into<String>,
message: impl Into<String>,
source: impl Into<String>,
) -> Self {
Self {
id: generate_event_id(),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
category,
severity,
event_type: event_type.into(),
message: message.into(),
source: source.into(),
metadata: HashMap::new(),
previous_value: None,
current_value: None,
acknowledged: false,
}
}
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
if let Ok(v) = serde_json::to_value(value) {
self.metadata.insert(key.into(), v);
}
self
}
pub fn with_change(mut self, previous: impl Serialize, current: impl Serialize) -> Self {
self.previous_value = serde_json::to_value(previous).ok();
self.current_value = serde_json::to_value(current).ok();
self
}
pub fn info(category: EventCategory, event_type: &str, message: &str, source: &str) -> Self {
Self::new(category, EventSeverity::Info, event_type, message, source)
}
pub fn warning(category: EventCategory, event_type: &str, message: &str, source: &str) -> Self {
Self::new(
category,
EventSeverity::Warning,
event_type,
message,
source,
)
}
pub fn error(category: EventCategory, event_type: &str, message: &str, source: &str) -> Self {
Self::new(category, EventSeverity::Error, event_type, message, source)
}
pub fn critical(
category: EventCategory,
event_type: &str,
message: &str,
source: &str,
) -> Self {
Self::new(
category,
EventSeverity::Critical,
event_type,
message,
source,
)
}
}
pub mod event_types {
pub mod cpu {
pub const HIGH_USAGE: &str = "high_usage";
pub const THROTTLING: &str = "throttling";
pub const HIGH_TEMPERATURE: &str = "high_temperature";
pub const FREQUENCY_CHANGE: &str = "frequency_change";
}
pub mod gpu {
pub const HIGH_USAGE: &str = "high_usage";
pub const HIGH_MEMORY: &str = "high_memory";
pub const HIGH_TEMPERATURE: &str = "high_temperature";
pub const THROTTLING: &str = "throttling";
pub const DRIVER_ERROR: &str = "driver_error";
pub const POWER_LIMIT: &str = "power_limit";
pub const ECC_ERROR: &str = "ecc_error";
}
pub mod memory {
pub const HIGH_USAGE: &str = "high_usage";
pub const LOW_AVAILABLE: &str = "low_available";
pub const SWAP_ACTIVE: &str = "swap_active";
pub const HIGH_SWAP: &str = "high_swap";
pub const OOM_RISK: &str = "oom_risk";
}
pub mod disk {
pub const LOW_SPACE: &str = "low_space";
pub const HIGH_IO: &str = "high_io";
pub const SMART_WARNING: &str = "smart_warning";
pub const MOUNT_CHANGE: &str = "mount_change";
}
pub mod network {
pub const HIGH_BANDWIDTH: &str = "high_bandwidth";
pub const LINK_UP: &str = "link_up";
pub const LINK_DOWN: &str = "link_down";
pub const HIGH_ERRORS: &str = "high_errors";
pub const HIGH_DROPPED: &str = "high_dropped";
}
pub mod power {
pub const LOW_BATTERY: &str = "low_battery";
pub const CRITICAL_BATTERY: &str = "critical_battery";
pub const POWER_CONNECTED: &str = "power_connected";
pub const POWER_DISCONNECTED: &str = "power_disconnected";
pub const HIGH_POWER_DRAW: &str = "high_power_draw";
}
pub mod temperature {
pub const HIGH_TEMP: &str = "high_temp";
pub const CRITICAL_TEMP: &str = "critical_temp";
pub const SENSOR_ERROR: &str = "sensor_error";
}
pub mod fan {
pub const HIGH_SPEED: &str = "high_speed";
pub const FAN_STOPPED: &str = "fan_stopped";
pub const FAN_ERROR: &str = "fan_error";
}
pub mod process {
pub const HIGH_CPU: &str = "high_cpu";
pub const HIGH_MEMORY: &str = "high_memory";
pub const STARTED: &str = "started";
pub const STOPPED: &str = "stopped";
pub const CRASHED: &str = "crashed";
}
pub mod hardware {
pub const DEVICE_ADDED: &str = "device_added";
pub const DEVICE_REMOVED: &str = "device_removed";
pub const DEVICE_ERROR: &str = "device_error";
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EventFilter {
pub categories: Option<Vec<EventCategory>>,
pub min_severity: Option<EventSeverity>,
pub source_pattern: Option<String>,
pub event_types: Option<Vec<String>>,
}
impl EventFilter {
pub fn new() -> Self {
Self::default()
}
pub fn with_categories(mut self, categories: Vec<EventCategory>) -> Self {
self.categories = Some(categories);
self
}
pub fn with_min_severity(mut self, severity: EventSeverity) -> Self {
self.min_severity = Some(severity);
self
}
pub fn with_source_pattern(mut self, pattern: impl Into<String>) -> Self {
self.source_pattern = Some(pattern.into());
self
}
pub fn with_event_types(mut self, types: Vec<String>) -> Self {
self.event_types = Some(types);
self
}
pub fn matches(&self, event: &SystemEvent) -> bool {
if let Some(ref categories) = self.categories {
if !categories.contains(&event.category) {
return false;
}
}
if let Some(min_severity) = self.min_severity {
let event_level = match event.severity {
EventSeverity::Info => 0,
EventSeverity::Warning => 1,
EventSeverity::Error => 2,
EventSeverity::Critical => 3,
};
let min_level = match min_severity {
EventSeverity::Info => 0,
EventSeverity::Warning => 1,
EventSeverity::Error => 2,
EventSeverity::Critical => 3,
};
if event_level < min_level {
return false;
}
}
if let Some(ref pattern) = self.source_pattern {
if !event.source.contains(pattern) {
return false;
}
}
if let Some(ref types) = self.event_types {
if !types.contains(&event.event_type) {
return false;
}
}
true
}
}
pub type EventCallback = Box<dyn Fn(&SystemEvent) + Send + Sync>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SubscriptionId(u64);
pub struct EventManager {
events: RwLock<Vec<SystemEvent>>,
max_events: usize,
subscribers: RwLock<HashMap<SubscriptionId, (EventFilter, Arc<EventCallback>)>>,
next_sub_id: RwLock<u64>,
}
impl EventManager {
pub fn new(max_events: usize) -> Self {
Self {
events: RwLock::new(Vec::with_capacity(max_events)),
max_events,
subscribers: RwLock::new(HashMap::new()),
next_sub_id: RwLock::new(0),
}
}
pub fn emit(&self, event: SystemEvent) {
if let Ok(subs) = self.subscribers.read() {
for (filter, callback) in subs.values() {
if filter.matches(&event) {
callback(&event);
}
}
}
if let Ok(mut events) = self.events.write() {
if events.len() >= self.max_events {
events.remove(0);
}
events.push(event);
}
}
pub fn subscribe(&self, filter: EventFilter, callback: EventCallback) -> SubscriptionId {
let mut next_id = self.next_sub_id.write().unwrap();
let id = SubscriptionId(*next_id);
*next_id += 1;
if let Ok(mut subs) = self.subscribers.write() {
subs.insert(id, (filter, Arc::new(callback)));
}
id
}
pub fn unsubscribe(&self, id: SubscriptionId) {
if let Ok(mut subs) = self.subscribers.write() {
subs.remove(&id);
}
}
pub fn get_events(
&self,
filter: Option<&EventFilter>,
limit: Option<usize>,
) -> Vec<SystemEvent> {
if let Ok(events) = self.events.read() {
let filtered: Vec<_> = events
.iter()
.filter(|e| filter.map(|f| f.matches(e)).unwrap_or(true))
.cloned()
.collect();
match limit {
Some(n) => filtered.into_iter().rev().take(n).collect(),
None => filtered.into_iter().rev().collect(),
}
} else {
Vec::new()
}
}
pub fn get_events_since(&self, since: u64) -> Vec<SystemEvent> {
if let Ok(events) = self.events.read() {
events
.iter()
.filter(|e| e.timestamp >= since)
.cloned()
.collect()
} else {
Vec::new()
}
}
pub fn acknowledge(&self, event_id: &str) -> bool {
if let Ok(mut events) = self.events.write() {
for event in events.iter_mut() {
if event.id == event_id {
event.acknowledged = true;
return true;
}
}
}
false
}
pub fn get_unacknowledged(&self) -> Vec<SystemEvent> {
if let Ok(events) = self.events.read() {
events.iter().filter(|e| !e.acknowledged).cloned().collect()
} else {
Vec::new()
}
}
pub fn clear(&self) {
if let Ok(mut events) = self.events.write() {
events.clear();
}
}
}
impl Default for EventManager {
fn default() -> Self {
Self::new(1000)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdConfig {
pub cpu_high_percent: Option<f32>,
pub memory_high_percent: Option<f32>,
pub gpu_high_percent: Option<f32>,
pub gpu_high_temp_c: Option<f32>,
pub cpu_high_temp_c: Option<f32>,
pub disk_high_percent: Option<f32>,
pub battery_low_percent: Option<f32>,
pub network_error_rate: Option<f32>,
pub process_cpu_percent: Option<f32>,
pub process_memory_mb: Option<u64>,
}
impl Default for ThresholdConfig {
fn default() -> Self {
Self {
cpu_high_percent: Some(90.0),
memory_high_percent: Some(90.0),
gpu_high_percent: Some(95.0),
gpu_high_temp_c: Some(85.0),
cpu_high_temp_c: Some(90.0),
disk_high_percent: Some(90.0),
battery_low_percent: Some(20.0),
network_error_rate: Some(5.0),
process_cpu_percent: Some(80.0),
process_memory_mb: Some(4096),
}
}
}
pub struct AlertChecker {
thresholds: ThresholdConfig,
event_manager: Arc<EventManager>,
last_alerts: RwLock<HashMap<String, u64>>,
alert_cooldown: Duration,
}
impl AlertChecker {
pub fn new(thresholds: ThresholdConfig, event_manager: Arc<EventManager>) -> Self {
Self {
thresholds,
event_manager,
last_alerts: RwLock::new(HashMap::new()),
alert_cooldown: Duration::from_secs(60),
}
}
pub fn with_cooldown(mut self, cooldown: Duration) -> Self {
self.alert_cooldown = cooldown;
self
}
fn should_alert(&self, key: &str) -> bool {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
if let Ok(mut last) = self.last_alerts.write() {
if let Some(&last_time) = last.get(key) {
if now - last_time < self.alert_cooldown.as_secs() {
return false;
}
}
last.insert(key.to_string(), now);
true
} else {
true
}
}
pub fn check_cpu(&self, usage: f32, temperature: Option<f32>) {
if let Some(threshold) = self.thresholds.cpu_high_percent {
if usage > threshold && self.should_alert("cpu_usage") {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Cpu,
event_types::cpu::HIGH_USAGE,
&format!("CPU usage at {:.1}% (threshold: {:.1}%)", usage, threshold),
"cpu",
)
.with_metadata("usage_percent", usage)
.with_metadata("threshold_percent", threshold),
);
}
}
if let Some(temp) = temperature {
if let Some(threshold) = self.thresholds.cpu_high_temp_c {
if temp > threshold && self.should_alert("cpu_temp") {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Temperature,
event_types::cpu::HIGH_TEMPERATURE,
&format!(
"CPU temperature at {:.1}°C (threshold: {:.1}°C)",
temp, threshold
),
"cpu",
)
.with_metadata("temperature_c", temp)
.with_metadata("threshold_c", threshold),
);
}
}
}
}
pub fn check_gpu(
&self,
index: usize,
usage: f32,
memory_percent: f32,
temperature: Option<f32>,
) {
let source = format!("gpu:{}", index);
if let Some(threshold) = self.thresholds.gpu_high_percent {
if usage > threshold && self.should_alert(&format!("gpu_{}_usage", index)) {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Gpu,
event_types::gpu::HIGH_USAGE,
&format!(
"GPU {} usage at {:.1}% (threshold: {:.1}%)",
index, usage, threshold
),
&source,
)
.with_metadata("usage_percent", usage)
.with_metadata("threshold_percent", threshold),
);
}
}
if memory_percent > 90.0 && self.should_alert(&format!("gpu_{}_memory", index)) {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Gpu,
event_types::gpu::HIGH_MEMORY,
&format!("GPU {} memory at {:.1}%", index, memory_percent),
&source,
)
.with_metadata("memory_percent", memory_percent),
);
}
if let Some(temp) = temperature {
if let Some(threshold) = self.thresholds.gpu_high_temp_c {
if temp > threshold && self.should_alert(&format!("gpu_{}_temp", index)) {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Temperature,
event_types::gpu::HIGH_TEMPERATURE,
&format!(
"GPU {} temperature at {:.1}°C (threshold: {:.1}°C)",
index, temp, threshold
),
&source,
)
.with_metadata("temperature_c", temp)
.with_metadata("threshold_c", threshold),
);
}
}
}
}
pub fn check_memory(&self, usage_percent: f32, swap_percent: f32) {
if let Some(threshold) = self.thresholds.memory_high_percent {
if usage_percent > threshold && self.should_alert("memory_usage") {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Memory,
event_types::memory::HIGH_USAGE,
&format!(
"Memory usage at {:.1}% (threshold: {:.1}%)",
usage_percent, threshold
),
"memory",
)
.with_metadata("usage_percent", usage_percent)
.with_metadata("threshold_percent", threshold),
);
}
}
if swap_percent > 50.0 && self.should_alert("swap_usage") {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Memory,
event_types::memory::HIGH_SWAP,
&format!("Swap usage at {:.1}%", swap_percent),
"memory",
)
.with_metadata("swap_percent", swap_percent),
);
}
}
pub fn check_disk(&self, device: &str, usage_percent: f32) {
if let Some(threshold) = self.thresholds.disk_high_percent {
if usage_percent > threshold && self.should_alert(&format!("disk_{}", device)) {
self.event_manager.emit(
SystemEvent::warning(
EventCategory::Disk,
event_types::disk::LOW_SPACE,
&format!(
"Disk {} usage at {:.1}% (threshold: {:.1}%)",
device, usage_percent, threshold
),
&format!("disk:{}", device),
)
.with_metadata("usage_percent", usage_percent)
.with_metadata("threshold_percent", threshold),
);
}
}
}
pub fn check_battery(&self, percent: f32, charging: bool) {
if !charging {
if let Some(threshold) = self.thresholds.battery_low_percent {
if percent < threshold && self.should_alert("battery_low") {
let severity = if percent < 10.0 {
EventSeverity::Critical
} else {
EventSeverity::Warning
};
self.event_manager.emit(
SystemEvent::new(
EventCategory::Power,
severity,
if percent < 10.0 {
event_types::power::CRITICAL_BATTERY
} else {
event_types::power::LOW_BATTERY
},
format!("Battery at {:.1}% (threshold: {:.1}%)", percent, threshold),
"battery",
)
.with_metadata("battery_percent", percent)
.with_metadata("threshold_percent", threshold),
);
}
}
}
}
}