use crate::error::{Error, Result};
use crate::manifest::GlobalConfig;
use serde::Serialize;
use vitals_machines::probe::{PoolHealth, PoolObservation, ZfsPool, inspect_pool};
pub const REFUSE_BELOW_FREE_PERCENT: u8 = 5;
pub const WARN_BELOW_FREE_PERCENT: u8 = 15;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stance {
Refuse,
WarnOnly,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Thresholds {
pub refuse_below: u8,
pub warn_below: u8,
}
impl Thresholds {
pub fn of(config: &GlobalConfig) -> Self {
let refuse_below = config
.zpool_min_free_percent
.unwrap_or(REFUSE_BELOW_FREE_PERCENT);
Self {
refuse_below,
warn_below: WARN_BELOW_FREE_PERCENT.max(refuse_below),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "verdict", content = "detail", rename_all = "snake_case")]
pub enum Verdict {
Clear,
Warn(String),
Refuse(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Assessment {
pub pool: String,
pub observation: PoolObservation,
#[serde(flatten)]
pub verdict: Verdict,
pub thresholds: Thresholds,
}
fn configured_pool(config: &GlobalConfig) -> Option<&str> {
config
.zfs_enabled
.then_some(config.zpool.as_deref())
.flatten()
}
pub fn assess(config: &GlobalConfig) -> Option<Assessment> {
let pool = configured_pool(config)?;
let observation = inspect_pool(pool);
let thresholds = Thresholds::of(config);
Some(Assessment {
pool: pool.to_string(),
verdict: judge(pool, &observation, thresholds),
observation,
thresholds,
})
}
pub fn check(config: &GlobalConfig, stance: Stance) -> Result<()> {
assess(config).map_or(Ok(()), |assessment| settle(assessment.verdict, stance))
}
fn settle(verdict: Verdict, stance: Stance) -> Result<()> {
match (verdict, stance) {
(Verdict::Clear, _) => Ok(()),
(Verdict::Warn(message) | Verdict::Refuse(message), Stance::WarnOnly)
| (Verdict::Warn(message), Stance::Refuse) => {
eprintln!("Warning: {message}");
Ok(())
}
(Verdict::Refuse(message), Stance::Refuse) => Err(Error::StoragePreflight(message)),
}
}
fn judge(pool: &str, observation: &PoolObservation, thresholds: Thresholds) -> Verdict {
match observation {
PoolObservation::Found(found) => judge_pool(found, thresholds),
PoolObservation::NotFound => Verdict::Warn(format!(
"zpool '{pool}' is not imported; storage preflight skipped"
)),
PoolObservation::Unavailable(reason) => Verdict::Warn(format!(
"cannot inspect zpool '{pool}': {reason}; storage preflight skipped"
)),
}
}
fn judge_pool(pool: &ZfsPool, thresholds: Thresholds) -> Verdict {
let free = pool.available_percent();
let health = health_label(&pool.health);
let mut reasons = Vec::new();
if !pool.is_online() {
reasons.push("it is not ONLINE".to_string());
}
if pool.read_only {
reasons.push("it is read-only".to_string());
}
if free.is_some_and(|free| free < f64::from(thresholds.refuse_below)) {
reasons.push("free space is below the threshold".to_string());
}
let free_text = free.map_or_else(|| "unknown".to_string(), |free| format!("{free:.1}%"));
if !reasons.is_empty() {
return Verdict::Refuse(format!(
"zpool '{}' is unfit for new data: {} (health {health}, {}, {free_text} free, refuse below {}% free)",
pool.name,
reasons.join(" and "),
if pool.read_only {
"read-only"
} else {
"read-write"
},
thresholds.refuse_below,
));
}
if free.is_some_and(|free| free < f64::from(thresholds.warn_below)) {
return Verdict::Warn(format!(
"zpool '{}' is down to {free_text} free (warning below {}%, refusing below {}%)",
pool.name, thresholds.warn_below, thresholds.refuse_below,
));
}
Verdict::Clear
}
fn health_label(health: &PoolHealth) -> String {
match health {
PoolHealth::Online => "ONLINE".into(),
PoolHealth::Degraded => "DEGRADED".into(),
PoolHealth::Faulted => "FAULTED".into(),
PoolHealth::Offline => "OFFLINE".into(),
PoolHealth::Unavail => "UNAVAIL".into(),
PoolHealth::Removed => "REMOVED".into(),
PoolHealth::Suspended => "SUSPENDED".into(),
PoolHealth::Unrecognized(other) => other.clone(),
}
}
#[cfg(test)]
mod tests;