diff --git a/Cargo.lock b/Cargo.lock index a14fde0..3821c24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,12 +22,12 @@ name = "agent" version = "0.1.0" dependencies = [ "anyhow", + "clap", "common", "rumqttc", "serde", "sysinfo", "tokio", - "toml 0.8.23", ] [[package]] diff --git a/agent/Cargo.toml b/agent/Cargo.toml index 48e61a2..c26abfe 100644 --- a/agent/Cargo.toml +++ b/agent/Cargo.toml @@ -7,7 +7,7 @@ edition = "2024" common = { path = "../common" } tokio = { version = "1.45.1", features = ["full"] } serde = { version = "1.0.219", features = ["derive"] } +clap = { version = "4.5.40", features = ["derive"] } anyhow = "1.0.98" sysinfo = "0.35.2" rumqttc = "0.24.0" -toml = "0.8.23" diff --git a/agent/config.toml b/agent/config.toml deleted file mode 100644 index 993df97..0000000 --- a/agent/config.toml +++ /dev/null @@ -1,6 +0,0 @@ -send_interval_seconds = 5 - -[mqtt] -agent = "agent-1" -host = "0.0.0.0" -port = 1883 diff --git a/agent/src/config.rs b/agent/src/config.rs deleted file mode 100644 index c789412..0000000 --- a/agent/src/config.rs +++ /dev/null @@ -1,27 +0,0 @@ -use serde::{Deserialize, Serialize}; - -const CONFIG_PATH: &str = if cfg!(debug_assertions) { - "agent/config.toml" -} else { - "config.toml" -}; - -#[derive(Serialize, Deserialize)] -pub struct MqttConfig { - pub agent: String, - pub host: String, - pub port: u16, -} - -#[derive(Serialize, Deserialize)] -pub struct Config { - pub send_interval_seconds: u64, - pub mqtt: MqttConfig, -} - -pub fn load_config() -> anyhow::Result { - let file = std::fs::read_to_string(CONFIG_PATH)?; - let config: Config = toml::from_str(&file)?; - - Ok(config) -} diff --git a/agent/src/main.rs b/agent/src/main.rs index 796249d..1f7288a 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -1,26 +1,42 @@ -use config::{load_config, Config}; -use mqtt::MqttHandle; use collector::Collector; use std::time::Duration; +use mqtt::MqttHandle; +use clap::Parser; use std::thread; pub mod collector; -pub mod config; pub mod mqtt; pub mod cpu; +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg(long, short, default_value = "unknown-agent")] + name: String, + + #[arg(long, default_value = "0.0.0.0")] + host: String, + + #[arg(long, default_value_t = 1883)] + port: u16, + + #[arg(long, short='i', default_value_t = 5)] + interval: u64, +} + #[tokio::main] async fn main() -> anyhow::Result<()> { - let cfg = load_config()?; - let cfg: &'static mut Config = Box::leak(Box::new(cfg)); + let args = Args::parse(); - let client = MqttHandle::create(&mut cfg.mqtt).await; + println!("running agent: {:?}", args); + + let client = MqttHandle::create(args.name, args.host, args.port).await; let mut collector = Collector::new(); loop { let metrics = collector.collect_all(); client.send_metrics(metrics).await?; - thread::sleep(Duration::from_secs(cfg.send_interval_seconds)); + thread::sleep(Duration::from_secs(args.interval)); } } diff --git a/agent/src/mqtt.rs b/agent/src/mqtt.rs index 4eaf22a..be765e9 100644 --- a/agent/src/mqtt.rs +++ b/agent/src/mqtt.rs @@ -3,17 +3,15 @@ use rumqttc::{AsyncClient, MqttOptions, QoS}; use std::time::Duration; use tokio::task::JoinHandle; -use crate::config::MqttConfig; - pub struct MqttHandle { - pub agent: &'static str, + pub agent: String, pub client: AsyncClient, pub eventloop_handle: JoinHandle<()>, } impl MqttHandle { - pub async fn create(cfg: &'static MqttConfig) -> Self { - let mut mqttoptions = MqttOptions::new(&cfg.agent, &cfg.host, cfg.port); + pub async fn create(agent: String, host: String, port: u16) -> Self { + let mut mqttoptions = MqttOptions::new(&agent, &host, port); mqttoptions.set_keep_alive(Duration::from_secs(5)); let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); @@ -28,7 +26,7 @@ impl MqttHandle { }); Self { - agent: &cfg.agent, + agent, client, eventloop_handle }