Implement WakeOnLAN functionality with simplified configuration
All checks were successful
Build and Release / build-and-release (push) Successful in 2m32s
All checks were successful
Build and Release / build-and-release (push) Successful in 2m32s
- Add Status::Offline enum variant for disconnected hosts
- All configured hosts now always visible showing offline status when disconnected
- Add WakeOnLAN support using wake-on-lan Rust crate
- Implement w key binding to wake offline hosts with MAC addresses
- Simplify configuration to single [hosts] section with MAC addresses only
- Change critical status icon from ◯ to ! for better visibility
- Add proper MAC address parsing and error handling
- Silent WakeOnLAN operation with logging for success/failure
Configuration format:
[hosts]
hostname = { mac_address = "AA:BB:CC:DD:EE:FF" }
This commit is contained in:
parent
57de4c366a
commit
6179bd51a7
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -270,7 +270,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.52"
|
version = "0.1.54"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@ -286,12 +286,13 @@ dependencies = [
|
|||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"wake-on-lan",
|
||||||
"zmq",
|
"zmq",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.52"
|
version = "0.1.54"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@ -314,7 +315,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.52"
|
version = "0.1.54"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
@ -2064,6 +2065,12 @@ version = "0.9.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wake-on-lan"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ccf60b60ad7e5b1b37372c5134cbcab4db0706c231d212e0c643a077462bc8f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "walkdir"
|
name = "walkdir"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-agent"
|
name = "cm-dashboard-agent"
|
||||||
version = "0.1.53"
|
version = "0.1.54"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -140,6 +140,7 @@ impl Collector for BackupCollector {
|
|||||||
Status::Warning => "warning".to_string(),
|
Status::Warning => "warning".to_string(),
|
||||||
Status::Critical => "critical".to_string(),
|
Status::Critical => "critical".to_string(),
|
||||||
Status::Unknown => "unknown".to_string(),
|
Status::Unknown => "unknown".to_string(),
|
||||||
|
Status::Offline => "offline".to_string(),
|
||||||
}),
|
}),
|
||||||
status: overall_status,
|
status: overall_status,
|
||||||
timestamp,
|
timestamp,
|
||||||
@ -202,6 +203,7 @@ impl Collector for BackupCollector {
|
|||||||
Status::Warning => "warning".to_string(),
|
Status::Warning => "warning".to_string(),
|
||||||
Status::Critical => "critical".to_string(),
|
Status::Critical => "critical".to_string(),
|
||||||
Status::Unknown => "unknown".to_string(),
|
Status::Unknown => "unknown".to_string(),
|
||||||
|
Status::Offline => "offline".to_string(),
|
||||||
}),
|
}),
|
||||||
status: service_status,
|
status: service_status,
|
||||||
timestamp,
|
timestamp,
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard"
|
name = "cm-dashboard"
|
||||||
version = "0.1.53"
|
version = "0.1.54"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -18,4 +18,5 @@ tracing-subscriber = { workspace = true }
|
|||||||
ratatui = { workspace = true }
|
ratatui = { workspace = true }
|
||||||
crossterm = { workspace = true }
|
crossterm = { workspace = true }
|
||||||
toml = { workspace = true }
|
toml = { workspace = true }
|
||||||
gethostname = { workspace = true }
|
gethostname = { workspace = true }
|
||||||
|
wake-on-lan = "0.2"
|
||||||
@ -67,8 +67,8 @@ impl Dashboard {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Connect to predefined hosts from configuration
|
// Connect to configured hosts from configuration
|
||||||
let hosts = config.hosts.predefined_hosts.clone();
|
let hosts: Vec<String> = config.hosts.keys().cloned().collect();
|
||||||
|
|
||||||
// Try to connect to hosts but don't fail if none are available
|
// Try to connect to hosts but don't fail if none are available
|
||||||
match zmq_consumer.connect_to_predefined_hosts(&hosts).await {
|
match zmq_consumer.connect_to_predefined_hosts(&hosts).await {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use std::path::Path;
|
|||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DashboardConfig {
|
pub struct DashboardConfig {
|
||||||
pub zmq: ZmqConfig,
|
pub zmq: ZmqConfig,
|
||||||
pub hosts: HostsConfig,
|
pub hosts: std::collections::HashMap<String, HostDetails>,
|
||||||
pub system: SystemConfig,
|
pub system: SystemConfig,
|
||||||
pub ssh: SshConfig,
|
pub ssh: SshConfig,
|
||||||
pub service_logs: std::collections::HashMap<String, Vec<ServiceLogConfig>>,
|
pub service_logs: std::collections::HashMap<String, Vec<ServiceLogConfig>>,
|
||||||
@ -18,10 +18,10 @@ pub struct ZmqConfig {
|
|||||||
pub subscriber_ports: Vec<u16>,
|
pub subscriber_ports: Vec<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hosts configuration
|
/// Individual host configuration details
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct HostsConfig {
|
pub struct HostDetails {
|
||||||
pub predefined_hosts: Vec<String>,
|
pub mac_address: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// System configuration
|
/// System configuration
|
||||||
@ -68,8 +68,3 @@ impl Default for ZmqConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for HostsConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
panic!("Dashboard configuration must be loaded from file - no hardcoded defaults allowed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ use ratatui::{
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
use wake_on_lan::MagicPacket;
|
||||||
|
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
pub mod widgets;
|
pub mod widgets;
|
||||||
@ -93,15 +94,25 @@ pub struct TuiApp {
|
|||||||
|
|
||||||
impl TuiApp {
|
impl TuiApp {
|
||||||
pub fn new(config: DashboardConfig) -> Self {
|
pub fn new(config: DashboardConfig) -> Self {
|
||||||
Self {
|
let mut app = Self {
|
||||||
host_widgets: HashMap::new(),
|
host_widgets: HashMap::new(),
|
||||||
current_host: None,
|
current_host: None,
|
||||||
available_hosts: Vec::new(),
|
available_hosts: config.hosts.keys().cloned().collect(),
|
||||||
host_index: 0,
|
host_index: 0,
|
||||||
should_quit: false,
|
should_quit: false,
|
||||||
user_navigated_away: false,
|
user_navigated_away: false,
|
||||||
config,
|
config,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sort predefined hosts
|
||||||
|
app.available_hosts.sort();
|
||||||
|
|
||||||
|
// Initialize with first host if available
|
||||||
|
if !app.available_hosts.is_empty() {
|
||||||
|
app.current_host = Some(app.available_hosts[0].clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get or create host widgets for the given hostname
|
/// Get or create host widgets for the given hostname
|
||||||
@ -186,21 +197,28 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Update available hosts with localhost prioritization
|
/// Update available hosts with localhost prioritization
|
||||||
pub fn update_hosts(&mut self, hosts: Vec<String>) {
|
pub fn update_hosts(&mut self, discovered_hosts: Vec<String>) {
|
||||||
// Sort hosts alphabetically
|
// Start with configured hosts (always visible)
|
||||||
let mut sorted_hosts = hosts.clone();
|
let mut all_hosts: Vec<String> = self.config.hosts.keys().cloned().collect();
|
||||||
|
|
||||||
|
// Add any discovered hosts that aren't already configured
|
||||||
|
for host in discovered_hosts {
|
||||||
|
if !all_hosts.contains(&host) {
|
||||||
|
all_hosts.push(host);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Keep hosts that have pending transitions even if they're offline
|
// Keep hosts that have pending transitions even if they're offline
|
||||||
for (hostname, host_widgets) in &self.host_widgets {
|
for (hostname, host_widgets) in &self.host_widgets {
|
||||||
if !host_widgets.pending_service_transitions.is_empty() {
|
if !host_widgets.pending_service_transitions.is_empty() {
|
||||||
if !sorted_hosts.contains(hostname) {
|
if !all_hosts.contains(hostname) {
|
||||||
sorted_hosts.push(hostname.clone());
|
all_hosts.push(hostname.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sorted_hosts.sort();
|
all_hosts.sort();
|
||||||
self.available_hosts = sorted_hosts;
|
self.available_hosts = all_hosts;
|
||||||
|
|
||||||
// Get the current hostname (localhost) for auto-selection
|
// Get the current hostname (localhost) for auto-selection
|
||||||
let localhost = gethostname::gethostname().to_string_lossy().to_string();
|
let localhost = gethostname::gethostname().to_string_lossy().to_string();
|
||||||
@ -331,6 +349,33 @@ impl TuiApp {
|
|||||||
return Ok(Some(UiCommand::TriggerBackup { hostname }));
|
return Ok(Some(UiCommand::TriggerBackup { hostname }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('w') => {
|
||||||
|
// Wake on LAN for offline hosts
|
||||||
|
if let Some(hostname) = self.current_host.clone() {
|
||||||
|
// Check if host has MAC address configured
|
||||||
|
if let Some(host_details) = self.config.hosts.get(&hostname) {
|
||||||
|
if let Some(mac_address) = &host_details.mac_address {
|
||||||
|
// Parse MAC address and send WoL packet
|
||||||
|
let mac_bytes = Self::parse_mac_address(mac_address);
|
||||||
|
match mac_bytes {
|
||||||
|
Ok(mac) => {
|
||||||
|
match MagicPacket::new(&mac).send() {
|
||||||
|
Ok(_) => {
|
||||||
|
info!("WakeOnLAN packet sent successfully to {} ({})", hostname, mac_address);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to send WakeOnLAN packet to {}: {}", hostname, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
tracing::error!("Invalid MAC address format for {}: {}", hostname, mac_address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
KeyCode::Tab => {
|
KeyCode::Tab => {
|
||||||
// Tab cycles to next host
|
// Tab cycles to next host
|
||||||
self.navigate_host(1);
|
self.navigate_host(1);
|
||||||
@ -674,7 +719,7 @@ impl TuiApp {
|
|||||||
let metrics = metric_store.get_metrics_for_host(hostname);
|
let metrics = metric_store.get_metrics_for_host(hostname);
|
||||||
|
|
||||||
if metrics.is_empty() {
|
if metrics.is_empty() {
|
||||||
return Status::Unknown;
|
return Status::Offline;
|
||||||
}
|
}
|
||||||
|
|
||||||
// First check if we have the aggregated host status summary from the agent
|
// First check if we have the aggregated host status summary from the agent
|
||||||
@ -694,7 +739,8 @@ impl TuiApp {
|
|||||||
Status::Warning => has_warning = true,
|
Status::Warning => has_warning = true,
|
||||||
Status::Pending => has_pending = true,
|
Status::Pending => has_pending = true,
|
||||||
Status::Ok => ok_count += 1,
|
Status::Ok => ok_count += 1,
|
||||||
Status::Unknown => {} // Ignore unknown for aggregation
|
Status::Unknown => {}, // Ignore unknown for aggregation
|
||||||
|
Status::Offline => {}, // Ignore offline for aggregation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -735,6 +781,7 @@ impl TuiApp {
|
|||||||
shortcuts.push("s/S: Start/Stop".to_string());
|
shortcuts.push("s/S: Start/Stop".to_string());
|
||||||
shortcuts.push("J: Logs".to_string());
|
shortcuts.push("J: Logs".to_string());
|
||||||
shortcuts.push("L: Custom".to_string());
|
shortcuts.push("L: Custom".to_string());
|
||||||
|
shortcuts.push("w: Wake".to_string());
|
||||||
|
|
||||||
// Always show quit
|
// Always show quit
|
||||||
shortcuts.push("q: Quit".to_string());
|
shortcuts.push("q: Quit".to_string());
|
||||||
@ -773,5 +820,20 @@ impl TuiApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse MAC address string (e.g., "AA:BB:CC:DD:EE:FF") to [u8; 6]
|
||||||
|
fn parse_mac_address(mac_str: &str) -> Result<[u8; 6], &'static str> {
|
||||||
|
let parts: Vec<&str> = mac_str.split(':').collect();
|
||||||
|
if parts.len() != 6 {
|
||||||
|
return Err("MAC address must have 6 parts separated by colons");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut mac = [0u8; 6];
|
||||||
|
for (i, part) in parts.iter().enumerate() {
|
||||||
|
match u8::from_str_radix(part, 16) {
|
||||||
|
Ok(byte) => mac[i] = byte,
|
||||||
|
Err(_) => return Err("Invalid hexadecimal byte in MAC address"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(mac)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -147,6 +147,7 @@ impl Theme {
|
|||||||
Status::Warning => Self::warning(),
|
Status::Warning => Self::warning(),
|
||||||
Status::Critical => Self::error(),
|
Status::Critical => Self::error(),
|
||||||
Status::Unknown => Self::muted_text(),
|
Status::Unknown => Self::muted_text(),
|
||||||
|
Status::Offline => Self::muted_text(), // Dark gray for offline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,8 +245,9 @@ impl StatusIcons {
|
|||||||
Status::Ok => "●",
|
Status::Ok => "●",
|
||||||
Status::Pending => "◉", // Hollow circle for pending
|
Status::Pending => "◉", // Hollow circle for pending
|
||||||
Status::Warning => "◐",
|
Status::Warning => "◐",
|
||||||
Status::Critical => "◯",
|
Status::Critical => "!",
|
||||||
Status::Unknown => "?",
|
Status::Unknown => "?",
|
||||||
|
Status::Offline => "○", // Empty circle for offline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,6 +260,7 @@ impl StatusIcons {
|
|||||||
Status::Warning => Theme::warning(), // Yellow
|
Status::Warning => Theme::warning(), // Yellow
|
||||||
Status::Critical => Theme::error(), // Red
|
Status::Critical => Theme::error(), // Red
|
||||||
Status::Unknown => Theme::muted_text(), // Gray
|
Status::Unknown => Theme::muted_text(), // Gray
|
||||||
|
Status::Offline => Theme::muted_text(), // Dark gray for offline
|
||||||
};
|
};
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
|
|||||||
@ -146,6 +146,7 @@ impl ServicesWidget {
|
|||||||
Status::Warning => Theme::warning(),
|
Status::Warning => Theme::warning(),
|
||||||
Status::Critical => Theme::error(),
|
Status::Critical => Theme::error(),
|
||||||
Status::Unknown => Theme::muted_text(),
|
Status::Unknown => Theme::muted_text(),
|
||||||
|
Status::Offline => Theme::muted_text(),
|
||||||
};
|
};
|
||||||
|
|
||||||
(icon.to_string(), info.status.clone(), status_color)
|
(icon.to_string(), info.status.clone(), status_color)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cm-dashboard-shared"
|
name = "cm-dashboard-shared"
|
||||||
version = "0.1.53"
|
version = "0.1.54"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -87,6 +87,7 @@ pub enum Status {
|
|||||||
Warning,
|
Warning,
|
||||||
Critical,
|
Critical,
|
||||||
Unknown,
|
Unknown,
|
||||||
|
Offline,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Status {
|
impl Status {
|
||||||
@ -190,6 +191,16 @@ impl HysteresisThresholds {
|
|||||||
Status::Ok
|
Status::Ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Status::Offline => {
|
||||||
|
// Host coming back online, use normal thresholds like first measurement
|
||||||
|
if value >= self.critical_high {
|
||||||
|
Status::Critical
|
||||||
|
} else if value >= self.warning_high {
|
||||||
|
Status::Warning
|
||||||
|
} else {
|
||||||
|
Status::Ok
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user