use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use super::disk::{check_all_mount_points, check_mount_points};
use super::notifier::Notifier;
use super::notifiers::{create_notifier, get_i3_notifier};
use super::types::{AlertLevel, DiskStatus, MonitorOptions};
use crate::error::Result;
pub struct MonitorService {
options: MonitorOptions,
notifier: Box<dyn Notifier>,
i3_notifier: Option<Box<dyn Notifier>>,
running: Arc<AtomicBool>,
last_alerts: HashMap<PathBuf, AlertLevel>,
}
impl MonitorService {
pub fn new(options: MonitorOptions, running: Arc<AtomicBool>) -> Self {
let notifier = create_notifier(options.notification_backend);
let i3_notifier = get_i3_notifier();
tracing::info!("Using notification backend: {}", notifier.name());
if i3_notifier.is_some() {
tracing::info!("i3-nagbar available for critical alerts");
}
Self {
options,
notifier,
i3_notifier,
running,
last_alerts: HashMap::new(),
}
}
pub fn running_flag(&self) -> Arc<AtomicBool> {
Arc::clone(&self.running)
}
pub fn run(&mut self) -> Result<()> {
tracing::info!(
"Starting monitor with {}s interval, warn={}%, critical={}%",
self.options.interval.as_secs(),
self.options.warn_threshold,
self.options.critical_threshold
);
loop {
let start = Instant::now();
self.check_and_notify()?;
if self.options.once {
break;
}
if !self.running.load(Ordering::SeqCst) {
tracing::info!("Monitor stopping");
break;
}
let elapsed = start.elapsed();
if elapsed < self.options.interval {
let sleep_time = self.options.interval - elapsed;
let chunk = Duration::from_secs(1);
let mut remaining = sleep_time;
while remaining > Duration::ZERO && self.running.load(Ordering::SeqCst) {
let sleep = remaining.min(chunk);
thread::sleep(sleep);
remaining = remaining.saturating_sub(sleep);
}
}
}
Ok(())
}
fn check_and_notify(&mut self) -> Result<()> {
let statuses = if self.options.mount_points.is_empty() {
check_all_mount_points()?
} else {
check_mount_points(&self.options.mount_points)?
};
for status in statuses {
let level = AlertLevel::from_percent(
status.percent,
self.options.warn_threshold,
self.options.critical_threshold,
);
self.maybe_send_alert(level, &status)?;
}
Ok(())
}
fn maybe_send_alert(&mut self, level: AlertLevel, status: &DiskStatus) -> Result<()> {
let last_level = self
.last_alerts
.get(&status.mount_point)
.copied()
.unwrap_or(AlertLevel::Normal);
let should_notify = match level {
AlertLevel::Normal => false,
AlertLevel::Emergency => true, _ => level > last_level,
};
if should_notify {
tracing::info!(
"Sending {:?} alert for {} ({}%)",
level,
status.mount_point.display(),
status.percent as u32
);
let delivered = match self.notifier.send_alert(level, status) {
Ok(()) => true,
Err(e) => {
tracing::error!("Failed to send notification: {}", e);
false
}
};
if level >= AlertLevel::Critical {
if let Some(ref i3) = self.i3_notifier {
if let Err(e) = i3.send_alert(level, status) {
tracing::warn!("Failed to send i3-nagbar notification: {}", e);
}
}
}
if delivered {
self.last_alerts.insert(status.mount_point.clone(), level);
}
return Ok(());
}
self.last_alerts.insert(status.mount_point.clone(), level);
Ok(())
}
pub fn stop(&self) {
self.running.store(false, Ordering::SeqCst);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::SweeperError;
use crate::monitor::types::NotificationUrgency;
use std::sync::atomic::AtomicUsize;
struct FailingNotifier {
attempts: Arc<AtomicUsize>,
}
impl Notifier for FailingNotifier {
fn name(&self) -> &'static str {
"failing-test-notifier"
}
fn is_available(&self) -> bool {
true
}
fn send_alert(&self, _level: AlertLevel, _status: &DiskStatus) -> Result<()> {
self.attempts.fetch_add(1, Ordering::SeqCst);
Err(SweeperError::Other("injected notification failure".into()))
}
fn send(&self, _title: &str, _body: &str, _urgency: NotificationUrgency) -> Result<()> {
Err(SweeperError::Other("injected notification failure".into()))
}
}
#[test]
fn test_monitor_service_creation() {
let options = MonitorOptions::default();
let running = Arc::new(AtomicBool::new(true));
let service = MonitorService::new(options, running);
assert!(service.running.load(Ordering::SeqCst));
}
#[test]
fn test_monitor_once_mode() {
let options = MonitorOptions {
once: true,
..Default::default()
};
let running = Arc::new(AtomicBool::new(true));
let mut service = MonitorService::new(options, running);
let result = service.run();
assert!(result.is_ok());
}
#[test]
fn test_monitor_stop() {
let options = MonitorOptions::default();
let running = Arc::new(AtomicBool::new(true));
let service = MonitorService::new(options, running);
service.stop();
assert!(!service.running.load(Ordering::SeqCst));
}
#[test]
fn test_running_flag_shared() {
let options = MonitorOptions::default();
let running = Arc::new(AtomicBool::new(true));
let service = MonitorService::new(options, Arc::clone(&running));
let flag = service.running_flag();
assert!(flag.load(Ordering::SeqCst));
running.store(false, Ordering::SeqCst);
assert!(!flag.load(Ordering::SeqCst));
}
#[test]
fn test_failed_alert_is_retried_at_same_level() {
let attempts = Arc::new(AtomicUsize::new(0));
let mut service = MonitorService {
options: MonitorOptions::default(),
notifier: Box::new(FailingNotifier {
attempts: Arc::clone(&attempts),
}),
i3_notifier: None,
running: Arc::new(AtomicBool::new(true)),
last_alerts: HashMap::new(),
};
let status = DiskStatus {
mount_point: PathBuf::from("/test"),
device: None,
total: 100,
used: 85,
available: 15,
percent: 85.0,
};
service
.maybe_send_alert(AlertLevel::Warning, &status)
.unwrap();
service
.maybe_send_alert(AlertLevel::Warning, &status)
.unwrap();
assert_eq!(attempts.load(Ordering::SeqCst), 2);
}
}