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:
		
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -22,12 +22,12 @@ name = "agent" | |||||||
| version = "0.1.0" | version = "0.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "anyhow", |  "anyhow", | ||||||
|  |  "clap", | ||||||
|  "common", |  "common", | ||||||
|  "rumqttc", |  "rumqttc", | ||||||
|  "serde", |  "serde", | ||||||
|  "sysinfo", |  "sysinfo", | ||||||
|  "tokio", |  "tokio", | ||||||
|  "toml 0.8.23", |  | ||||||
| ] | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ edition = "2024" | |||||||
| common = { path = "../common" } | common = { path = "../common" } | ||||||
| tokio = { version = "1.45.1", features = ["full"] } | tokio = { version = "1.45.1", features = ["full"] } | ||||||
| serde = { version = "1.0.219", features = ["derive"] } | serde = { version = "1.0.219", features = ["derive"] } | ||||||
|  | clap = { version = "4.5.40", features = ["derive"] } | ||||||
| anyhow = "1.0.98" | anyhow = "1.0.98" | ||||||
| sysinfo = "0.35.2" | sysinfo = "0.35.2" | ||||||
| rumqttc = "0.24.0" | 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 collector::Collector; | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
|  | use mqtt::MqttHandle; | ||||||
|  | use clap::Parser; | ||||||
| use std::thread; | use std::thread; | ||||||
|  |  | ||||||
| pub mod collector; | pub mod collector; | ||||||
| pub mod config; |  | ||||||
| pub mod mqtt; | pub mod mqtt; | ||||||
| pub mod cpu; | 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] | #[tokio::main] | ||||||
| async fn main() -> anyhow::Result<()> { | async fn main() -> anyhow::Result<()> { | ||||||
|     let cfg = load_config()?; |     let args = Args::parse(); | ||||||
|     let cfg: &'static mut Config = Box::leak(Box::new(cfg)); |  | ||||||
|  |  | ||||||
|     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(); |     let mut collector = Collector::new(); | ||||||
|  |  | ||||||
|     loop { |     loop { | ||||||
|         let metrics = collector.collect_all(); |         let metrics = collector.collect_all(); | ||||||
|         client.send_metrics(metrics).await?; |         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 std::time::Duration; | ||||||
| use tokio::task::JoinHandle; | use tokio::task::JoinHandle; | ||||||
|  |  | ||||||
| use crate::config::MqttConfig; |  | ||||||
|  |  | ||||||
| pub struct MqttHandle { | pub struct MqttHandle { | ||||||
|     pub agent: &'static str, |     pub agent: String, | ||||||
|     pub client: AsyncClient, |     pub client: AsyncClient, | ||||||
|     pub eventloop_handle: JoinHandle<()>, |     pub eventloop_handle: JoinHandle<()>, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl MqttHandle { | impl MqttHandle { | ||||||
|     pub async fn create(cfg: &'static MqttConfig) -> Self { |     pub async fn create(agent: String, host: String, port: u16) -> Self { | ||||||
|         let mut mqttoptions = MqttOptions::new(&cfg.agent, &cfg.host, cfg.port); |         let mut mqttoptions = MqttOptions::new(&agent, &host, port); | ||||||
|         mqttoptions.set_keep_alive(Duration::from_secs(5)); |         mqttoptions.set_keep_alive(Duration::from_secs(5)); | ||||||
|  |  | ||||||
|         let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); |         let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); | ||||||
| @@ -28,7 +26,7 @@ impl MqttHandle { | |||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         Self { |         Self { | ||||||
|             agent: &cfg.agent, |             agent, | ||||||
|             client, |             client, | ||||||
|             eventloop_handle |             eventloop_handle | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user