Implement multi-disk backup support
- Update BackupData structure to support multiple backup disks - Scan /var/lib/backup/status/ directory for all status files - Calculate status icons for backup and disk usage - Aggregate repository status from all disks - Update dashboard to display all backup disks with per-disk status - Display repository list with count and aggregated status
This commit is contained in:
@@ -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,109 @@ 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();
|
||||
|
||||
// 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,
|
||||
};
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user