added commentation

This commit is contained in:
2025-10-01 12:07:53 +02:00
parent d994be757e
commit 8c49a63a50
13 changed files with 570 additions and 56 deletions

View File

@@ -1,20 +1,55 @@
/// 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.
//! # WatcherAgent
//!
//! **WatcherAgent** is a cross-platform system monitoring agent written in Rust.
//!
//! ## Overview
//! This agent collects real-time hardware metrics (CPU, GPU, RAM, disk, network) and communicates with a backend server for registration, reporting, and remote control. It is designed for deployment in environments where automated monitoring and remote management of system resources is required.
//!
//! ## Features
//! - **Hardware Metrics:** Collects CPU, GPU, RAM, disk, and network statistics using platform-specific APIs.
//! - **Docker Integration:** Detects and manages its own Docker container, supports image updates and container restarts.
//! - **Server Communication:** Registers with a backend server, sends periodic heartbeats, and reports metrics securely.
//! - **Remote Commands:** Listens for and executes commands from the backend (e.g., update image, restart container, stop agent).
//!
//! ## Modules
//! - [`api`]: Handles HTTP communication with the backend server (registration, heartbeat, metrics, commands).
//! - [`hardware`]: Collects hardware metrics from the host system (CPU, GPU, RAM, disk, network).
//! - [`metrics`]: Orchestrates metric collection and reporting.
//! - [`models`]: Defines data structures for server communication and metrics.
//! - [`docker`]: Integrates with Docker for container management and agent lifecycle.
//!
//! ## Usage
//! Run the agent with the backend server URL as an argument:
//! ```sh
//! watcheragent <server-url>
//! ```
//!
//! The agent will register itself, start collecting metrics, and listen for remote commands.
pub mod api;
pub mod hardware;
pub mod metrics;
pub mod models;
pub mod docker;
use std::env;
use std::error::Error;
use std::marker::Send;
use std::marker::Sync;
use std::result::Result;
use tokio::task::JoinHandle;
use bollard::Docker;
/// Awaits a spawned asynchronous task and flattens its nested `Result` type.
///
/// This utility is used to handle the result of a `tokio::spawn`ed task that itself returns a `Result`,
/// propagating any errors from both the task and its execution.
///
/// # Type Parameters
/// * `T` - The type returned by the task on success.
///
/// # Arguments
/// * `handle` - The `JoinHandle` of the spawned task.
///
/// # Returns
/// * `Result<T, Box<dyn Error + Send + Sync>>` - The result of the task, or an error if the task failed or panicked.
async fn flatten<T>(
handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>,
) -> Result<T, Box<dyn Error + Send + Sync>> {
@@ -25,6 +60,24 @@ async fn flatten<T>(
}
}
/// Main entry point for the WatcherAgent application.
///
/// This function performs the following steps:
/// 1. Initializes the Docker client for container management.
/// 2. Detects the current running image version.
/// 3. Parses command-line arguments to obtain the backend server URL.
/// 4. Registers the agent with the backend server and retrieves its server ID and IP address.
/// 5. Spawns background tasks for:
/// - Listening for remote commands from the server
/// - Sending periodic heartbeat signals
/// - Collecting and reporting hardware metrics
/// 6. Waits for all background tasks to complete and logs their results.
///
/// # Arguments
/// * `server-url` - The URL of the backend server to register and report metrics to (passed as a command-line argument).
///
/// # Errors
/// Returns an error if registration or any background task fails, or if required arguments are missing.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Initialize Docker client
@@ -54,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let server_url = &args[1];
println!("Server URL: {:?}", server_url);
// Registration
// Registration with backend server
let (server_id, ip) = match api::register_with_server(&server_url).await {
Ok((id, ip)) => (id, ip),
Err(e) => {