Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad1fcaa27b | |||
| 60ab4d4f9e | |||
| 67034c84b9 | |||
| c62c7fa698 |
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.224"
|
||||
version = "0.1.229"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
@@ -301,7 +301,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.224"
|
||||
version = "0.1.229"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -325,7 +325,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.224"
|
||||
version = "0.1.229"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"serde",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.225"
|
||||
version = "0.1.229"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use gethostname::gethostname;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::time::interval;
|
||||
use tracing::{debug, error, info};
|
||||
|
||||
@@ -19,11 +19,19 @@ use crate::collectors::{
|
||||
use crate::notifications::NotificationManager;
|
||||
use cm_dashboard_shared::AgentData;
|
||||
|
||||
/// Wrapper for collectors with timing information
|
||||
struct TimedCollector {
|
||||
collector: Box<dyn Collector>,
|
||||
interval: Duration,
|
||||
last_collection: Option<Instant>,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub struct Agent {
|
||||
hostname: String,
|
||||
config: AgentConfig,
|
||||
zmq_handler: ZmqHandler,
|
||||
collectors: Vec<Box<dyn Collector>>,
|
||||
collectors: Vec<TimedCollector>,
|
||||
notification_manager: NotificationManager,
|
||||
previous_status: Option<SystemStatus>,
|
||||
}
|
||||
@@ -55,36 +63,78 @@ impl Agent {
|
||||
config.zmq.publisher_port
|
||||
);
|
||||
|
||||
// Initialize collectors
|
||||
let mut collectors: Vec<Box<dyn Collector>> = Vec::new();
|
||||
|
||||
// Initialize collectors with timing information
|
||||
let mut collectors: Vec<TimedCollector> = Vec::new();
|
||||
|
||||
// Add enabled collectors
|
||||
if config.collectors.cpu.enabled {
|
||||
collectors.push(Box::new(CpuCollector::new(config.collectors.cpu.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(CpuCollector::new(config.collectors.cpu.clone())),
|
||||
interval: Duration::from_secs(config.collectors.cpu.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "CPU".to_string(),
|
||||
});
|
||||
info!("CPU collector initialized with {}s interval", config.collectors.cpu.interval_seconds);
|
||||
}
|
||||
|
||||
|
||||
if config.collectors.memory.enabled {
|
||||
collectors.push(Box::new(MemoryCollector::new(config.collectors.memory.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(MemoryCollector::new(config.collectors.memory.clone())),
|
||||
interval: Duration::from_secs(config.collectors.memory.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "Memory".to_string(),
|
||||
});
|
||||
info!("Memory collector initialized with {}s interval", config.collectors.memory.interval_seconds);
|
||||
}
|
||||
|
||||
|
||||
if config.collectors.disk.enabled {
|
||||
collectors.push(Box::new(DiskCollector::new(config.collectors.disk.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(DiskCollector::new(config.collectors.disk.clone())),
|
||||
interval: Duration::from_secs(config.collectors.disk.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "Disk".to_string(),
|
||||
});
|
||||
info!("Disk collector initialized with {}s interval", config.collectors.disk.interval_seconds);
|
||||
}
|
||||
|
||||
|
||||
if config.collectors.systemd.enabled {
|
||||
collectors.push(Box::new(SystemdCollector::new(config.collectors.systemd.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(SystemdCollector::new(config.collectors.systemd.clone())),
|
||||
interval: Duration::from_secs(config.collectors.systemd.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "Systemd".to_string(),
|
||||
});
|
||||
info!("Systemd collector initialized with {}s interval", config.collectors.systemd.interval_seconds);
|
||||
}
|
||||
|
||||
|
||||
if config.collectors.backup.enabled {
|
||||
collectors.push(Box::new(BackupCollector::new()));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(BackupCollector::new()),
|
||||
interval: Duration::from_secs(config.collectors.backup.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "Backup".to_string(),
|
||||
});
|
||||
info!("Backup collector initialized with {}s interval", config.collectors.backup.interval_seconds);
|
||||
}
|
||||
|
||||
if config.collectors.network.enabled {
|
||||
collectors.push(Box::new(NetworkCollector::new(config.collectors.network.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(NetworkCollector::new(config.collectors.network.clone())),
|
||||
interval: Duration::from_secs(config.collectors.network.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "Network".to_string(),
|
||||
});
|
||||
info!("Network collector initialized with {}s interval", config.collectors.network.interval_seconds);
|
||||
}
|
||||
|
||||
if config.collectors.nixos.enabled {
|
||||
collectors.push(Box::new(NixOSCollector::new(config.collectors.nixos.clone())));
|
||||
collectors.push(TimedCollector {
|
||||
collector: Box::new(NixOSCollector::new(config.collectors.nixos.clone())),
|
||||
interval: Duration::from_secs(config.collectors.nixos.interval_seconds),
|
||||
last_collection: None,
|
||||
name: "NixOS".to_string(),
|
||||
});
|
||||
info!("NixOS collector initialized with {}s interval", config.collectors.nixos.interval_seconds);
|
||||
}
|
||||
|
||||
info!("Initialized {} collectors", collectors.len());
|
||||
@@ -152,11 +202,27 @@ impl Agent {
|
||||
// Initialize empty AgentData
|
||||
let mut agent_data = AgentData::new(self.hostname.clone(), env!("CARGO_PKG_VERSION").to_string());
|
||||
|
||||
// Collect data from all collectors
|
||||
for collector in &self.collectors {
|
||||
if let Err(e) = collector.collect_structured(&mut agent_data).await {
|
||||
error!("Collector failed: {}", e);
|
||||
// Continue with other collectors even if one fails
|
||||
// Collect data from collectors whose intervals have elapsed
|
||||
let now = Instant::now();
|
||||
for timed_collector in &mut self.collectors {
|
||||
let should_collect = match timed_collector.last_collection {
|
||||
None => true, // First collection
|
||||
Some(last_time) => now.duration_since(last_time) >= timed_collector.interval,
|
||||
};
|
||||
|
||||
if should_collect {
|
||||
if let Err(e) = timed_collector.collector.collect_structured(&mut agent_data).await {
|
||||
error!("Collector {} failed: {}", timed_collector.name, e);
|
||||
// Update last_collection time even on failure to prevent immediate retries
|
||||
timed_collector.last_collection = Some(now);
|
||||
} else {
|
||||
timed_collector.last_collection = Some(now);
|
||||
debug!(
|
||||
"Collected from {} ({}s interval)",
|
||||
timed_collector.name,
|
||||
timed_collector.interval.as_secs()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,7 @@ use cm_dashboard_shared::{AgentData, DriveData, FilesystemData, PoolData, Hyster
|
||||
use crate::config::DiskConfig;
|
||||
use tokio::process::Command as TokioCommand;
|
||||
use std::process::Command as StdCommand;
|
||||
use std::time::Instant;
|
||||
use std::collections::HashMap;
|
||||
use tracing::debug;
|
||||
|
||||
use super::{Collector, CollectorError};
|
||||
|
||||
@@ -68,9 +66,6 @@ impl DiskCollector {
|
||||
|
||||
/// Collect all storage data and populate AgentData
|
||||
async fn collect_storage_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||
let start_time = Instant::now();
|
||||
debug!("Starting clean storage collection");
|
||||
|
||||
// Step 1: Get mount points and their backing devices
|
||||
let mount_devices = self.get_mount_devices().await?;
|
||||
|
||||
@@ -105,9 +100,6 @@ impl DiskCollector {
|
||||
self.populate_drives_data(&physical_drives, &smart_data, agent_data)?;
|
||||
self.populate_pools_data(&mergerfs_pools, &smart_data, agent_data)?;
|
||||
|
||||
let elapsed = start_time.elapsed();
|
||||
debug!("Storage collection completed in {:?}", elapsed);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -142,7 +134,6 @@ impl DiskCollector {
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Found {} mounted block devices", mount_devices.len());
|
||||
Ok(mount_devices)
|
||||
}
|
||||
|
||||
@@ -155,8 +146,8 @@ impl DiskCollector {
|
||||
Ok((total, used)) => {
|
||||
filesystem_usage.insert(mount_point.clone(), (total, used));
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to get filesystem info for {}: {}", mount_point, e);
|
||||
Err(_e) => {
|
||||
// Silently skip filesystems we can't read
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,8 +168,6 @@ impl DiskCollector {
|
||||
// Only add if we don't already have usage data for this mount point
|
||||
if !filesystem_usage.contains_key(&mount_point) {
|
||||
if let Ok((total, used)) = self.get_filesystem_info(&mount_point) {
|
||||
debug!("Added MergerFS filesystem usage for {}: {}GB total, {}GB used",
|
||||
mount_point, total as f32 / (1024.0 * 1024.0 * 1024.0), used as f32 / (1024.0 * 1024.0 * 1024.0));
|
||||
filesystem_usage.insert(mount_point, (total, used));
|
||||
}
|
||||
}
|
||||
@@ -253,9 +242,8 @@ impl DiskCollector {
|
||||
} else {
|
||||
mount_point.trim_start_matches('/').replace('/', "_")
|
||||
};
|
||||
|
||||
|
||||
if pool_name.is_empty() {
|
||||
debug!("Skipping mergerfs pool with empty name: {}", mount_point);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -283,8 +271,7 @@ impl DiskCollector {
|
||||
// Categorize as data vs parity drives
|
||||
let (data_drives, parity_drives) = match self.categorize_pool_drives(&all_member_paths) {
|
||||
Ok(drives) => drives,
|
||||
Err(e) => {
|
||||
debug!("Failed to categorize drives for pool {}: {}. Skipping.", mount_point, e);
|
||||
Err(_e) => {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -299,8 +286,7 @@ impl DiskCollector {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Found {} mergerfs pools", pools.len());
|
||||
|
||||
Ok(pools)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.225"
|
||||
version = "0.1.229"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -11,6 +11,74 @@ use tracing::debug;
|
||||
use crate::ui::theme::{Components, StatusIcons, Theme, Typography};
|
||||
use ratatui::style::Style;
|
||||
|
||||
/// Column visibility configuration based on terminal width
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct ColumnVisibility {
|
||||
show_name: bool,
|
||||
show_status: bool,
|
||||
show_ram: bool,
|
||||
show_uptime: bool,
|
||||
show_restarts: bool,
|
||||
}
|
||||
|
||||
impl ColumnVisibility {
|
||||
/// Calculate actual width needed for all columns
|
||||
const NAME_WIDTH: u16 = 23;
|
||||
const STATUS_WIDTH: u16 = 10;
|
||||
const RAM_WIDTH: u16 = 8;
|
||||
const UPTIME_WIDTH: u16 = 8;
|
||||
const RESTARTS_WIDTH: u16 = 5;
|
||||
const COLUMN_SPACING: u16 = 1; // Space between columns
|
||||
|
||||
/// Determine which columns to show based on available width
|
||||
/// Priority order: Name > Status > RAM > Uptime > Restarts
|
||||
fn from_width(width: u16) -> Self {
|
||||
// Calculate cumulative widths for each configuration
|
||||
let minimal = Self::NAME_WIDTH + Self::COLUMN_SPACING + Self::STATUS_WIDTH; // 34
|
||||
let with_ram = minimal + Self::COLUMN_SPACING + Self::RAM_WIDTH; // 43
|
||||
let with_uptime = with_ram + Self::COLUMN_SPACING + Self::UPTIME_WIDTH; // 52
|
||||
let full = with_uptime + Self::COLUMN_SPACING + Self::RESTARTS_WIDTH; // 58
|
||||
|
||||
if width >= full {
|
||||
// Show all columns
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: true,
|
||||
show_restarts: true,
|
||||
}
|
||||
} else if width >= with_uptime {
|
||||
// Hide restarts
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: true,
|
||||
show_restarts: false,
|
||||
}
|
||||
} else if width >= with_ram {
|
||||
// Hide uptime and restarts
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: false,
|
||||
show_restarts: false,
|
||||
}
|
||||
} else {
|
||||
// Minimal: Name + Status only
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: false,
|
||||
show_uptime: false,
|
||||
show_restarts: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Services widget displaying hierarchical systemd service statuses
|
||||
#[derive(Clone)]
|
||||
pub struct ServicesWidget {
|
||||
@@ -76,10 +144,12 @@ impl ServicesWidget {
|
||||
}
|
||||
|
||||
/// Format parent service line - returns text without icon for span formatting
|
||||
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo) -> String {
|
||||
// Truncate long service names to fit layout (account for icon space)
|
||||
let short_name = if name.len() > 22 {
|
||||
format!("{}...", &name[..19])
|
||||
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo, columns: ColumnVisibility) -> String {
|
||||
// Truncate long service names to fit layout
|
||||
// NAME_WIDTH - 3 chars for "..." = max displayable chars
|
||||
let max_name_len = (ColumnVisibility::NAME_WIDTH - 3) as usize;
|
||||
let short_name = if name.len() > max_name_len {
|
||||
format!("{}...", &name[..max_name_len.saturating_sub(3)])
|
||||
} else {
|
||||
name.to_string()
|
||||
};
|
||||
@@ -129,10 +199,25 @@ impl ServicesWidget {
|
||||
}
|
||||
});
|
||||
|
||||
format!(
|
||||
"{:<23} {:<10} {:<8} {:<8} {:<5}",
|
||||
short_name, status_str, memory_str, uptime_str, restart_str
|
||||
)
|
||||
// Build format string based on column visibility
|
||||
let mut parts = Vec::new();
|
||||
if columns.show_name {
|
||||
parts.push(format!("{:<width$}", short_name, width = ColumnVisibility::NAME_WIDTH as usize));
|
||||
}
|
||||
if columns.show_status {
|
||||
parts.push(format!("{:<width$}", status_str, width = ColumnVisibility::STATUS_WIDTH as usize));
|
||||
}
|
||||
if columns.show_ram {
|
||||
parts.push(format!("{:<width$}", memory_str, width = ColumnVisibility::RAM_WIDTH as usize));
|
||||
}
|
||||
if columns.show_uptime {
|
||||
parts.push(format!("{:<width$}", uptime_str, width = ColumnVisibility::UPTIME_WIDTH as usize));
|
||||
}
|
||||
if columns.show_restarts {
|
||||
parts.push(format!("{:<width$}", restart_str, width = ColumnVisibility::RESTARTS_WIDTH as usize));
|
||||
}
|
||||
|
||||
parts.join(" ")
|
||||
}
|
||||
|
||||
|
||||
@@ -476,11 +561,28 @@ impl ServicesWidget {
|
||||
.constraints([Constraint::Length(1), Constraint::Min(0)])
|
||||
.split(inner_area);
|
||||
|
||||
// Header
|
||||
let header = format!(
|
||||
"{:<25} {:<10} {:<8} {:<8} {:<5}",
|
||||
"Service:", "Status:", "RAM:", "Uptime:", "↻:"
|
||||
);
|
||||
// Determine which columns to show based on available width
|
||||
let columns = ColumnVisibility::from_width(inner_area.width);
|
||||
|
||||
// Build header based on visible columns
|
||||
let mut header_parts = Vec::new();
|
||||
if columns.show_name {
|
||||
header_parts.push(format!("{:<width$}", "Service:", width = ColumnVisibility::NAME_WIDTH as usize));
|
||||
}
|
||||
if columns.show_status {
|
||||
header_parts.push(format!("{:<width$}", "Status:", width = ColumnVisibility::STATUS_WIDTH as usize));
|
||||
}
|
||||
if columns.show_ram {
|
||||
header_parts.push(format!("{:<width$}", "RAM:", width = ColumnVisibility::RAM_WIDTH as usize));
|
||||
}
|
||||
if columns.show_uptime {
|
||||
header_parts.push(format!("{:<width$}", "Uptime:", width = ColumnVisibility::UPTIME_WIDTH as usize));
|
||||
}
|
||||
if columns.show_restarts {
|
||||
header_parts.push(format!("{:<width$}", "↻:", width = ColumnVisibility::RESTARTS_WIDTH as usize));
|
||||
}
|
||||
let header = header_parts.join(" ");
|
||||
|
||||
let header_para = Paragraph::new(header).style(Typography::muted());
|
||||
frame.render_widget(header_para, content_chunks[0]);
|
||||
|
||||
@@ -492,11 +594,11 @@ impl ServicesWidget {
|
||||
}
|
||||
|
||||
// Render the services list
|
||||
self.render_services(frame, content_chunks[1], is_focused);
|
||||
self.render_services(frame, content_chunks[1], is_focused, columns);
|
||||
}
|
||||
|
||||
/// Render services list
|
||||
fn render_services(&mut self, frame: &mut Frame, area: Rect, is_focused: bool) {
|
||||
fn render_services(&mut self, frame: &mut Frame, area: Rect, is_focused: bool, columns: ColumnVisibility) {
|
||||
// Build hierarchical service list for display
|
||||
let mut display_lines: Vec<(String, Status, bool, Option<(ServiceInfo, bool)>)> = Vec::new();
|
||||
|
||||
@@ -506,7 +608,7 @@ impl ServicesWidget {
|
||||
|
||||
for (parent_name, parent_info) in parent_services {
|
||||
// Add parent service line
|
||||
let parent_line = self.format_parent_service_line(parent_name, parent_info);
|
||||
let parent_line = self.format_parent_service_line(parent_name, parent_info, columns);
|
||||
display_lines.push((parent_line, parent_info.widget_status, false, None));
|
||||
|
||||
// Add sub-services for this parent (if any)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.225"
|
||||
version = "0.1.229"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
Reference in New Issue
Block a user