cm-dashboard/agent/src/config/validation.rs

121 lines
3.9 KiB
Rust

use crate::config::AgentConfig;
use anyhow::{bail, Result};
pub fn validate_config(config: &AgentConfig) -> Result<()> {
// Validate ZMQ configuration
if config.zmq.publisher_port == 0 {
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");
}
if config.zmq.timeout_ms == 0 {
bail!("ZMQ timeout cannot be 0");
}
// Validate collection interval
if config.collection_interval_seconds == 0 {
bail!("Collection interval cannot be 0");
}
// Validate CPU thresholds
if config.collectors.cpu.enabled {
if config.collectors.cpu.load_warning_threshold <= 0.0 {
bail!("CPU load warning threshold must be positive");
}
if config.collectors.cpu.load_critical_threshold
<= config.collectors.cpu.load_warning_threshold
{
bail!("CPU load critical threshold must be greater than warning threshold");
}
if config.collectors.cpu.temperature_warning_threshold <= 0.0 {
bail!("CPU temperature warning threshold must be positive");
}
if config.collectors.cpu.temperature_critical_threshold
<= config.collectors.cpu.temperature_warning_threshold
{
bail!("CPU temperature critical threshold must be greater than warning threshold");
}
}
// Validate memory thresholds
if config.collectors.memory.enabled {
if config.collectors.memory.usage_warning_percent <= 0.0
|| config.collectors.memory.usage_warning_percent > 100.0
{
bail!("Memory usage warning threshold must be between 0 and 100");
}
if config.collectors.memory.usage_critical_percent
<= config.collectors.memory.usage_warning_percent
|| config.collectors.memory.usage_critical_percent > 100.0
{
bail!("Memory usage critical threshold must be between warning threshold and 100");
}
}
// Validate disk thresholds
if config.collectors.disk.enabled {
if config.collectors.disk.usage_warning_percent <= 0.0
|| config.collectors.disk.usage_warning_percent > 100.0
{
bail!("Disk usage warning threshold must be between 0 and 100");
}
if config.collectors.disk.usage_critical_percent
<= config.collectors.disk.usage_warning_percent
|| config.collectors.disk.usage_critical_percent > 100.0
{
bail!("Disk usage critical threshold must be between warning threshold and 100");
}
}
// Validate SMTP configuration
if config.notifications.enabled {
if config.notifications.smtp_host.is_empty() {
bail!("SMTP host cannot be empty when notifications are enabled");
}
if config.notifications.smtp_port == 0 {
bail!("SMTP port cannot be 0");
}
if config.notifications.from_email.is_empty() {
bail!("From email cannot be empty when notifications are enabled");
}
if config.notifications.to_email.is_empty() {
bail!("To email cannot be empty when notifications are enabled");
}
// Basic email validation
if !config.notifications.from_email.contains('@') {
bail!("From email must contain @ symbol");
}
if !config.notifications.to_email.contains('@') {
bail!("To email must contain @ symbol");
}
}
// Validate cache configuration
if config.cache.persist_path.is_empty() {
bail!("Cache persist path cannot be empty");
}
Ok(())
}