Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 67034c84b9 | |||
| c62c7fa698 | |||
| 0b1d8c0a73 | |||
| c77aa6eaaa | |||
| 8a0e68f0e3 | |||
| 2d653fe9ae | |||
| caba78004e | |||
| 77bf08a978 | |||
| 929870f8b6 | |||
| 7aae852b7b | |||
| 40f3ff66d8 | |||
| 1c1beddb55 | |||
| 620d1f10b6 | |||
| a0d571a40e | |||
| 977200fff3 | |||
| d692de5f83 | |||
| f5913dbd43 | |||
| faa30a7839 | |||
| 6e4a42799f | |||
| afb8d68e03 | |||
| 5e08b34280 | |||
| 0d8284b69c | |||
| d84690cb3b | |||
| 7c030b33d6 | |||
| c6817537a8 | |||
| 2189d34b16 | |||
| 28cfd5758f | |||
| 5deb8cf8d8 | |||
| 0e01813ff5 | |||
| c3c9507a42 | |||
| 4d77ffe17e | |||
| 14f74b4cac | |||
| 67b686f8c7 | |||
| e3996fdb84 | |||
| f94ca60e69 | |||
| c19ff56df8 | |||
| fe2f604703 | |||
| 8bfd416327 | |||
| 85c6c624fb | |||
| eab3f17428 | |||
| 7ad149bbe4 | |||
| b444c88ea0 | |||
| 317cf76bd1 | |||
| 0db1a165b9 | |||
| 3c2955376d | |||
| f09ccabc7f | |||
| 43dd5a901a | |||
| 01e1f33b66 | |||
| ed6399b914 | |||
| 14618c59c6 | |||
| 2740de9b54 | |||
| 37f2650200 | |||
| 833010e270 | |||
| 549d9d1c72 | |||
| 9b84b70581 | |||
| 92c3ee3f2a | |||
| 1be55f765d | |||
| 2f94a4b853 |
11
CLAUDE.md
11
CLAUDE.md
@@ -327,9 +327,16 @@ Storage:
|
||||
├─ ● Data_2: GGA04461 T: 28°C
|
||||
└─ ● Parity: WDZS8RY0 T: 29°C
|
||||
Backup:
|
||||
● Repo: 4
|
||||
├─ getea
|
||||
├─ vaultwarden
|
||||
├─ mysql
|
||||
└─ immich
|
||||
● W800639Y W: 2%
|
||||
├─ ● Backup: 2025-11-29T04:00:01.324623
|
||||
└─ ● Usage: 8% 70GB/916GB
|
||||
● WD-WCC7K1234567 T: 32°C W: 12%
|
||||
├─ Last: 2h ago (12.3GB)
|
||||
├─ Next: in 22h
|
||||
├─ ● Backup: 2025-11-29T04:00:01.324623
|
||||
└─ ● Usage: 45% 678GB/1.5TB
|
||||
```
|
||||
|
||||
|
||||
48
Cargo.lock
generated
48
Cargo.lock
generated
@@ -279,7 +279,7 @@ checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.188"
|
||||
version = "0.1.226"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
@@ -301,7 +301,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.188"
|
||||
version = "0.1.226"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -309,6 +309,7 @@ dependencies = [
|
||||
"chrono-tz",
|
||||
"clap",
|
||||
"cm-dashboard-shared",
|
||||
"futures",
|
||||
"gethostname",
|
||||
"lettre",
|
||||
"reqwest",
|
||||
@@ -324,7 +325,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.188"
|
||||
version = "0.1.226"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"serde",
|
||||
@@ -552,6 +553,21 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.31"
|
||||
@@ -559,6 +575,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -567,12 +584,34 @@ version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.31"
|
||||
@@ -591,8 +630,11 @@ version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-agent"
|
||||
version = "0.1.189"
|
||||
version = "0.1.227"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
@@ -20,4 +20,5 @@ gethostname = { workspace = true }
|
||||
chrono-tz = "0.8"
|
||||
toml = { workspace = true }
|
||||
async-trait = "0.1"
|
||||
reqwest = { version = "0.11", features = ["json", "blocking"] }
|
||||
reqwest = { version = "0.11", features = ["json", "blocking"] }
|
||||
futures = "0.3"
|
||||
@@ -4,7 +4,7 @@ use std::time::Duration;
|
||||
use tokio::time::interval;
|
||||
use tracing::{debug, error, info};
|
||||
|
||||
use crate::communication::{AgentCommand, ZmqHandler};
|
||||
use crate::communication::ZmqHandler;
|
||||
use crate::config::AgentConfig;
|
||||
use crate::collectors::{
|
||||
Collector,
|
||||
@@ -114,7 +114,7 @@ impl Agent {
|
||||
|
||||
// Set up intervals
|
||||
let mut transmission_interval = interval(Duration::from_secs(
|
||||
self.config.collection_interval_seconds,
|
||||
self.config.zmq.transmission_interval_seconds,
|
||||
));
|
||||
let mut notification_interval = interval(Duration::from_secs(30)); // Check notifications every 30s
|
||||
|
||||
@@ -134,12 +134,6 @@ impl Agent {
|
||||
// NOTE: With structured data, we might need to implement status tracking differently
|
||||
// For now, we skip this until status evaluation is migrated
|
||||
}
|
||||
// Handle incoming commands (check periodically)
|
||||
_ = tokio::time::sleep(Duration::from_millis(100)) => {
|
||||
if let Err(e) = self.handle_commands().await {
|
||||
error!("Error handling commands: {}", e);
|
||||
}
|
||||
}
|
||||
_ = &mut shutdown_rx => {
|
||||
info!("Shutdown signal received, stopping agent loop");
|
||||
break;
|
||||
@@ -259,36 +253,4 @@ impl Agent {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle incoming commands from dashboard
|
||||
async fn handle_commands(&mut self) -> Result<()> {
|
||||
// Try to receive a command (non-blocking)
|
||||
if let Ok(Some(command)) = self.zmq_handler.try_receive_command() {
|
||||
info!("Received command: {:?}", command);
|
||||
|
||||
match command {
|
||||
AgentCommand::CollectNow => {
|
||||
info!("Received immediate collection request");
|
||||
if let Err(e) = self.collect_and_broadcast().await {
|
||||
error!("Failed to collect on demand: {}", e);
|
||||
}
|
||||
}
|
||||
AgentCommand::SetInterval { seconds } => {
|
||||
info!("Received interval change request: {}s", seconds);
|
||||
// Note: This would require more complex handling to update the interval
|
||||
// For now, just acknowledge
|
||||
}
|
||||
AgentCommand::ToggleCollector { name, enabled } => {
|
||||
info!("Received collector toggle request: {} -> {}", name, enabled);
|
||||
// Note: This would require more complex handling to enable/disable collectors
|
||||
// For now, just acknowledge
|
||||
}
|
||||
AgentCommand::Ping => {
|
||||
info!("Received ping command");
|
||||
// Maybe send back a pong or status
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +1,66 @@
|
||||
use async_trait::async_trait;
|
||||
use cm_dashboard_shared::{AgentData, BackupData, BackupDiskData};
|
||||
use cm_dashboard_shared::{AgentData, BackupData, BackupDiskData, Status};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tracing::debug;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use super::{Collector, CollectorError};
|
||||
|
||||
/// Backup collector that reads backup status from TOML files with structured data output
|
||||
pub struct BackupCollector {
|
||||
/// Path to backup status file
|
||||
status_file_path: String,
|
||||
/// Directory containing backup status files
|
||||
status_dir: String,
|
||||
}
|
||||
|
||||
impl BackupCollector {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
status_file_path: "/var/lib/backup/backup-status.toml".to_string(),
|
||||
status_dir: "/var/lib/backup/status".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read backup status from TOML file
|
||||
async fn read_backup_status(&self) -> Result<Option<BackupStatusToml>, CollectorError> {
|
||||
if !Path::new(&self.status_file_path).exists() {
|
||||
debug!("Backup status file not found: {}", self.status_file_path);
|
||||
return Ok(None);
|
||||
/// Scan directory for all backup status files
|
||||
async fn scan_status_files(&self) -> Result<Vec<PathBuf>, CollectorError> {
|
||||
let status_path = Path::new(&self.status_dir);
|
||||
|
||||
if !status_path.exists() {
|
||||
debug!("Backup status directory not found: {}", self.status_dir);
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&self.status_file_path)
|
||||
let mut status_files = Vec::new();
|
||||
|
||||
match fs::read_dir(status_path) {
|
||||
Ok(entries) => {
|
||||
for entry in entries {
|
||||
if let Ok(entry) = entry {
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
|
||||
if filename.starts_with("backup-status-") && filename.ends_with(".toml") {
|
||||
status_files.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to read backup status directory: {}", e);
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(status_files)
|
||||
}
|
||||
|
||||
/// Read a single backup status file
|
||||
async fn read_status_file(&self, path: &Path) -> Result<BackupStatusToml, CollectorError> {
|
||||
let content = fs::read_to_string(path)
|
||||
.map_err(|e| CollectorError::SystemRead {
|
||||
path: self.status_file_path.clone(),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
error: e.to_string(),
|
||||
})?;
|
||||
|
||||
@@ -40,66 +70,122 @@ impl BackupCollector {
|
||||
error: format!("Failed to parse backup status TOML: {}", e),
|
||||
})?;
|
||||
|
||||
Ok(Some(status))
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
/// Calculate backup status from TOML status field
|
||||
fn calculate_backup_status(status_str: &str) -> Status {
|
||||
match status_str.to_lowercase().as_str() {
|
||||
"success" => Status::Ok,
|
||||
"warning" => Status::Warning,
|
||||
"failed" | "error" => Status::Critical,
|
||||
_ => Status::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate usage status from disk usage percentage
|
||||
fn calculate_usage_status(usage_percent: f32) -> Status {
|
||||
if usage_percent < 80.0 {
|
||||
Status::Ok
|
||||
} else if usage_percent < 90.0 {
|
||||
Status::Warning
|
||||
} else {
|
||||
Status::Critical
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert BackupStatusToml to BackupData and populate AgentData
|
||||
async fn populate_backup_data(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||
if let Some(backup_status) = self.read_backup_status().await? {
|
||||
// Use raw start_time string from TOML
|
||||
let status_files = self.scan_status_files().await?;
|
||||
|
||||
// Extract disk information
|
||||
let repository_disk = if let Some(disk_space) = &backup_status.disk_space {
|
||||
Some(BackupDiskData {
|
||||
serial: backup_status.disk_serial_number.clone().unwrap_or_else(|| "Unknown".to_string()),
|
||||
usage_percent: disk_space.usage_percent as f32,
|
||||
used_gb: disk_space.used_gb as f32,
|
||||
total_gb: disk_space.total_gb as f32,
|
||||
wear_percent: backup_status.disk_wear_percent,
|
||||
temperature_celsius: None, // Not available in current TOML
|
||||
})
|
||||
} else if let Some(serial) = &backup_status.disk_serial_number {
|
||||
// Fallback: create minimal disk info if we have serial but no disk_space
|
||||
Some(BackupDiskData {
|
||||
serial: serial.clone(),
|
||||
usage_percent: 0.0,
|
||||
used_gb: 0.0,
|
||||
total_gb: 0.0,
|
||||
wear_percent: backup_status.disk_wear_percent,
|
||||
temperature_celsius: None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Calculate total repository size from services
|
||||
let total_size_gb = backup_status.services
|
||||
.values()
|
||||
.map(|service| service.repo_size_bytes as f32 / (1024.0 * 1024.0 * 1024.0))
|
||||
.sum::<f32>();
|
||||
|
||||
let backup_data = BackupData {
|
||||
status: backup_status.status,
|
||||
total_size_gb: Some(total_size_gb),
|
||||
repository_health: Some("ok".to_string()), // Derive from status if needed
|
||||
repository_disk,
|
||||
last_backup_size_gb: None, // Not available in current TOML format
|
||||
start_time_raw: Some(backup_status.start_time),
|
||||
};
|
||||
|
||||
agent_data.backup = backup_data;
|
||||
} else {
|
||||
// No backup status available - set default values
|
||||
if status_files.is_empty() {
|
||||
debug!("No backup status files found");
|
||||
agent_data.backup = BackupData {
|
||||
status: "unavailable".to_string(),
|
||||
total_size_gb: None,
|
||||
repository_health: None,
|
||||
repository_disk: None,
|
||||
last_backup_size_gb: None,
|
||||
start_time_raw: None,
|
||||
repositories: Vec::new(),
|
||||
repository_status: Status::Unknown,
|
||||
disks: Vec::new(),
|
||||
};
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut all_repositories = HashSet::new();
|
||||
let mut disks = Vec::new();
|
||||
let mut worst_status = Status::Ok;
|
||||
|
||||
for status_file in status_files {
|
||||
match self.read_status_file(&status_file).await {
|
||||
Ok(backup_status) => {
|
||||
// Collect all service names
|
||||
for service_name in backup_status.services.keys() {
|
||||
all_repositories.insert(service_name.clone());
|
||||
}
|
||||
|
||||
// Calculate backup status
|
||||
let backup_status_enum = Self::calculate_backup_status(&backup_status.status);
|
||||
|
||||
// Calculate usage status from disk space
|
||||
let (usage_percent, used_gb, total_gb, usage_status) = if let Some(disk_space) = &backup_status.disk_space {
|
||||
let usage_pct = disk_space.usage_percent as f32;
|
||||
(
|
||||
usage_pct,
|
||||
disk_space.used_gb as f32,
|
||||
disk_space.total_gb as f32,
|
||||
Self::calculate_usage_status(usage_pct),
|
||||
)
|
||||
} else {
|
||||
(0.0, 0.0, 0.0, Status::Unknown)
|
||||
};
|
||||
|
||||
// Update worst status
|
||||
worst_status = worst_status.max(backup_status_enum).max(usage_status);
|
||||
|
||||
// Build service list for this disk
|
||||
let services: Vec<String> = backup_status.services.keys().cloned().collect();
|
||||
|
||||
// Get min and max archive counts to detect inconsistencies
|
||||
let archives_min: i64 = backup_status.services.values()
|
||||
.map(|service| service.archive_count)
|
||||
.min()
|
||||
.unwrap_or(0);
|
||||
|
||||
let archives_max: i64 = backup_status.services.values()
|
||||
.map(|service| service.archive_count)
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
|
||||
// Create disk data
|
||||
let disk_data = BackupDiskData {
|
||||
serial: backup_status.disk_serial_number.unwrap_or_else(|| "Unknown".to_string()),
|
||||
product_name: backup_status.disk_product_name,
|
||||
wear_percent: backup_status.disk_wear_percent,
|
||||
temperature_celsius: None, // Not available in current TOML
|
||||
last_backup_time: Some(backup_status.start_time),
|
||||
backup_status: backup_status_enum,
|
||||
disk_usage_percent: usage_percent,
|
||||
disk_used_gb: used_gb,
|
||||
disk_total_gb: total_gb,
|
||||
usage_status,
|
||||
services,
|
||||
archives_min,
|
||||
archives_max,
|
||||
};
|
||||
|
||||
disks.push(disk_data);
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to read backup status file {:?}: {}", status_file, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let repositories: Vec<String> = all_repositories.into_iter().collect();
|
||||
|
||||
agent_data.backup = BackupData {
|
||||
repositories,
|
||||
repository_status: worst_status,
|
||||
disks,
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,36 +119,69 @@ impl CpuCollector {
|
||||
utils::parse_u64(content.trim())
|
||||
}
|
||||
|
||||
/// Collect CPU frequency and populate AgentData
|
||||
async fn collect_frequency(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||
// Try scaling frequency first (more accurate for current frequency)
|
||||
if let Ok(freq) =
|
||||
utils::read_proc_file("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
|
||||
{
|
||||
if let Ok(freq_khz) = utils::parse_u64(freq.trim()) {
|
||||
let freq_mhz = freq_khz as f32 / 1000.0;
|
||||
agent_data.system.cpu.frequency_mhz = freq_mhz;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
/// Collect CPU C-state (idle depth) and populate AgentData with top 3 C-states by usage
|
||||
async fn collect_cstate(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
|
||||
// Read C-state usage from first CPU (representative of overall system)
|
||||
// C-states indicate CPU idle depth: C1=light sleep, C6=deep sleep, C10=deepest
|
||||
|
||||
// Fallback: parse /proc/cpuinfo for base frequency
|
||||
if let Ok(content) = utils::read_proc_file("/proc/cpuinfo") {
|
||||
for line in content.lines() {
|
||||
if line.starts_with("cpu MHz") {
|
||||
if let Some(freq_str) = line.split(':').nth(1) {
|
||||
if let Ok(freq_mhz) = utils::parse_f32(freq_str) {
|
||||
agent_data.system.cpu.frequency_mhz = freq_mhz;
|
||||
return Ok(());
|
||||
let mut cstate_times: Vec<(String, u64)> = Vec::new();
|
||||
let mut total_time: u64 = 0;
|
||||
|
||||
// Collect all C-state times from CPU0
|
||||
for state_num in 0..=10 {
|
||||
let time_path = format!("/sys/devices/system/cpu/cpu0/cpuidle/state{}/time", state_num);
|
||||
let name_path = format!("/sys/devices/system/cpu/cpu0/cpuidle/state{}/name", state_num);
|
||||
|
||||
if let Ok(time_str) = utils::read_proc_file(&time_path) {
|
||||
if let Ok(time) = utils::parse_u64(time_str.trim()) {
|
||||
if let Ok(name) = utils::read_proc_file(&name_path) {
|
||||
let state_name = name.trim();
|
||||
// Skip POLL state (not real idle)
|
||||
if state_name != "POLL" && time > 0 {
|
||||
// Extract "C" + digits pattern (C3, C10, etc.) to reduce JSON size
|
||||
// Handles formats like "C3_ACPI", "C10_MWAIT", etc.
|
||||
let clean_name = if let Some(c_pos) = state_name.find('C') {
|
||||
let rest = &state_name[c_pos + 1..];
|
||||
let digit_count = rest.chars().take_while(|c| c.is_ascii_digit()).count();
|
||||
if digit_count > 0 {
|
||||
state_name[c_pos..c_pos + 1 + digit_count].to_string()
|
||||
} else {
|
||||
state_name.to_string()
|
||||
}
|
||||
} else {
|
||||
state_name.to_string()
|
||||
};
|
||||
cstate_times.push((clean_name, time));
|
||||
total_time += time;
|
||||
}
|
||||
}
|
||||
break; // Only need first CPU entry
|
||||
}
|
||||
} else {
|
||||
// No more states available
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
debug!("CPU frequency not available");
|
||||
// Leave frequency as 0.0 if not available
|
||||
// Sort by time descending to get top 3
|
||||
cstate_times.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
|
||||
// Calculate percentages for top 3 and populate AgentData
|
||||
agent_data.system.cpu.cstates = cstate_times
|
||||
.iter()
|
||||
.take(3)
|
||||
.map(|(name, time)| {
|
||||
let percent = if total_time > 0 {
|
||||
(*time as f32 / total_time as f32) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
cm_dashboard_shared::CStateInfo {
|
||||
name: name.clone(),
|
||||
percent,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -165,8 +198,8 @@ impl Collector for CpuCollector {
|
||||
// Collect temperature (optional)
|
||||
self.collect_temperature(agent_data).await?;
|
||||
|
||||
// Collect frequency (optional)
|
||||
self.collect_frequency(agent_data).await?;
|
||||
// Collect C-state (CPU idle depth)
|
||||
self.collect_cstate(agent_data).await?;
|
||||
|
||||
let duration = start.elapsed();
|
||||
debug!("CPU collection completed in {:?}", duration);
|
||||
|
||||
@@ -3,10 +3,9 @@ use async_trait::async_trait;
|
||||
use cm_dashboard_shared::{AgentData, DriveData, FilesystemData, PoolData, HysteresisThresholds, Status};
|
||||
|
||||
use crate::config::DiskConfig;
|
||||
use std::process::Command;
|
||||
use std::time::Instant;
|
||||
use tokio::process::Command as TokioCommand;
|
||||
use std::process::Command as StdCommand;
|
||||
use std::collections::HashMap;
|
||||
use tracing::debug;
|
||||
|
||||
use super::{Collector, CollectorError};
|
||||
|
||||
@@ -67,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?;
|
||||
|
||||
@@ -104,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(())
|
||||
}
|
||||
|
||||
@@ -114,7 +107,7 @@ impl DiskCollector {
|
||||
async fn get_mount_devices(&self) -> Result<HashMap<String, String>, CollectorError> {
|
||||
use super::run_command_with_timeout;
|
||||
|
||||
let mut cmd = Command::new("lsblk");
|
||||
let mut cmd = TokioCommand::new("lsblk");
|
||||
cmd.args(&["-rn", "-o", "NAME,MOUNTPOINT"]);
|
||||
|
||||
let output = run_command_with_timeout(cmd, 2).await
|
||||
@@ -141,7 +134,6 @@ impl DiskCollector {
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Found {} mounted block devices", mount_devices.len());
|
||||
Ok(mount_devices)
|
||||
}
|
||||
|
||||
@@ -154,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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,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));
|
||||
}
|
||||
}
|
||||
@@ -189,7 +179,7 @@ impl DiskCollector {
|
||||
|
||||
/// Get filesystem info for a single mount point
|
||||
fn get_filesystem_info(&self, mount_point: &str) -> Result<(u64, u64), CollectorError> {
|
||||
let output = std::process::Command::new("timeout")
|
||||
let output = StdCommand::new("timeout")
|
||||
.args(&["2", "df", "--block-size=1", mount_point])
|
||||
.output()
|
||||
.map_err(|e| CollectorError::SystemRead {
|
||||
@@ -252,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;
|
||||
}
|
||||
|
||||
@@ -282,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;
|
||||
}
|
||||
};
|
||||
@@ -298,8 +286,7 @@ impl DiskCollector {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Found {} mergerfs pools", pools.len());
|
||||
|
||||
Ok(pools)
|
||||
}
|
||||
|
||||
@@ -386,9 +373,9 @@ impl DiskCollector {
|
||||
device.to_string()
|
||||
}
|
||||
|
||||
/// Get SMART data for drives
|
||||
/// Get SMART data for drives in parallel
|
||||
async fn get_smart_data_for_drives(&self, physical_drives: &[PhysicalDrive], mergerfs_pools: &[MergerfsPool]) -> HashMap<String, SmartData> {
|
||||
let mut smart_data = HashMap::new();
|
||||
use futures::future::join_all;
|
||||
|
||||
// Collect all drive names
|
||||
let mut all_drives = std::collections::HashSet::new();
|
||||
@@ -404,9 +391,24 @@ impl DiskCollector {
|
||||
}
|
||||
}
|
||||
|
||||
// Get SMART data for each drive
|
||||
for drive_name in all_drives {
|
||||
if let Ok(data) = self.get_smart_data(&drive_name).await {
|
||||
// Collect SMART data for all drives in parallel
|
||||
let futures: Vec<_> = all_drives
|
||||
.iter()
|
||||
.map(|drive_name| {
|
||||
let drive = drive_name.clone();
|
||||
async move {
|
||||
let result = self.get_smart_data(&drive).await;
|
||||
(drive, result)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let results = join_all(futures).await;
|
||||
|
||||
// Build HashMap from results
|
||||
let mut smart_data = HashMap::new();
|
||||
for (drive_name, result) in results {
|
||||
if let Ok(data) = result {
|
||||
smart_data.insert(drive_name, data);
|
||||
}
|
||||
}
|
||||
@@ -420,7 +422,7 @@ impl DiskCollector {
|
||||
|
||||
// Use direct smartctl (no sudo) - service has CAP_SYS_RAWIO and CAP_SYS_ADMIN capabilities
|
||||
// For NVMe drives, specify device type explicitly
|
||||
let mut cmd = Command::new("smartctl");
|
||||
let mut cmd = TokioCommand::new("smartctl");
|
||||
if drive_name.starts_with("nvme") {
|
||||
cmd.args(&["-d", "nvme", "-a", &format!("/dev/{}", drive_name)]);
|
||||
} else {
|
||||
@@ -435,8 +437,10 @@ impl DiskCollector {
|
||||
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
if !output.status.success() {
|
||||
// Return unknown data rather than failing completely
|
||||
// Note: smartctl returns non-zero exit codes for warnings (like exit code 32
|
||||
// for "temperature was high in the past"), but the output data is still valid.
|
||||
// Only check if we got any output at all, don't reject based on exit code.
|
||||
if output_str.is_empty() {
|
||||
return Ok(SmartData {
|
||||
health: "UNKNOWN".to_string(),
|
||||
serial_number: None,
|
||||
@@ -444,7 +448,7 @@ impl DiskCollector {
|
||||
wear_percent: None,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
let mut health = "UNKNOWN".to_string();
|
||||
let mut serial_number = None;
|
||||
let mut temperature = None;
|
||||
@@ -763,7 +767,7 @@ impl DiskCollector {
|
||||
/// Get drive information for a mount path
|
||||
fn get_drive_info_for_path(&self, path: &str) -> anyhow::Result<PoolDrive> {
|
||||
// Use lsblk to find the backing device with timeout
|
||||
let output = Command::new("timeout")
|
||||
let output = StdCommand::new("timeout")
|
||||
.args(&["2", "lsblk", "-rn", "-o", "NAME,MOUNTPOINT"])
|
||||
.output()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to run lsblk: {}", e))?;
|
||||
@@ -785,20 +789,13 @@ impl DiskCollector {
|
||||
|
||||
// Extract base device name (e.g., "sda1" -> "sda")
|
||||
let base_device = self.extract_base_device(&format!("/dev/{}", device));
|
||||
|
||||
// Get temperature from SMART data if available
|
||||
let temperature = if let Ok(smart_data) = tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current().block_on(self.get_smart_data(&base_device))
|
||||
}) {
|
||||
smart_data.temperature_celsius
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
|
||||
// Temperature will be filled in later from parallel SMART collection
|
||||
// Don't collect it here to avoid sequential blocking with problematic async nesting
|
||||
Ok(PoolDrive {
|
||||
name: base_device,
|
||||
mount_point: path.to_string(),
|
||||
temperature_celsius: temperature,
|
||||
temperature_celsius: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use cm_dashboard_shared::{AgentData};
|
||||
use std::process::{Command, Output};
|
||||
use std::process::Output;
|
||||
use std::time::Duration;
|
||||
use tokio::time::timeout;
|
||||
|
||||
pub mod backup;
|
||||
pub mod cpu;
|
||||
@@ -16,16 +15,34 @@ pub mod systemd;
|
||||
pub use error::CollectorError;
|
||||
|
||||
/// Run a command with a timeout to prevent blocking
|
||||
pub async fn run_command_with_timeout(mut cmd: Command, timeout_secs: u64) -> std::io::Result<Output> {
|
||||
/// Properly kills the process if timeout is exceeded
|
||||
pub async fn run_command_with_timeout(mut cmd: tokio::process::Command, timeout_secs: u64) -> std::io::Result<Output> {
|
||||
use tokio::time::timeout;
|
||||
use std::process::Stdio;
|
||||
let timeout_duration = Duration::from_secs(timeout_secs);
|
||||
|
||||
match timeout(timeout_duration, tokio::task::spawn_blocking(move || cmd.output())).await {
|
||||
Ok(Ok(result)) => result,
|
||||
Ok(Err(e)) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
|
||||
Err(_) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::TimedOut,
|
||||
format!("Command timed out after {} seconds", timeout_secs)
|
||||
)),
|
||||
// Configure stdio to capture output
|
||||
cmd.stdout(Stdio::piped());
|
||||
cmd.stderr(Stdio::piped());
|
||||
|
||||
let child = cmd.spawn()?;
|
||||
let pid = child.id();
|
||||
|
||||
match timeout(timeout_duration, child.wait_with_output()).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => {
|
||||
// Timeout - force kill the process using system kill command
|
||||
if let Some(process_id) = pid {
|
||||
let _ = tokio::process::Command::new("kill")
|
||||
.args(&["-9", &process_id.to_string()])
|
||||
.output()
|
||||
.await;
|
||||
}
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::TimedOut,
|
||||
format!("Command timed out after {} seconds", timeout_secs)
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ use cm_dashboard_shared::{AgentData, ServiceData, SubServiceData, SubServiceMetr
|
||||
use std::process::Command;
|
||||
use std::sync::RwLock;
|
||||
use std::time::Instant;
|
||||
use tracing::{debug, warn};
|
||||
use tracing::debug;
|
||||
|
||||
use super::{Collector, CollectorError};
|
||||
use crate::config::SystemdConfig;
|
||||
@@ -43,9 +43,10 @@ struct ServiceCacheState {
|
||||
/// Cached service status information from systemctl list-units
|
||||
#[derive(Debug, Clone)]
|
||||
struct ServiceStatusInfo {
|
||||
load_state: String,
|
||||
active_state: String,
|
||||
sub_state: String,
|
||||
memory_bytes: Option<u64>,
|
||||
restart_count: Option<u32>,
|
||||
start_timestamp: Option<u64>,
|
||||
}
|
||||
|
||||
impl SystemdCollector {
|
||||
@@ -86,14 +87,20 @@ impl SystemdCollector {
|
||||
let mut complete_service_data = Vec::new();
|
||||
for service_name in &monitored_services {
|
||||
match self.get_service_status(service_name) {
|
||||
Ok((active_status, _detailed_info)) => {
|
||||
let memory_mb = self.get_service_memory_usage(service_name).await.unwrap_or(0.0);
|
||||
let disk_gb = self.get_service_disk_usage(service_name).await.unwrap_or(0.0);
|
||||
|
||||
Ok(status_info) => {
|
||||
let mut sub_services = Vec::new();
|
||||
|
||||
// Calculate uptime if we have start timestamp
|
||||
let uptime_seconds = status_info.start_timestamp.and_then(|start| {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()?
|
||||
.as_secs();
|
||||
Some(now.saturating_sub(start))
|
||||
});
|
||||
|
||||
// Sub-service metrics for specific services (always include cached results)
|
||||
if service_name.contains("nginx") && active_status == "active" {
|
||||
if service_name.contains("nginx") && status_info.active_state == "active" {
|
||||
let nginx_sites = self.get_nginx_site_metrics();
|
||||
for (site_name, latency_ms) in nginx_sites {
|
||||
let site_status = if latency_ms >= 0.0 && latency_ms < self.config.nginx_latency_critical_ms {
|
||||
@@ -113,11 +120,12 @@ impl SystemdCollector {
|
||||
name: site_name.clone(),
|
||||
service_status: self.calculate_service_status(&site_name, &site_status),
|
||||
metrics,
|
||||
service_type: "nginx_site".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if service_name.contains("docker") && active_status == "active" {
|
||||
if service_name.contains("docker") && status_info.active_state == "active" {
|
||||
let docker_containers = self.get_docker_containers();
|
||||
for (container_name, container_status) in docker_containers {
|
||||
// For now, docker containers have no additional metrics
|
||||
@@ -128,12 +136,13 @@ impl SystemdCollector {
|
||||
name: container_name.clone(),
|
||||
service_status: self.calculate_service_status(&container_name, &container_status),
|
||||
metrics,
|
||||
service_type: "container".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
// Add Docker images
|
||||
let docker_images = self.get_docker_images();
|
||||
for (image_name, image_status, image_size_str, image_size_mb) in docker_images {
|
||||
for (image_name, image_status, image_size_mb) in docker_images {
|
||||
let mut metrics = Vec::new();
|
||||
metrics.push(SubServiceMetric {
|
||||
label: "size".to_string(),
|
||||
@@ -142,9 +151,10 @@ impl SystemdCollector {
|
||||
});
|
||||
|
||||
sub_services.push(SubServiceData {
|
||||
name: format!("{} ({})", image_name, image_size_str),
|
||||
name: image_name.to_string(),
|
||||
service_status: self.calculate_service_status(&image_name, &image_status),
|
||||
metrics,
|
||||
service_type: "image".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -152,11 +162,12 @@ impl SystemdCollector {
|
||||
// Create complete service data
|
||||
let service_data = ServiceData {
|
||||
name: service_name.clone(),
|
||||
memory_mb,
|
||||
disk_gb,
|
||||
user_stopped: false, // TODO: Integrate with service tracker
|
||||
service_status: self.calculate_service_status(service_name, &active_status),
|
||||
service_status: self.calculate_service_status(service_name, &status_info.active_state),
|
||||
sub_services,
|
||||
memory_bytes: status_info.memory_bytes,
|
||||
restart_count: status_info.restart_count,
|
||||
uptime_seconds,
|
||||
};
|
||||
|
||||
// Add to AgentData and cache
|
||||
@@ -292,14 +303,13 @@ impl SystemdCollector {
|
||||
let fields: Vec<&str> = line.split_whitespace().collect();
|
||||
if fields.len() >= 4 && fields[0].ends_with(".service") {
|
||||
let service_name = fields[0].trim_end_matches(".service");
|
||||
let load_state = fields.get(1).unwrap_or(&"unknown").to_string();
|
||||
let active_state = fields.get(2).unwrap_or(&"unknown").to_string();
|
||||
let sub_state = fields.get(3).unwrap_or(&"unknown").to_string();
|
||||
|
||||
status_cache.insert(service_name.to_string(), ServiceStatusInfo {
|
||||
load_state,
|
||||
active_state,
|
||||
sub_state,
|
||||
memory_bytes: None,
|
||||
restart_count: None,
|
||||
start_timestamp: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -308,9 +318,10 @@ impl SystemdCollector {
|
||||
for service_name in &all_service_names {
|
||||
if !status_cache.contains_key(service_name) {
|
||||
status_cache.insert(service_name.to_string(), ServiceStatusInfo {
|
||||
load_state: "not-loaded".to_string(),
|
||||
active_state: "inactive".to_string(),
|
||||
sub_state: "dead".to_string(),
|
||||
memory_bytes: None,
|
||||
restart_count: None,
|
||||
start_timestamp: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -342,36 +353,60 @@ impl SystemdCollector {
|
||||
Ok((services, status_cache))
|
||||
}
|
||||
|
||||
/// Get service status from cache (if available) or fallback to systemctl
|
||||
fn get_service_status(&self, service: &str) -> Result<(String, String)> {
|
||||
// Try to get status from cache first
|
||||
if let Ok(state) = self.state.read() {
|
||||
if let Some(cached_info) = state.service_status_cache.get(service) {
|
||||
let active_status = cached_info.active_state.clone();
|
||||
let detailed_info = format!(
|
||||
"LoadState={}\nActiveState={}\nSubState={}",
|
||||
cached_info.load_state,
|
||||
cached_info.active_state,
|
||||
cached_info.sub_state
|
||||
);
|
||||
return Ok((active_status, detailed_info));
|
||||
/// Get service status with detailed metrics from systemctl
|
||||
fn get_service_status(&self, service: &str) -> Result<ServiceStatusInfo> {
|
||||
// Always fetch fresh data to get detailed metrics (memory, restarts, uptime)
|
||||
// Note: Cache in service_status_cache only has basic active_state from discovery,
|
||||
// with all detailed metrics set to None. We need fresh systemctl show data.
|
||||
|
||||
let output = Command::new("timeout")
|
||||
.args(&[
|
||||
"2",
|
||||
"systemctl",
|
||||
"show",
|
||||
&format!("{}.service", service),
|
||||
"--property=LoadState,ActiveState,SubState,MemoryCurrent,NRestarts,ExecMainStartTimestamp"
|
||||
])
|
||||
.output()?;
|
||||
|
||||
let output_str = String::from_utf8(output.stdout)?;
|
||||
|
||||
// Parse properties
|
||||
let mut active_state = String::new();
|
||||
let mut memory_bytes = None;
|
||||
let mut restart_count = None;
|
||||
let mut start_timestamp = None;
|
||||
|
||||
for line in output_str.lines() {
|
||||
if let Some(value) = line.strip_prefix("ActiveState=") {
|
||||
active_state = value.to_string();
|
||||
} else if let Some(value) = line.strip_prefix("MemoryCurrent=") {
|
||||
if value != "[not set]" {
|
||||
memory_bytes = value.parse().ok();
|
||||
}
|
||||
} else if let Some(value) = line.strip_prefix("NRestarts=") {
|
||||
restart_count = value.parse().ok();
|
||||
} else if let Some(value) = line.strip_prefix("ExecMainStartTimestamp=") {
|
||||
if value != "[not set]" && !value.is_empty() {
|
||||
// Parse timestamp to seconds since epoch
|
||||
if let Ok(output) = Command::new("date")
|
||||
.args(&["+%s", "-d", value])
|
||||
.output()
|
||||
{
|
||||
if let Ok(timestamp_str) = String::from_utf8(output.stdout) {
|
||||
start_timestamp = timestamp_str.trim().parse().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to systemctl if not in cache (with 2 second timeout)
|
||||
let output = Command::new("timeout")
|
||||
.args(&["2", "systemctl", "is-active", &format!("{}.service", service)])
|
||||
.output()?;
|
||||
|
||||
let active_status = String::from_utf8(output.stdout)?.trim().to_string();
|
||||
|
||||
// Get more detailed info (with 2 second timeout)
|
||||
let output = Command::new("timeout")
|
||||
.args(&["2", "systemctl", "show", &format!("{}.service", service), "--property=LoadState,ActiveState,SubState"])
|
||||
.output()?;
|
||||
|
||||
let detailed_info = String::from_utf8(output.stdout)?;
|
||||
Ok((active_status, detailed_info))
|
||||
Ok(ServiceStatusInfo {
|
||||
active_state,
|
||||
memory_bytes,
|
||||
restart_count,
|
||||
start_timestamp,
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if service name matches pattern (supports wildcards like nginx*)
|
||||
@@ -413,80 +448,6 @@ impl SystemdCollector {
|
||||
true
|
||||
}
|
||||
|
||||
/// Get disk usage for a specific service
|
||||
async fn get_service_disk_usage(&self, service_name: &str) -> Result<f32, CollectorError> {
|
||||
// Check if this service has configured directory paths
|
||||
if let Some(dirs) = self.config.service_directories.get(service_name) {
|
||||
// Service has configured paths - use the first accessible one
|
||||
for dir in dirs {
|
||||
if let Some(size) = self.get_directory_size(dir).await {
|
||||
return Ok(size);
|
||||
}
|
||||
}
|
||||
// If configured paths failed, return 0
|
||||
return Ok(0.0);
|
||||
}
|
||||
|
||||
// No configured path - try to get WorkingDirectory from systemctl (with 2 second timeout)
|
||||
let output = Command::new("timeout")
|
||||
.args(&["2", "systemctl", "show", &format!("{}.service", service_name), "--property=WorkingDirectory"])
|
||||
.output()
|
||||
.map_err(|e| CollectorError::SystemRead {
|
||||
path: format!("WorkingDirectory for {}", service_name),
|
||||
error: e.to_string(),
|
||||
})?;
|
||||
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
for line in output_str.lines() {
|
||||
if line.starts_with("WorkingDirectory=") && !line.contains("[not set]") {
|
||||
let dir = line.strip_prefix("WorkingDirectory=").unwrap_or("");
|
||||
if !dir.is_empty() && dir != "/" {
|
||||
return Ok(self.get_directory_size(dir).await.unwrap_or(0.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(0.0)
|
||||
}
|
||||
|
||||
/// Get size of a directory in GB (with 2 second timeout)
|
||||
async fn get_directory_size(&self, path: &str) -> Option<f32> {
|
||||
use super::run_command_with_timeout;
|
||||
|
||||
// Use -s (summary) and --apparent-size for speed, 2 second timeout
|
||||
let mut cmd = Command::new("sudo");
|
||||
cmd.args(&["du", "-s", "--apparent-size", "--block-size=1", path]);
|
||||
|
||||
let output = run_command_with_timeout(cmd, 2).await.ok()?;
|
||||
|
||||
if !output.status.success() {
|
||||
// Log permission errors for debugging but don't spam logs
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
if stderr.contains("Permission denied") {
|
||||
debug!("Permission denied accessing directory: {}", path);
|
||||
} else if stderr.contains("timed out") {
|
||||
warn!("Directory size check timed out for {}", path);
|
||||
} else {
|
||||
debug!("Failed to get size for directory {}: {}", path, stderr);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
let output_str = String::from_utf8(output.stdout).ok()?;
|
||||
let size_str = output_str.split_whitespace().next()?;
|
||||
if let Ok(size_bytes) = size_str.parse::<u64>() {
|
||||
let size_gb = size_bytes as f32 / (1024.0 * 1024.0 * 1024.0);
|
||||
// Return size even if very small (minimum 0.001 GB = 1MB for visibility)
|
||||
if size_gb > 0.0 {
|
||||
Some(size_gb.max(0.001))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate service status, taking user-stopped services into account
|
||||
fn calculate_service_status(&self, service_name: &str, active_status: &str) -> Status {
|
||||
match active_status.to_lowercase().as_str() {
|
||||
@@ -504,33 +465,6 @@ impl SystemdCollector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get memory usage for a specific service
|
||||
async fn get_service_memory_usage(&self, service_name: &str) -> Result<f32, CollectorError> {
|
||||
let output = Command::new("systemctl")
|
||||
.args(&["show", &format!("{}.service", service_name), "--property=MemoryCurrent"])
|
||||
.output()
|
||||
.map_err(|e| CollectorError::SystemRead {
|
||||
path: format!("memory usage for {}", service_name),
|
||||
error: e.to_string(),
|
||||
})?;
|
||||
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
for line in output_str.lines() {
|
||||
if line.starts_with("MemoryCurrent=") {
|
||||
if let Some(mem_str) = line.strip_prefix("MemoryCurrent=") {
|
||||
if mem_str != "[not set]" {
|
||||
if let Ok(memory_bytes) = mem_str.parse::<u64>() {
|
||||
return Ok(memory_bytes as f32 / (1024.0 * 1024.0)); // Convert to MB
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(0.0)
|
||||
}
|
||||
|
||||
/// Check if service collection cache should be updated
|
||||
fn should_update_cache(&self) -> bool {
|
||||
let state = self.state.read().unwrap();
|
||||
@@ -824,7 +758,7 @@ impl SystemdCollector {
|
||||
}
|
||||
|
||||
/// Get docker images as sub-services
|
||||
fn get_docker_images(&self) -> Vec<(String, String, String, f32)> {
|
||||
fn get_docker_images(&self) -> Vec<(String, String, f32)> {
|
||||
let mut images = Vec::new();
|
||||
// Check if docker is available (cm-agent user is in docker group) with 3 second timeout
|
||||
let output = Command::new("timeout")
|
||||
@@ -865,9 +799,8 @@ impl SystemdCollector {
|
||||
let size_mb = self.parse_docker_size(size_str);
|
||||
|
||||
images.push((
|
||||
format!("I {}", image_name),
|
||||
image_name.to_string(),
|
||||
"inactive".to_string(), // Images are informational - use inactive for neutral display
|
||||
size_str.to_string(),
|
||||
size_mb
|
||||
));
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ use zmq::{Context, Socket, SocketType};
|
||||
|
||||
use crate::config::ZmqConfig;
|
||||
|
||||
/// ZMQ communication handler for publishing metrics and receiving commands
|
||||
/// ZMQ communication handler for publishing metrics
|
||||
pub struct ZmqHandler {
|
||||
publisher: Socket,
|
||||
command_receiver: Socket,
|
||||
}
|
||||
|
||||
impl ZmqHandler {
|
||||
@@ -26,20 +25,8 @@ impl ZmqHandler {
|
||||
publisher.set_sndhwm(1000)?; // High water mark for outbound messages
|
||||
publisher.set_linger(1000)?; // Linger time on close
|
||||
|
||||
// Create command receiver socket (PULL socket to receive commands from dashboard)
|
||||
let command_receiver = context.socket(SocketType::PULL)?;
|
||||
let cmd_bind_address = format!("tcp://{}:{}", config.bind_address, config.command_port);
|
||||
command_receiver.bind(&cmd_bind_address)?;
|
||||
|
||||
info!("ZMQ command receiver bound to {}", cmd_bind_address);
|
||||
|
||||
// Set non-blocking mode for command receiver
|
||||
command_receiver.set_rcvtimeo(0)?; // Non-blocking receive
|
||||
command_receiver.set_linger(1000)?;
|
||||
|
||||
Ok(Self {
|
||||
publisher,
|
||||
command_receiver,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -65,36 +52,4 @@ impl ZmqHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Try to receive a command (non-blocking)
|
||||
pub fn try_receive_command(&self) -> Result<Option<AgentCommand>> {
|
||||
match self.command_receiver.recv_bytes(zmq::DONTWAIT) {
|
||||
Ok(bytes) => {
|
||||
debug!("Received command message ({} bytes)", bytes.len());
|
||||
|
||||
let command: AgentCommand = serde_json::from_slice(&bytes)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to deserialize command: {}", e))?;
|
||||
|
||||
debug!("Parsed command: {:?}", command);
|
||||
Ok(Some(command))
|
||||
}
|
||||
Err(zmq::Error::EAGAIN) => {
|
||||
// No message available (non-blocking)
|
||||
Ok(None)
|
||||
}
|
||||
Err(e) => Err(anyhow::anyhow!("ZMQ receive error: {}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Commands that can be sent to the agent
|
||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
||||
pub enum AgentCommand {
|
||||
/// Request immediate metric collection
|
||||
CollectNow,
|
||||
/// Change collection interval
|
||||
SetInterval { seconds: u64 },
|
||||
/// Enable/disable a collector
|
||||
ToggleCollector { name: String, enabled: bool },
|
||||
/// Request status/health check
|
||||
Ping,
|
||||
}
|
||||
|
||||
@@ -13,14 +13,12 @@ pub struct AgentConfig {
|
||||
pub collectors: CollectorConfig,
|
||||
pub cache: CacheConfig,
|
||||
pub notifications: NotificationConfig,
|
||||
pub collection_interval_seconds: u64,
|
||||
}
|
||||
|
||||
/// ZMQ communication configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ZmqConfig {
|
||||
pub publisher_port: u16,
|
||||
pub command_port: u16,
|
||||
pub bind_address: String,
|
||||
pub transmission_interval_seconds: u64,
|
||||
/// Heartbeat transmission interval in seconds for host connectivity detection
|
||||
|
||||
@@ -7,21 +7,13 @@ pub fn validate_config(config: &AgentConfig) -> Result<()> {
|
||||
bail!("ZMQ publisher port cannot be 0");
|
||||
}
|
||||
|
||||
if config.zmq.command_port == 0 {
|
||||
bail!("ZMQ command port cannot be 0");
|
||||
}
|
||||
|
||||
if config.zmq.publisher_port == config.zmq.command_port {
|
||||
bail!("ZMQ publisher and command ports cannot be the same");
|
||||
}
|
||||
|
||||
if config.zmq.bind_address.is_empty() {
|
||||
bail!("ZMQ bind address cannot be empty");
|
||||
}
|
||||
|
||||
// Validate collection interval
|
||||
if config.collection_interval_seconds == 0 {
|
||||
bail!("Collection interval cannot be 0");
|
||||
// Validate ZMQ transmission interval
|
||||
if config.zmq.transmission_interval_seconds == 0 {
|
||||
bail!("ZMQ transmission interval cannot be 0");
|
||||
}
|
||||
|
||||
// Validate CPU thresholds
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard"
|
||||
version = "0.1.189"
|
||||
version = "0.1.227"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -11,6 +11,59 @@ 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 {
|
||||
/// Determine which columns to show based on available width
|
||||
fn from_width(width: u16) -> Self {
|
||||
if width >= 80 {
|
||||
// Full layout: Name (25) + Status (10) + RAM (8) + Uptime (8) + Restarts (5) = 56 chars
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: true,
|
||||
show_restarts: true,
|
||||
}
|
||||
} else if width >= 60 {
|
||||
// Hide restarts: Name (25) + Status (10) + RAM (8) + Uptime (8) = 51 chars
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: true,
|
||||
show_restarts: false,
|
||||
}
|
||||
} else if width >= 45 {
|
||||
// Hide uptime and restarts: Name (25) + Status (10) + RAM (8) = 43 chars
|
||||
Self {
|
||||
show_name: true,
|
||||
show_status: true,
|
||||
show_ram: true,
|
||||
show_uptime: false,
|
||||
show_restarts: false,
|
||||
}
|
||||
} else {
|
||||
// Minimal: Name (25) + Status (10) = 35 chars
|
||||
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 {
|
||||
@@ -28,10 +81,12 @@ pub struct ServicesWidget {
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ServiceInfo {
|
||||
memory_mb: Option<f32>,
|
||||
disk_gb: Option<f32>,
|
||||
metrics: Vec<(String, f32, Option<String>)>, // (label, value, unit)
|
||||
widget_status: Status,
|
||||
service_type: String, // "nginx_site", "container", "image", or empty for parent services
|
||||
memory_bytes: Option<u64>,
|
||||
restart_count: Option<u32>,
|
||||
uptime_seconds: Option<u64>,
|
||||
}
|
||||
|
||||
impl ServicesWidget {
|
||||
@@ -51,8 +106,6 @@ impl ServicesWidget {
|
||||
if metric_name.starts_with("service_") {
|
||||
if let Some(end_pos) = metric_name
|
||||
.rfind("_status")
|
||||
.or_else(|| metric_name.rfind("_memory_mb"))
|
||||
.or_else(|| metric_name.rfind("_disk_gb"))
|
||||
.or_else(|| metric_name.rfind("_latency_ms"))
|
||||
{
|
||||
let service_part = &metric_name[8..end_pos]; // Remove "service_" prefix
|
||||
@@ -75,36 +128,8 @@ impl ServicesWidget {
|
||||
None
|
||||
}
|
||||
|
||||
/// Format disk size with appropriate units (kB/MB/GB)
|
||||
fn format_disk_size(size_gb: f32) -> String {
|
||||
let size_mb = size_gb * 1024.0; // Convert GB to MB
|
||||
|
||||
if size_mb >= 1024.0 {
|
||||
// Show as GB
|
||||
format!("{:.1}GB", size_gb)
|
||||
} else if size_mb >= 1.0 {
|
||||
// Show as MB
|
||||
format!("{:.0}MB", size_mb)
|
||||
} else if size_mb >= 0.001 {
|
||||
// Convert to kB
|
||||
let size_kb = size_mb * 1024.0;
|
||||
format!("{:.0}kB", size_kb)
|
||||
} else {
|
||||
// Show very small sizes as bytes
|
||||
let size_bytes = size_mb * 1024.0 * 1024.0;
|
||||
format!("{:.0}B", size_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Format parent service line - returns text without icon for span formatting
|
||||
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo) -> String {
|
||||
let memory_str = info
|
||||
.memory_mb
|
||||
.map_or("0M".to_string(), |m| format!("{:.0}M", m));
|
||||
let disk_str = info
|
||||
.disk_gb
|
||||
.map_or("0".to_string(), |d| Self::format_disk_size(d));
|
||||
|
||||
fn format_parent_service_line(&self, name: &str, info: &ServiceInfo, columns: ColumnVisibility) -> String {
|
||||
// Truncate long service names to fit layout (account for icon space)
|
||||
let short_name = if name.len() > 22 {
|
||||
format!("{}...", &name[..19])
|
||||
@@ -115,7 +140,7 @@ impl ServicesWidget {
|
||||
// Convert Status enum to display text
|
||||
let status_str = match info.widget_status {
|
||||
Status::Ok => "active",
|
||||
Status::Inactive => "inactive",
|
||||
Status::Inactive => "inactive",
|
||||
Status::Critical => "failed",
|
||||
Status::Pending => "pending",
|
||||
Status::Warning => "warning",
|
||||
@@ -123,10 +148,59 @@ impl ServicesWidget {
|
||||
Status::Offline => "offline",
|
||||
};
|
||||
|
||||
format!(
|
||||
"{:<23} {:<10} {:<8} {:<8}",
|
||||
short_name, status_str, memory_str, disk_str
|
||||
)
|
||||
// Format memory
|
||||
let memory_str = info.memory_bytes.map_or("-".to_string(), |bytes| {
|
||||
let mb = bytes as f64 / (1024.0 * 1024.0);
|
||||
if mb >= 1000.0 {
|
||||
format!("{:.1}G", mb / 1024.0)
|
||||
} else {
|
||||
format!("{:.0}M", mb)
|
||||
}
|
||||
});
|
||||
|
||||
// Format uptime
|
||||
let uptime_str = info.uptime_seconds.map_or("-".to_string(), |secs| {
|
||||
let days = secs / 86400;
|
||||
let hours = (secs % 86400) / 3600;
|
||||
let mins = (secs % 3600) / 60;
|
||||
|
||||
if days > 0 {
|
||||
format!("{}d{}h", days, hours)
|
||||
} else if hours > 0 {
|
||||
format!("{}h{}m", hours, mins)
|
||||
} else {
|
||||
format!("{}m", mins)
|
||||
}
|
||||
});
|
||||
|
||||
// Format restarts (show "!" if > 0 to indicate instability)
|
||||
let restart_str = info.restart_count.map_or("-".to_string(), |count| {
|
||||
if count > 0 {
|
||||
format!("!{}", count)
|
||||
} else {
|
||||
"0".to_string()
|
||||
}
|
||||
});
|
||||
|
||||
// Build format string based on column visibility
|
||||
let mut parts = Vec::new();
|
||||
if columns.show_name {
|
||||
parts.push(format!("{:<23}", short_name));
|
||||
}
|
||||
if columns.show_status {
|
||||
parts.push(format!("{:<10}", status_str));
|
||||
}
|
||||
if columns.show_ram {
|
||||
parts.push(format!("{:<8}", memory_str));
|
||||
}
|
||||
if columns.show_uptime {
|
||||
parts.push(format!("{:<8}", uptime_str));
|
||||
}
|
||||
if columns.show_restarts {
|
||||
parts.push(format!("{:<5}", restart_str));
|
||||
}
|
||||
|
||||
parts.join(" ")
|
||||
}
|
||||
|
||||
|
||||
@@ -169,7 +243,7 @@ impl ServicesWidget {
|
||||
// Convert Status enum to display text for sub-services
|
||||
match info.widget_status {
|
||||
Status::Ok => "active",
|
||||
Status::Inactive => "inactive",
|
||||
Status::Inactive => "inactive",
|
||||
Status::Critical => "failed",
|
||||
Status::Pending => "pending",
|
||||
Status::Warning => "warning",
|
||||
@@ -179,32 +253,62 @@ impl ServicesWidget {
|
||||
};
|
||||
let tree_symbol = if is_last { "└─" } else { "├─" };
|
||||
|
||||
vec![
|
||||
// Indentation and tree prefix
|
||||
ratatui::text::Span::styled(
|
||||
format!(" {} ", tree_symbol),
|
||||
Typography::tree(),
|
||||
),
|
||||
// Status icon
|
||||
ratatui::text::Span::styled(
|
||||
format!("{} ", icon),
|
||||
Style::default().fg(status_color).bg(Theme::background()),
|
||||
),
|
||||
// Service name
|
||||
ratatui::text::Span::styled(
|
||||
format!("{:<18} ", short_name),
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
// Status/latency text
|
||||
ratatui::text::Span::styled(
|
||||
status_str,
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
]
|
||||
// Docker images use docker whale icon
|
||||
if info.service_type == "image" {
|
||||
vec![
|
||||
// Indentation and tree prefix
|
||||
ratatui::text::Span::styled(
|
||||
format!(" {} ", tree_symbol),
|
||||
Typography::tree(),
|
||||
),
|
||||
// Docker icon (simple character for performance)
|
||||
ratatui::text::Span::styled(
|
||||
"D ".to_string(),
|
||||
Style::default().fg(Theme::highlight()).bg(Theme::background()),
|
||||
),
|
||||
// Service name
|
||||
ratatui::text::Span::styled(
|
||||
format!("{:<18} ", short_name),
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
// Status/metrics text
|
||||
ratatui::text::Span::styled(
|
||||
status_str,
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
// Indentation and tree prefix
|
||||
ratatui::text::Span::styled(
|
||||
format!(" {} ", tree_symbol),
|
||||
Typography::tree(),
|
||||
),
|
||||
// Status icon
|
||||
ratatui::text::Span::styled(
|
||||
format!("{} ", icon),
|
||||
Style::default().fg(status_color).bg(Theme::background()),
|
||||
),
|
||||
// Service name
|
||||
ratatui::text::Span::styled(
|
||||
format!("{:<18} ", short_name),
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
// Status/latency text
|
||||
ratatui::text::Span::styled(
|
||||
status_str,
|
||||
Style::default()
|
||||
.fg(Theme::secondary_text())
|
||||
.bg(Theme::background()),
|
||||
),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Move selection up
|
||||
@@ -278,13 +382,15 @@ impl Widget for ServicesWidget {
|
||||
for service in &agent_data.services {
|
||||
// Store parent service
|
||||
let parent_info = ServiceInfo {
|
||||
memory_mb: Some(service.memory_mb),
|
||||
disk_gb: Some(service.disk_gb),
|
||||
metrics: Vec::new(), // Parent services don't have custom metrics
|
||||
widget_status: service.service_status,
|
||||
service_type: String::new(), // Parent services have no type
|
||||
memory_bytes: service.memory_bytes,
|
||||
restart_count: service.restart_count,
|
||||
uptime_seconds: service.uptime_seconds,
|
||||
};
|
||||
self.parent_services.insert(service.name.clone(), parent_info);
|
||||
|
||||
|
||||
// Process sub-services if any
|
||||
if !service.sub_services.is_empty() {
|
||||
let mut sub_list = Vec::new();
|
||||
@@ -293,12 +399,14 @@ impl Widget for ServicesWidget {
|
||||
let metrics: Vec<(String, f32, Option<String>)> = sub_service.metrics.iter()
|
||||
.map(|m| (m.label.clone(), m.value, m.unit.clone()))
|
||||
.collect();
|
||||
|
||||
|
||||
let sub_info = ServiceInfo {
|
||||
memory_mb: None, // Not used for sub-services
|
||||
disk_gb: None, // Not used for sub-services
|
||||
metrics,
|
||||
widget_status: sub_service.service_status,
|
||||
service_type: sub_service.service_type.clone(),
|
||||
memory_bytes: None, // Sub-services don't have individual metrics yet
|
||||
restart_count: None,
|
||||
uptime_seconds: None,
|
||||
};
|
||||
sub_list.push((sub_service.name.clone(), sub_info));
|
||||
}
|
||||
@@ -338,22 +446,16 @@ impl ServicesWidget {
|
||||
self.parent_services
|
||||
.entry(parent_service)
|
||||
.or_insert(ServiceInfo {
|
||||
memory_mb: None,
|
||||
disk_gb: None,
|
||||
metrics: Vec::new(),
|
||||
widget_status: Status::Unknown,
|
||||
service_type: String::new(),
|
||||
memory_bytes: None,
|
||||
restart_count: None,
|
||||
uptime_seconds: None,
|
||||
});
|
||||
|
||||
if metric.name.ends_with("_status") {
|
||||
service_info.widget_status = metric.status;
|
||||
} else if metric.name.ends_with("_memory_mb") {
|
||||
if let Some(memory) = metric.value.as_f32() {
|
||||
service_info.memory_mb = Some(memory);
|
||||
}
|
||||
} else if metric.name.ends_with("_disk_gb") {
|
||||
if let Some(disk) = metric.value.as_f32() {
|
||||
service_info.disk_gb = Some(disk);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(sub_name) => {
|
||||
@@ -373,10 +475,12 @@ impl ServicesWidget {
|
||||
sub_service_list.push((
|
||||
sub_name.clone(),
|
||||
ServiceInfo {
|
||||
memory_mb: None,
|
||||
disk_gb: None,
|
||||
metrics: Vec::new(),
|
||||
widget_status: Status::Unknown,
|
||||
service_type: String::new(), // Unknown type in legacy path
|
||||
memory_bytes: None,
|
||||
restart_count: None,
|
||||
uptime_seconds: None,
|
||||
},
|
||||
));
|
||||
&mut sub_service_list.last_mut().unwrap().1
|
||||
@@ -384,14 +488,6 @@ impl ServicesWidget {
|
||||
|
||||
if metric.name.ends_with("_status") {
|
||||
sub_service_info.widget_status = metric.status;
|
||||
} else if metric.name.ends_with("_memory_mb") {
|
||||
if let Some(memory) = metric.value.as_f32() {
|
||||
sub_service_info.memory_mb = Some(memory);
|
||||
}
|
||||
} else if metric.name.ends_with("_disk_gb") {
|
||||
if let Some(disk) = metric.value.as_f32() {
|
||||
sub_service_info.disk_gb = Some(disk);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -448,11 +544,28 @@ impl ServicesWidget {
|
||||
.constraints([Constraint::Length(1), Constraint::Min(0)])
|
||||
.split(inner_area);
|
||||
|
||||
// Header
|
||||
let header = format!(
|
||||
"{:<25} {:<10} {:<8} {:<8}",
|
||||
"Service:", "Status:", "RAM:", "Disk:"
|
||||
);
|
||||
// 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!("{:<25}", "Service:"));
|
||||
}
|
||||
if columns.show_status {
|
||||
header_parts.push(format!("{:<10}", "Status:"));
|
||||
}
|
||||
if columns.show_ram {
|
||||
header_parts.push(format!("{:<8}", "RAM:"));
|
||||
}
|
||||
if columns.show_uptime {
|
||||
header_parts.push(format!("{:<8}", "Uptime:"));
|
||||
}
|
||||
if columns.show_restarts {
|
||||
header_parts.push(format!("{:<5}", "↻:"));
|
||||
}
|
||||
let header = header_parts.join(" ");
|
||||
|
||||
let header_para = Paragraph::new(header).style(Typography::muted());
|
||||
frame.render_widget(header_para, content_chunks[0]);
|
||||
|
||||
@@ -464,11 +577,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();
|
||||
|
||||
@@ -478,7 +591,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)
|
||||
|
||||
@@ -26,7 +26,7 @@ pub struct SystemWidget {
|
||||
cpu_load_1min: Option<f32>,
|
||||
cpu_load_5min: Option<f32>,
|
||||
cpu_load_15min: Option<f32>,
|
||||
cpu_frequency: Option<f32>,
|
||||
cpu_cstates: Vec<cm_dashboard_shared::CStateInfo>,
|
||||
cpu_status: Status,
|
||||
|
||||
// Memory metrics
|
||||
@@ -45,15 +45,9 @@ pub struct SystemWidget {
|
||||
storage_pools: Vec<StoragePool>,
|
||||
|
||||
// Backup metrics
|
||||
backup_status: String,
|
||||
backup_start_time_raw: Option<String>,
|
||||
backup_disk_serial: Option<String>,
|
||||
backup_disk_usage_percent: Option<f32>,
|
||||
backup_disk_used_gb: Option<f32>,
|
||||
backup_disk_total_gb: Option<f32>,
|
||||
backup_disk_wear_percent: Option<f32>,
|
||||
backup_disk_temperature: Option<f32>,
|
||||
backup_last_size_gb: Option<f32>,
|
||||
backup_repositories: Vec<String>,
|
||||
backup_repository_status: Status,
|
||||
backup_disks: Vec<cm_dashboard_shared::BackupDiskData>,
|
||||
|
||||
// Overall status
|
||||
has_data: bool,
|
||||
@@ -102,7 +96,7 @@ impl SystemWidget {
|
||||
cpu_load_1min: None,
|
||||
cpu_load_5min: None,
|
||||
cpu_load_15min: None,
|
||||
cpu_frequency: None,
|
||||
cpu_cstates: Vec::new(),
|
||||
cpu_status: Status::Unknown,
|
||||
memory_usage_percent: None,
|
||||
memory_used_gb: None,
|
||||
@@ -114,15 +108,9 @@ impl SystemWidget {
|
||||
tmp_status: Status::Unknown,
|
||||
tmpfs_mounts: Vec::new(),
|
||||
storage_pools: Vec::new(),
|
||||
backup_status: "unknown".to_string(),
|
||||
backup_start_time_raw: None,
|
||||
backup_disk_serial: None,
|
||||
backup_disk_usage_percent: None,
|
||||
backup_disk_used_gb: None,
|
||||
backup_disk_total_gb: None,
|
||||
backup_disk_wear_percent: None,
|
||||
backup_disk_temperature: None,
|
||||
backup_last_size_gb: None,
|
||||
backup_repositories: Vec::new(),
|
||||
backup_repository_status: Status::Unknown,
|
||||
backup_disks: Vec::new(),
|
||||
has_data: false,
|
||||
}
|
||||
}
|
||||
@@ -137,12 +125,19 @@ impl SystemWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Format CPU frequency
|
||||
fn format_cpu_frequency(&self) -> String {
|
||||
match self.cpu_frequency {
|
||||
Some(freq) => format!("{:.0} MHz", freq),
|
||||
None => "— MHz".to_string(),
|
||||
/// Format CPU C-states (idle depth) with percentages
|
||||
fn format_cpu_cstate(&self) -> String {
|
||||
if self.cpu_cstates.is_empty() {
|
||||
return "—".to_string();
|
||||
}
|
||||
|
||||
// Format top 3 C-states with percentages: "C10:79% C8:10% C6:8%"
|
||||
// Agent already sends clean names (C3, C10, etc.)
|
||||
self.cpu_cstates
|
||||
.iter()
|
||||
.map(|cs| format!("{}:{:.0}%", cs.name, cs.percent))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
/// Format memory usage
|
||||
@@ -188,7 +183,7 @@ impl Widget for SystemWidget {
|
||||
self.cpu_load_1min = Some(cpu.load_1min);
|
||||
self.cpu_load_5min = Some(cpu.load_5min);
|
||||
self.cpu_load_15min = Some(cpu.load_15min);
|
||||
self.cpu_frequency = Some(cpu.frequency_mhz);
|
||||
self.cpu_cstates = cpu.cstates.clone();
|
||||
self.cpu_status = Status::Ok;
|
||||
|
||||
// Extract memory data directly
|
||||
@@ -214,25 +209,9 @@ impl Widget for SystemWidget {
|
||||
|
||||
// Extract backup data
|
||||
let backup = &agent_data.backup;
|
||||
self.backup_status = backup.status.clone();
|
||||
self.backup_start_time_raw = backup.start_time_raw.clone();
|
||||
self.backup_last_size_gb = backup.last_backup_size_gb;
|
||||
|
||||
if let Some(disk) = &backup.repository_disk {
|
||||
self.backup_disk_serial = Some(disk.serial.clone());
|
||||
self.backup_disk_usage_percent = Some(disk.usage_percent);
|
||||
self.backup_disk_used_gb = Some(disk.used_gb);
|
||||
self.backup_disk_total_gb = Some(disk.total_gb);
|
||||
self.backup_disk_wear_percent = disk.wear_percent;
|
||||
self.backup_disk_temperature = disk.temperature_celsius;
|
||||
} else {
|
||||
self.backup_disk_serial = None;
|
||||
self.backup_disk_usage_percent = None;
|
||||
self.backup_disk_used_gb = None;
|
||||
self.backup_disk_total_gb = None;
|
||||
self.backup_disk_wear_percent = None;
|
||||
self.backup_disk_temperature = None;
|
||||
}
|
||||
self.backup_repositories = backup.repositories.clone();
|
||||
self.backup_repository_status = backup.repository_status;
|
||||
self.backup_disks = backup.disks.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,14 +511,36 @@ impl SystemWidget {
|
||||
fn render_backup(&self) -> Vec<Line<'_>> {
|
||||
let mut lines = Vec::new();
|
||||
|
||||
// First line: serial number with temperature and wear
|
||||
if let Some(serial) = &self.backup_disk_serial {
|
||||
let truncated_serial = truncate_serial(serial);
|
||||
// First section: Repository status and list
|
||||
if !self.backup_repositories.is_empty() {
|
||||
let repo_text = format!("Repo: {}", self.backup_repositories.len());
|
||||
let repo_spans = StatusIcons::create_status_spans(self.backup_repository_status, &repo_text);
|
||||
lines.push(Line::from(repo_spans));
|
||||
|
||||
// List all repositories (sorted for consistent display)
|
||||
let mut sorted_repos = self.backup_repositories.clone();
|
||||
sorted_repos.sort();
|
||||
let repo_count = sorted_repos.len();
|
||||
for (idx, repo) in sorted_repos.iter().enumerate() {
|
||||
let tree_char = if idx == repo_count - 1 { "└─" } else { "├─" };
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(format!(" {} ", tree_char), Typography::tree()),
|
||||
Span::styled(repo.clone(), Typography::secondary()),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
// Second section: Per-disk backup information (sorted by serial for consistent display)
|
||||
let mut sorted_disks = self.backup_disks.clone();
|
||||
sorted_disks.sort_by(|a, b| a.serial.cmp(&b.serial));
|
||||
for disk in &sorted_disks {
|
||||
let truncated_serial = truncate_serial(&disk.serial);
|
||||
let mut details = Vec::new();
|
||||
if let Some(temp) = self.backup_disk_temperature {
|
||||
|
||||
if let Some(temp) = disk.temperature_celsius {
|
||||
details.push(format!("T: {}°C", temp as i32));
|
||||
}
|
||||
if let Some(wear) = self.backup_disk_wear_percent {
|
||||
if let Some(wear) = disk.wear_percent {
|
||||
details.push(format!("W: {}%", wear as i32));
|
||||
}
|
||||
|
||||
@@ -549,44 +550,40 @@ impl SystemWidget {
|
||||
truncated_serial
|
||||
};
|
||||
|
||||
let backup_status = match self.backup_status.as_str() {
|
||||
"completed" | "success" => Status::Ok,
|
||||
"running" => Status::Pending,
|
||||
"failed" => Status::Critical,
|
||||
_ => Status::Unknown,
|
||||
};
|
||||
|
||||
let disk_spans = StatusIcons::create_status_spans(backup_status, &disk_text);
|
||||
// Overall disk status (worst of backup and usage)
|
||||
let disk_status = disk.backup_status.max(disk.usage_status);
|
||||
let disk_spans = StatusIcons::create_status_spans(disk_status, &disk_text);
|
||||
lines.push(Line::from(disk_spans));
|
||||
|
||||
// Show backup time from TOML if available
|
||||
if let Some(start_time) = &self.backup_start_time_raw {
|
||||
let time_text = if let Some(size) = self.backup_last_size_gb {
|
||||
format!("Time: {} ({:.1}GB)", start_time, size)
|
||||
} else {
|
||||
format!("Time: {}", start_time)
|
||||
};
|
||||
|
||||
lines.push(Line::from(vec![
|
||||
// Show backup time with status
|
||||
if let Some(backup_time) = &disk.last_backup_time {
|
||||
let time_text = format!("Backup: {}", backup_time);
|
||||
let mut time_spans = vec![
|
||||
Span::styled(" ├─ ", Typography::tree()),
|
||||
Span::styled(time_text, Typography::secondary())
|
||||
]));
|
||||
];
|
||||
time_spans.extend(StatusIcons::create_status_spans(disk.backup_status, &time_text));
|
||||
lines.push(Line::from(time_spans));
|
||||
}
|
||||
|
||||
// Usage information
|
||||
if let (Some(used), Some(total), Some(usage_percent)) = (
|
||||
self.backup_disk_used_gb,
|
||||
self.backup_disk_total_gb,
|
||||
self.backup_disk_usage_percent
|
||||
) {
|
||||
let usage_text = format!("Usage: {:.0}% {:.0}GB/{:.0}GB", usage_percent, used, total);
|
||||
let usage_spans = StatusIcons::create_status_spans(Status::Ok, &usage_text);
|
||||
let mut full_spans = vec![
|
||||
Span::styled(" └─ ", Typography::tree()),
|
||||
];
|
||||
full_spans.extend(usage_spans);
|
||||
lines.push(Line::from(full_spans));
|
||||
}
|
||||
// Show usage with status and archive count
|
||||
let archive_display = if disk.archives_min == disk.archives_max {
|
||||
format!("{}", disk.archives_min)
|
||||
} else {
|
||||
format!("{}-{}", disk.archives_min, disk.archives_max)
|
||||
};
|
||||
|
||||
let usage_text = format!(
|
||||
"Usage: ({}) {:.0}% {:.0}GB/{:.0}GB",
|
||||
archive_display,
|
||||
disk.disk_usage_percent,
|
||||
disk.disk_used_gb,
|
||||
disk.disk_total_gb
|
||||
);
|
||||
let mut usage_spans = vec![
|
||||
Span::styled(" └─ ", Typography::tree()),
|
||||
];
|
||||
usage_spans.extend(StatusIcons::create_status_spans(disk.usage_status, &usage_text));
|
||||
lines.push(Line::from(usage_spans));
|
||||
}
|
||||
|
||||
lines
|
||||
@@ -832,10 +829,10 @@ impl SystemWidget {
|
||||
);
|
||||
lines.push(Line::from(cpu_spans));
|
||||
|
||||
let freq_text = self.format_cpu_frequency();
|
||||
let cstate_text = self.format_cpu_cstate();
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(" └─ ", Typography::tree()),
|
||||
Span::styled(format!("Freq: {}", freq_text), Typography::secondary())
|
||||
Span::styled(format!("C-state: {}", cstate_text), Typography::secondary())
|
||||
]));
|
||||
|
||||
// RAM section
|
||||
@@ -894,7 +891,7 @@ impl SystemWidget {
|
||||
lines.extend(storage_lines);
|
||||
|
||||
// Backup section (if available)
|
||||
if self.backup_status != "unavailable" && self.backup_status != "unknown" {
|
||||
if !self.backup_repositories.is_empty() || !self.backup_disks.is_empty() {
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled("Backup:", Typography::widget_title())
|
||||
]));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cm-dashboard-shared"
|
||||
version = "0.1.189"
|
||||
version = "0.1.227"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -40,13 +40,20 @@ pub struct NetworkInterfaceData {
|
||||
pub vlan_id: Option<u16>,
|
||||
}
|
||||
|
||||
/// CPU C-state usage information
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CStateInfo {
|
||||
pub name: String,
|
||||
pub percent: f32,
|
||||
}
|
||||
|
||||
/// CPU monitoring data
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CpuData {
|
||||
pub load_1min: f32,
|
||||
pub load_5min: f32,
|
||||
pub load_15min: f32,
|
||||
pub frequency_mhz: f32,
|
||||
pub cstates: Vec<CStateInfo>, // C-state usage percentages (C1, C6, C10, etc.) - indicates CPU idle depth distribution
|
||||
pub temperature_celsius: Option<f32>,
|
||||
pub load_status: Status,
|
||||
pub temperature_status: Status,
|
||||
@@ -136,11 +143,15 @@ pub struct PoolDriveData {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ServiceData {
|
||||
pub name: String,
|
||||
pub memory_mb: f32,
|
||||
pub disk_gb: f32,
|
||||
pub user_stopped: bool,
|
||||
pub service_status: Status,
|
||||
pub sub_services: Vec<SubServiceData>,
|
||||
/// Memory usage in bytes (from MemoryCurrent)
|
||||
pub memory_bytes: Option<u64>,
|
||||
/// Number of service restarts (from NRestarts)
|
||||
pub restart_count: Option<u32>,
|
||||
/// Uptime in seconds (calculated from ExecMainStartTimestamp)
|
||||
pub uptime_seconds: Option<u64>,
|
||||
}
|
||||
|
||||
/// Sub-service data (nginx sites, docker containers, etc.)
|
||||
@@ -149,6 +160,9 @@ pub struct SubServiceData {
|
||||
pub name: String,
|
||||
pub service_status: Status,
|
||||
pub metrics: Vec<SubServiceMetric>,
|
||||
/// Type of sub-service: "nginx_site", "container", "image"
|
||||
#[serde(default)]
|
||||
pub service_type: String,
|
||||
}
|
||||
|
||||
/// Individual metric for a sub-service
|
||||
@@ -162,23 +176,27 @@ pub struct SubServiceMetric {
|
||||
/// Backup system data
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BackupData {
|
||||
pub status: String,
|
||||
pub total_size_gb: Option<f32>,
|
||||
pub repository_health: Option<String>,
|
||||
pub repository_disk: Option<BackupDiskData>,
|
||||
pub last_backup_size_gb: Option<f32>,
|
||||
pub start_time_raw: Option<String>,
|
||||
pub repositories: Vec<String>,
|
||||
pub repository_status: Status,
|
||||
pub disks: Vec<BackupDiskData>,
|
||||
}
|
||||
|
||||
/// Backup repository disk information
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BackupDiskData {
|
||||
pub serial: String,
|
||||
pub usage_percent: f32,
|
||||
pub used_gb: f32,
|
||||
pub total_gb: f32,
|
||||
pub product_name: Option<String>,
|
||||
pub wear_percent: Option<f32>,
|
||||
pub temperature_celsius: Option<f32>,
|
||||
pub last_backup_time: Option<String>,
|
||||
pub backup_status: Status,
|
||||
pub disk_usage_percent: f32,
|
||||
pub disk_used_gb: f32,
|
||||
pub disk_total_gb: f32,
|
||||
pub usage_status: Status,
|
||||
pub services: Vec<String>,
|
||||
pub archives_min: i64,
|
||||
pub archives_max: i64,
|
||||
}
|
||||
|
||||
impl AgentData {
|
||||
@@ -197,7 +215,7 @@ impl AgentData {
|
||||
load_1min: 0.0,
|
||||
load_5min: 0.0,
|
||||
load_15min: 0.0,
|
||||
frequency_mhz: 0.0,
|
||||
cstates: Vec::new(),
|
||||
temperature_celsius: None,
|
||||
load_status: Status::Unknown,
|
||||
temperature_status: Status::Unknown,
|
||||
@@ -219,12 +237,9 @@ impl AgentData {
|
||||
},
|
||||
services: Vec::new(),
|
||||
backup: BackupData {
|
||||
status: "unknown".to_string(),
|
||||
total_size_gb: None,
|
||||
repository_health: None,
|
||||
repository_disk: None,
|
||||
last_backup_size_gb: None,
|
||||
start_time_raw: None,
|
||||
repositories: Vec::new(),
|
||||
repository_status: Status::Unknown,
|
||||
disks: Vec::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user