use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct DiskStatus {
pub mount_point: PathBuf,
pub device: Option<String>,
pub total: u64,
pub used: u64,
pub available: u64,
pub percent: f32,
}
impl DiskStatus {
pub fn used_human(&self) -> String {
humansize::format_size(self.used, humansize::BINARY)
}
pub fn total_human(&self) -> String {
humansize::format_size(self.total, humansize::BINARY)
}
pub fn available_human(&self) -> String {
humansize::format_size(self.available, humansize::BINARY)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AlertLevel {
Normal,
Warning,
Critical,
Emergency,
}
impl AlertLevel {
pub fn from_percent(percent: f32, warn: u8, critical: u8) -> Self {
let emergency_threshold = 95.0_f32.max(critical as f32 + 1.0);
if percent >= emergency_threshold {
AlertLevel::Emergency
} else if percent >= critical as f32 {
AlertLevel::Critical
} else if percent >= warn as f32 {
AlertLevel::Warning
} else {
AlertLevel::Normal
}
}
pub fn urgency(&self) -> NotificationUrgency {
match self {
AlertLevel::Normal => NotificationUrgency::Low,
AlertLevel::Warning => NotificationUrgency::Normal,
AlertLevel::Critical => NotificationUrgency::Critical,
AlertLevel::Emergency => NotificationUrgency::Critical,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationUrgency {
Low,
Normal,
Critical,
}
#[derive(Debug, Clone)]
pub struct MonitorOptions {
pub interval: Duration,
pub warn_threshold: u8,
pub critical_threshold: u8,
pub mount_points: Vec<PathBuf>,
pub daemon: bool,
pub once: bool,
pub notification_backend: NotificationBackend,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NotificationBackend {
#[default]
Auto,
DBus,
NotifySend,
I3Nagbar,
Stderr,
}
impl Default for MonitorOptions {
fn default() -> Self {
Self {
interval: Duration::from_secs(300),
warn_threshold: 80,
critical_threshold: 90,
mount_points: vec![],
daemon: false,
once: false,
notification_backend: NotificationBackend::Auto,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_alert_level_from_percent() {
assert_eq!(AlertLevel::from_percent(50.0, 80, 90), AlertLevel::Normal);
assert_eq!(AlertLevel::from_percent(80.0, 80, 90), AlertLevel::Warning);
assert_eq!(AlertLevel::from_percent(85.0, 80, 90), AlertLevel::Warning);
assert_eq!(AlertLevel::from_percent(90.0, 80, 90), AlertLevel::Critical);
assert_eq!(
AlertLevel::from_percent(95.0, 80, 90),
AlertLevel::Emergency
);
assert_eq!(
AlertLevel::from_percent(99.0, 80, 90),
AlertLevel::Emergency
);
}
#[test]
fn test_custom_thresholds_above_95_remain_reachable() {
assert_eq!(AlertLevel::from_percent(96.0, 96, 99), AlertLevel::Warning);
assert_eq!(AlertLevel::from_percent(99.0, 96, 99), AlertLevel::Critical);
assert_eq!(
AlertLevel::from_percent(100.0, 96, 99),
AlertLevel::Emergency
);
}
#[test]
fn test_alert_level_ordering() {
assert!(AlertLevel::Normal < AlertLevel::Warning);
assert!(AlertLevel::Warning < AlertLevel::Critical);
assert!(AlertLevel::Critical < AlertLevel::Emergency);
}
#[test]
fn test_disk_status_human_readable() {
let status = DiskStatus {
mount_point: PathBuf::from("/"),
device: Some("/dev/sda1".to_string()),
total: 1024 * 1024 * 1024 * 100, used: 1024 * 1024 * 1024 * 80, available: 1024 * 1024 * 1024 * 20, percent: 80.0,
};
assert!(status.total_human().contains("100"));
assert!(status.used_human().contains("80"));
}
#[test]
fn test_default_monitor_options() {
let options = MonitorOptions::default();
assert_eq!(options.interval, Duration::from_secs(300));
assert_eq!(options.warn_threshold, 80);
assert_eq!(options.critical_threshold, 90);
assert!(!options.daemon);
assert!(!options.once);
}
#[test]
fn test_urgency_mapping() {
assert_eq!(AlertLevel::Normal.urgency(), NotificationUrgency::Low);
assert_eq!(AlertLevel::Warning.urgency(), NotificationUrgency::Normal);
assert_eq!(
AlertLevel::Critical.urgency(),
NotificationUrgency::Critical
);
assert_eq!(
AlertLevel::Emergency.urgency(),
NotificationUrgency::Critical
);
}
}