Files
nano_metrics/agent/src/config.rs
T
Gilles Soulier 5ee8b66464 feat(v0.1.7): port iperf3 configurable + iperf3 docker sur port 5202
- config.toml: nouveau champ [server] iperf3_port (défaut 5201)
- network_info: iperf3 -p <port> utilise le port configuré
- docker-compose: iperf3 exposé sur 5202 (5201 occupé par linux_benchtools)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 06:47:15 +02:00

132 lines
3.1 KiB
Rust

use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize, Debug, Clone, Default)]
pub struct Config {
pub server: ServerConfig,
pub protocols: ProtocolsConfig,
#[serde(default)]
pub metrics: MetricsConfig,
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct ServerConfig {
pub ip: String,
pub port: u16,
#[serde(default = "default_iperf3_port")]
pub iperf3_port: u16,
}
fn default_iperf3_port() -> u16 { 5201 }
#[derive(Deserialize, Debug, Clone, Default)]
pub struct ProtocolsConfig {
#[serde(default)]
pub udp: UdpConfig,
#[serde(default)]
pub mqtt: MqttConfig,
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct UdpConfig {
#[serde(default)]
pub enabled: bool,
}
#[derive(Deserialize, Debug, Clone)]
pub struct MqttConfig {
#[serde(default)]
pub enabled: bool,
#[serde(default = "default_mqtt_host")]
pub host: String,
#[serde(default = "default_mqtt_port")]
pub port: u16,
#[serde(default = "default_topic_base")]
pub topic_base: String,
#[serde(default = "default_true")]
pub auto_discovery: bool,
#[serde(default = "default_true")]
pub birth_message: bool,
#[serde(default = "default_true")]
pub last_will: bool,
}
impl Default for MqttConfig {
fn default() -> Self {
MqttConfig {
enabled: false,
host: default_mqtt_host(),
port: default_mqtt_port(),
topic_base: default_topic_base(),
auto_discovery: true,
birth_message: true,
last_will: true,
}
}
}
fn default_mqtt_host() -> String {
"10.0.0.3".to_string()
}
fn default_mqtt_port() -> u16 {
1883
}
fn default_topic_base() -> String {
"nanometrics/agents".to_string()
}
fn default_true() -> bool {
true
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct MetricsConfig {
#[serde(default)]
pub cpu: MetricProto,
#[serde(default)]
pub memory: MetricProto,
#[serde(default)]
pub disk: MetricProto,
#[serde(default)]
pub network: MetricProto,
#[serde(default)]
pub uptime: MetricProto,
#[serde(default)]
pub temperature: MetricProto,
#[serde(default)]
pub smart: MetricProto,
#[serde(default)]
pub network_info: SlowMetricProto,
#[serde(default)]
pub hardware_info: SlowMetricProto,
#[serde(default = "default_slow_time")]
pub slow_daily_time: String,
}
fn default_slow_time() -> String { "03:00".to_string() }
#[derive(Deserialize, Debug, Clone)]
pub struct SlowMetricProto {
#[serde(default = "default_true")]
pub udp: bool,
#[serde(default)]
pub mqtt: bool,
}
impl Default for SlowMetricProto {
fn default() -> Self { Self { udp: true, mqtt: false } }
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct MetricProto {
#[serde(default)]
pub udp: bool,
#[serde(default)]
pub mqtt: bool,
}
pub fn load(path: &Path) -> Result<Config, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(path)?;
let config: Config = toml::from_str(&content)?;
Ok(config)
}