73 lines
2.1 KiB
Rust
73 lines
2.1 KiB
Rust
/// WatcherAgent - A Rust-based system monitoring agent
|
|
/// This agent collects hardware metrics and sends them to a backend server.
|
|
/// It supports CPU, GPU, RAM, disk, and network metrics.
|
|
pub mod api;
|
|
pub mod hardware;
|
|
pub mod metrics;
|
|
pub mod models;
|
|
|
|
pub use crate::hardware::gpu;
|
|
use std::error::Error;
|
|
use std::marker::Send;
|
|
use std::marker::Sync;
|
|
use std::result::Result;
|
|
use tokio::task::JoinHandle;
|
|
|
|
async fn flatten<T>(
|
|
handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>,
|
|
) -> Result<T, Box<dyn Error + Send + Sync>> {
|
|
match handle.await {
|
|
Ok(Ok(result)) => Ok(result),
|
|
Ok(Err(err)) => Err(err),
|
|
Err(_err) => Err("handling failed".into()),
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
let server_url = "http://localhost:5000";
|
|
|
|
// Registration
|
|
let (server_id, ip) = match api::register_with_server(server_url).await {
|
|
Ok((id, ip)) => (id, ip),
|
|
Err(e) => {
|
|
eprintln!("Fehler bei der Registrierung am Server: {e}");
|
|
return Err(e.into());
|
|
}
|
|
};
|
|
|
|
// Start background tasks
|
|
// Start heartbeat in background
|
|
let heartbeat_handle = tokio::spawn({
|
|
let ip = ip.clone();
|
|
let server_url = server_url.to_string();
|
|
async move { api::heartbeat_loop(&server_url, &ip).await }
|
|
});
|
|
|
|
// Main metrics loop
|
|
println!("Starting metrics collection...");
|
|
let metrics_handle = tokio::spawn({
|
|
let ip = ip.clone();
|
|
let server_url = server_url.to_string();
|
|
async move {
|
|
let mut collector = metrics::Collector::new(server_id, ip);
|
|
collector.run(&server_url).await
|
|
}
|
|
});
|
|
|
|
// Warte auf beide Tasks und prüfe explizit auf Fehler
|
|
let (heartbeat_handle, metrics_handle) =
|
|
tokio::try_join!(flatten(heartbeat_handle), flatten(metrics_handle))?;
|
|
|
|
match (heartbeat_handle, metrics_handle) {
|
|
(heartbeat, metrics) => println!(
|
|
"All tasks completed successfully: {:?}, {:?}.",
|
|
heartbeat, metrics
|
|
),
|
|
}
|
|
|
|
println!("All tasks completed successfully.");
|
|
|
|
Ok(())
|
|
}
|