use crate::error::IronError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WatchdogType {
Hardware,
Software,
Unknown,
}
impl std::fmt::Display for WatchdogType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Hardware => write!(f, "hardware"),
Self::Software => write!(f, "software"),
Self::Unknown => write!(f, "unknown"),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct WatchdogStatus {
pub active: Option<bool>,
pub triggered: bool,
pub boot_triggered: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PreTimeoutGovernor {
Noop,
Panic,
Custom(String),
None,
}
impl std::fmt::Display for PreTimeoutGovernor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Noop => write!(f, "noop"),
Self::Panic => write!(f, "panic"),
Self::Custom(s) => write!(f, "{}", s),
Self::None => write!(f, "none"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchdogInfo {
pub name: String,
pub identity: String,
pub watchdog_type: WatchdogType,
pub timeout_secs: Option<u32>,
pub pretimeout_secs: Option<u32>,
pub pretimeout_governor: PreTimeoutGovernor,
pub min_timeout_secs: Option<u32>,
pub max_timeout_secs: Option<u32>,
pub firmware_version: Option<u32>,
pub status: WatchdogStatus,
pub available_governors: Vec<String>,
}
impl WatchdogInfo {
pub fn is_default_timeout(&self) -> Option<bool> {
self.timeout_secs.map(|t| t == 30 || t == 60)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchdogOverview {
pub devices: Vec<WatchdogInfo>,
pub total_count: u32,
pub active_count: u32,
pub hardware_count: u32,
pub recommendations: Vec<String>,
}
pub struct WatchdogMonitor {
overview: WatchdogOverview,
}
impl WatchdogMonitor {
pub fn new() -> Result<Self, IronError> {
let overview = Self::scan()?;
Ok(Self { overview })
}
pub fn refresh(&mut self) -> Result<(), IronError> {
self.overview = Self::scan()?;
Ok(())
}
pub fn overview(&self) -> &WatchdogOverview {
&self.overview
}
pub fn devices(&self) -> &[WatchdogInfo] {
&self.overview.devices
}
pub fn device(&self, name: &str) -> Option<&WatchdogInfo> {
self.overview.devices.iter().find(|d| d.name == name)
}
#[cfg(target_os = "linux")]
fn scan() -> Result<WatchdogOverview, IronError> {
let wdt_path = std::path::Path::new("/sys/class/watchdog");
if !wdt_path.exists() {
return Ok(Self::empty_overview());
}
let entries = std::fs::read_dir(wdt_path).map_err(IronError::Io)?;
let mut devices = Vec::new();
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name().to_string_lossy().to_string();
let identity =
Self::read_sysfs(&path.join("identity")).unwrap_or_else(|| "unknown".into());
let watchdog_type = if identity.contains("soft") || identity.contains("Soft") {
WatchdogType::Software
} else if identity != "unknown" {
WatchdogType::Hardware
} else {
WatchdogType::Unknown
};
let timeout_secs = Self::read_sysfs_u32(&path.join("timeout"));
let pretimeout_secs = Self::read_sysfs_u32(&path.join("pretimeout"));
let min_timeout_secs = Self::read_sysfs_u32(&path.join("min_timeout"));
let max_timeout_secs = Self::read_sysfs_u32(&path.join("max_timeout"));
let firmware_version = Self::read_sysfs_u32(&path.join("fw_version"));
let pretimeout_governor =
match Self::read_sysfs(&path.join("pretimeout_governor")).as_deref() {
Some("noop") => PreTimeoutGovernor::Noop,
Some("panic") => PreTimeoutGovernor::Panic,
Some("") | None => PreTimeoutGovernor::None,
Some(other) => PreTimeoutGovernor::Custom(other.to_string()),
};
let available_governors =
Self::read_sysfs(&path.join("pretimeout_available_governors"))
.map(|s| s.split_whitespace().map(String::from).collect())
.unwrap_or_default();
let active = Self::read_sysfs(&path.join("state")).map(|s| s == "active");
let boot_triggered = Self::read_sysfs_u32(&path.join("bootstatus")).map(|b| b != 0);
let status = WatchdogStatus {
active,
triggered: false,
boot_triggered,
};
devices.push(WatchdogInfo {
name,
identity,
watchdog_type,
timeout_secs,
pretimeout_secs,
pretimeout_governor,
min_timeout_secs,
max_timeout_secs,
firmware_version,
status,
available_governors,
});
}
devices.sort_by(|a, b| a.name.cmp(&b.name));
let total = devices.len() as u32;
let active = devices
.iter()
.filter(|d| d.status.active == Some(true))
.count() as u32;
let hw = devices
.iter()
.filter(|d| d.watchdog_type == WatchdogType::Hardware)
.count() as u32;
let mut recs = Vec::new();
if hw == 0 && total > 0 {
recs.push("No hardware watchdog detected; software watchdog only".into());
}
for dev in &devices {
match dev.status.boot_triggered {
Some(true) => recs.push(format!(
"{}: watchdog-triggered reboot detected in boot status",
dev.name
)),
Some(false) => {}
None => recs.push(format!(
concat!(
"{}: boot status could not be read, so a ",
"watchdog-triggered reboot can be neither ",
"confirmed nor ruled out"
),
dev.name
)),
}
}
Ok(WatchdogOverview {
devices,
total_count: total,
active_count: active,
hardware_count: hw,
recommendations: recs,
})
}
#[cfg(target_os = "linux")]
fn read_sysfs(path: &std::path::Path) -> Option<String> {
std::fs::read_to_string(path)
.ok()
.map(|s| s.trim().to_string())
}
#[cfg(target_os = "linux")]
fn read_sysfs_u32(path: &std::path::Path) -> Option<u32> {
std::fs::read_to_string(path)
.ok()
.and_then(|s| s.trim().parse().ok())
}
#[cfg(not(target_os = "linux"))]
fn scan() -> Result<WatchdogOverview, IronError> {
Err(IronError::UnsupportedPlatform(
"watchdog devices are read from `/sys/class/watchdog`, which this platform does not expose"
.into(),
))
}
fn empty_overview() -> WatchdogOverview {
WatchdogOverview {
devices: Vec::new(),
total_count: 0,
active_count: 0,
hardware_count: 0,
recommendations: Vec::new(),
}
}
}
impl Default for WatchdogMonitor {
fn default() -> Self {
Self::new().unwrap_or(Self {
overview: Self::empty_overview(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_type_display() {
assert_eq!(WatchdogType::Hardware.to_string(), "hardware");
assert_eq!(WatchdogType::Software.to_string(), "software");
}
#[test]
fn test_governor_display() {
assert_eq!(PreTimeoutGovernor::Panic.to_string(), "panic");
assert_eq!(PreTimeoutGovernor::Noop.to_string(), "noop");
assert_eq!(PreTimeoutGovernor::None.to_string(), "none");
}
#[test]
fn test_default_timeout() {
let info = WatchdogInfo {
name: "watchdog0".into(),
identity: "iTCO_wdt".into(),
watchdog_type: WatchdogType::Hardware,
timeout_secs: Some(30),
pretimeout_secs: Some(0),
pretimeout_governor: PreTimeoutGovernor::None,
min_timeout_secs: Some(2),
max_timeout_secs: Some(614),
firmware_version: Some(0),
status: WatchdogStatus {
active: Some(false),
triggered: false,
boot_triggered: Some(false),
},
available_governors: Vec::new(),
};
assert_eq!(info.is_default_timeout(), Some(true));
let unread = WatchdogInfo {
timeout_secs: None,
..info
};
assert_eq!(unread.is_default_timeout(), None);
}
#[test]
fn test_monitor_default() {
let monitor = WatchdogMonitor::default();
let _overview = monitor.overview();
}
#[test]
fn test_serialization() {
let info = WatchdogInfo {
name: "watchdog0".into(),
identity: "softdog".into(),
watchdog_type: WatchdogType::Software,
timeout_secs: Some(60),
pretimeout_secs: Some(10),
pretimeout_governor: PreTimeoutGovernor::Panic,
min_timeout_secs: Some(1),
max_timeout_secs: Some(65535),
firmware_version: Some(0),
status: WatchdogStatus {
active: Some(true),
triggered: false,
boot_triggered: Some(false),
},
available_governors: vec!["noop".into(), "panic".into()],
};
let json = serde_json::to_string(&info).unwrap();
assert!(json.contains("softdog"));
let _: WatchdogInfo = serde_json::from_str(&json).unwrap();
}
}