mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
use cli parser for agent
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
send_interval_seconds = 5
|
||||
|
||||
[mqtt]
|
||||
agent = "agent-1"
|
||||
host = "0.0.0.0"
|
||||
port = 1883
|
||||
@@ -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<Config> {
|
||||
let file = std::fs::read_to_string(CONFIG_PATH)?;
|
||||
let config: Config = toml::from_str(&file)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user