diff --git a/WatcherAgent/src/api.rs b/WatcherAgent/src/api.rs index d66f5df..ab22c26 100644 --- a/WatcherAgent/src/api.rs +++ b/WatcherAgent/src/api.rs @@ -1,17 +1,17 @@ -//! # API Module -//! -//! This module provides all HTTP communication between WatcherAgent and the backend server. -//! -//! ## Responsibilities -//! - **Registration:** Registers the agent with the backend and retrieves its server ID and IP address. -//! - **Heartbeat:** Periodically sends heartbeat signals to indicate liveness. -//! - **Metrics Reporting:** Sends collected hardware and network metrics to the backend. -//! - **Command Listening:** Polls for and executes remote commands from the backend (e.g., update image, restart container). -//! -//! ## Usage -//! These functions are called from the main agent loop and background tasks. All network operations are asynchronous and robust to transient failures. +/// # API Module +/// +/// This module provides all HTTP communication between WatcherAgent and the backend server. +/// +/// ## Responsibilities +/// - **Registration:** Registers the agent with the backend and retrieves its server ID and IP address. +/// - **Heartbeat:** Periodically sends heartbeat signals to indicate liveness. +/// - **Metrics Reporting:** Sends collected hardware and network metrics to the backend. +/// - **Command Listening:** Polls for and executes remote commands from the backend (e.g., update image, restart container). +/// +/// ## Usage +/// These functions are called from the main agent loop and background tasks. All network operations are asynchronous and robust to transient failures. use std::time::Duration; use crate::hardware::HardwareInfo; diff --git a/WatcherAgent/src/docker/mod.rs b/WatcherAgent/src/docker/mod.rs index 1e701ab..ebaf999 100644 --- a/WatcherAgent/src/docker/mod.rs +++ b/WatcherAgent/src/docker/mod.rs @@ -1,13 +1,13 @@ -//! # Docker Module -//! -//! This module provides Docker integration for WatcherAgent, including container enumeration, statistics, and lifecycle management. -//! -//! ## Responsibilities -//! - **Container Management:** Lists, inspects, and manages Docker containers relevant to the agent. -//! - **Statistics Aggregation:** Collects network and CPU statistics for all managed containers. -//! - **Lifecycle Operations:** Supports container restart and ID lookup for agent self-management. -//! +/// # Docker Module +/// +/// This module provides Docker integration for WatcherAgent, including container enumeration, statistics, and lifecycle management. +/// +/// ## Responsibilities +/// - **Container Management:** Lists, inspects, and manages Docker containers relevant to the agent. +/// - **Statistics Aggregation:** Collects network and CPU statistics for all managed containers. +/// - **Lifecycle Operations:** Supports container restart and ID lookup for agent self-management. +/// pub mod container; pub mod serverclientcomm; diff --git a/WatcherAgent/src/hardware/cpu.rs b/WatcherAgent/src/hardware/cpu.rs index 333bc97..cfc82cc 100644 --- a/WatcherAgent/src/hardware/cpu.rs +++ b/WatcherAgent/src/hardware/cpu.rs @@ -2,20 +2,20 @@ use anyhow::Result; use std::error::Error; use sysinfo::System; -//! # CPU Hardware Module -//! -//! This module provides CPU information collection for WatcherAgent, including load, temperature, and system uptime. -//! -//! ## Responsibilities -//! - **CPU Detection:** Identifies CPU model and core count. -//! - **Metric Collection:** Queries CPU load, temperature, and uptime. -//! - **Error Handling:** Graceful fallback if metrics are unavailable. -//! -//! ## Units -//! - `current_load`: CPU usage as a percentage (**0.0–100.0**) -//! - `current_temp`: CPU temperature in **degrees Celsius (°C)** -//! - `uptime`: System uptime in **seconds (s)** -//! +/// # CPU Hardware Module +/// +/// This module provides CPU information collection for WatcherAgent, including load, temperature, and system uptime. +/// +/// ## Responsibilities +/// - **CPU Detection:** Identifies CPU model and core count. +/// - **Metric Collection:** Queries CPU load, temperature, and uptime. +/// - **Error Handling:** Graceful fallback if metrics are unavailable. +/// +/// ## Units +/// - `current_load`: CPU usage as a percentage (**0.0–100.0**) +/// - `current_temp`: CPU temperature in **degrees Celsius (°C)** +/// - `uptime`: System uptime in **seconds (s)** +/// /// CPU statistics for the host system. /// /// # Fields diff --git a/WatcherAgent/src/hardware/disk.rs b/WatcherAgent/src/hardware/disk.rs index afc3727..dce17ac 100644 --- a/WatcherAgent/src/hardware/disk.rs +++ b/WatcherAgent/src/hardware/disk.rs @@ -7,19 +7,19 @@ use sysinfo::{Component, Components, Disk, Disks}; use serde::Serialize; -//! # Disk Hardware Module -//! -//! This module provides disk information collection for WatcherAgent, including total and per-disk statistics and temperature data. -//! -//! ## Responsibilities -//! - **Disk Enumeration:** Lists all physical disks and their properties. -//! - **Usage Calculation:** Computes total and per-disk usage, available space, and usage percentage. -//! - **Temperature Monitoring:** Associates disk components with temperature sensors if available. -//! -//! ## Units -//! - All sizes are in **bytes** unless otherwise noted. -//! - Temperatures are in **degrees Celsius (°C)**. -//! +/// # Disk Hardware Module +/// +/// This module provides disk information collection for WatcherAgent, including total and per-disk statistics and temperature data. +/// +/// ## Responsibilities +/// - **Disk Enumeration:** Lists all physical disks and their properties. +/// - **Usage Calculation:** Computes total and per-disk usage, available space, and usage percentage. +/// - **Temperature Monitoring:** Associates disk components with temperature sensors if available. +/// +/// ## Units +/// - All sizes are in **bytes** unless otherwise noted. +/// - Temperatures are in **degrees Celsius (°C)**. +/// /// Summary of disk statistics for the system. /// /// # Fields diff --git a/WatcherAgent/src/hardware/gpu.rs b/WatcherAgent/src/hardware/gpu.rs index 8366c1e..f1064d3 100644 --- a/WatcherAgent/src/hardware/gpu.rs +++ b/WatcherAgent/src/hardware/gpu.rs @@ -2,21 +2,21 @@ use anyhow::Result; use nvml_wrapper::Nvml; use std::error::Error; -//! # GPU Hardware Module -//! -//! This module provides GPU information collection for WatcherAgent, including load, temperature, and VRAM statistics. -//! -//! ## Responsibilities -//! - **GPU Detection:** Identifies GPU model and capabilities. -//! - **Metric Collection:** Queries GPU load, temperature, and VRAM usage using NVML (NVIDIA only). -//! - **Error Handling:** Graceful fallback if GPU or NVML is unavailable. -//! -//! ## Units -//! - `current_load`: GPU usage as a percentage (**0.0–100.0**) -//! - `current_temp`: GPU temperature in **degrees Celsius (°C)** -//! - `vram_total`: Total VRAM in **megabytes (MB)** -//! - `vram_used`: Used VRAM in **megabytes (MB)** -//! +/// # GPU Hardware Module +/// +/// This module provides GPU information collection for WatcherAgent, including load, temperature, and VRAM statistics. +/// +/// ## Responsibilities +/// - **GPU Detection:** Identifies GPU model and capabilities. +/// - **Metric Collection:** Queries GPU load, temperature, and VRAM usage using NVML (NVIDIA only). +/// - **Error Handling:** Graceful fallback if GPU or NVML is unavailable. +/// +/// ## Units +/// - `current_load`: GPU usage as a percentage (**0.0–100.0**) +/// - `current_temp`: GPU temperature in **degrees Celsius (°C)** +/// - `vram_total`: Total VRAM in **megabytes (MB)** +/// - `vram_used`: Used VRAM in **megabytes (MB)** +/// /// GPU statistics for the host system. /// /// # Fields diff --git a/WatcherAgent/src/hardware/memory.rs b/WatcherAgent/src/hardware/memory.rs index 1aad068..1820299 100644 --- a/WatcherAgent/src/hardware/memory.rs +++ b/WatcherAgent/src/hardware/memory.rs @@ -3,18 +3,18 @@ use std::error::Error; use anyhow::Result; use sysinfo::System; -//! # Memory Hardware Module -//! -//! This module provides memory information collection for WatcherAgent, including total, used, and free RAM. -//! -//! ## Responsibilities -//! - **Memory Detection:** Queries system for total, used, and free RAM. -//! - **Usage Calculation:** Computes memory usage percentage. -//! - **Error Handling:** Graceful fallback if metrics are unavailable. -//! -//! ## Units -//! - `total`, `used`, `free`: RAM in **megabytes (MB)** -//! +/// # Memory Hardware Module +/// +/// This module provides memory information collection for WatcherAgent, including total, used, and free RAM. +/// +/// ## Responsibilities +/// - **Memory Detection:** Queries system for total, used, and free RAM. +/// - **Usage Calculation:** Computes memory usage percentage. +/// - **Error Handling:** Graceful fallback if metrics are unavailable. +/// +/// ## Units +/// - `total`, `used`, `free`: RAM in **megabytes (MB)** +/// /// Memory statistics for the host system. /// /// # Fields diff --git a/WatcherAgent/src/hardware/mod.rs b/WatcherAgent/src/hardware/mod.rs index a584d40..017e45a 100644 --- a/WatcherAgent/src/hardware/mod.rs +++ b/WatcherAgent/src/hardware/mod.rs @@ -14,14 +14,14 @@ pub use memory::get_memory_info; pub use network::get_network_info; pub use network::NetworkMonitor; -//! # Hardware Module -//! -//! This module aggregates all hardware subsystems for WatcherAgent, providing unified collection and access to CPU, GPU, memory, disk, and network statistics. -//! -//! ## Responsibilities -//! - **Subsystem Aggregation:** Combines all hardware modules into a single struct for easy access. -//! - **Unified Collection:** Provides a single async method to collect all hardware metrics at once. -//! +/// # Hardware Module +/// +/// This module aggregates all hardware subsystems for WatcherAgent, providing unified collection and access to CPU, GPU, memory, disk, and network statistics. +/// +/// ## Responsibilities +/// - **Subsystem Aggregation:** Combines all hardware modules into a single struct for easy access. +/// - **Unified Collection:** Provides a single async method to collect all hardware metrics at once. +/// /// Aggregated hardware statistics for the host system. /// /// # Fields diff --git a/WatcherAgent/src/hardware/network.rs b/WatcherAgent/src/hardware/network.rs index 40b3d2e..109cff2 100644 --- a/WatcherAgent/src/hardware/network.rs +++ b/WatcherAgent/src/hardware/network.rs @@ -2,18 +2,18 @@ use std::error::Error; use std::result::Result; use std::time::Instant; -//! # Network Hardware Module -//! -//! This module provides network information collection for WatcherAgent, including interface enumeration and bandwidth statistics. -//! -//! ## Responsibilities -//! - **Interface Detection:** Lists all network interfaces. -//! - **Bandwidth Monitoring:** Tracks receive/transmit rates using a rolling monitor. -//! - **Error Handling:** Graceful fallback if metrics are unavailable. -//! -//! ## Units -//! - `rx_rate`, `tx_rate`: Network bandwidth in **bytes per second (B/s)** -//! +/// # Network Hardware Module +/// +/// This module provides network information collection for WatcherAgent, including interface enumeration and bandwidth statistics. +/// +/// ## Responsibilities +/// - **Interface Detection:** Lists all network interfaces. +/// - **Bandwidth Monitoring:** Tracks receive/transmit rates using a rolling monitor. +/// - **Error Handling:** Graceful fallback if metrics are unavailable. +/// +/// ## Units +/// - `rx_rate`, `tx_rate`: Network bandwidth in **bytes per second (B/s)** +/// /// Network statistics for the host system. /// /// # Fields diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index cde9dfd..db508a9 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -1,31 +1,31 @@ -//! # 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 -//! ``` -//! -//! The agent will register itself, start collecting metrics, and listen for remote commands. +/// # 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 +/// ``` +/// +/// The agent will register itself, start collecting metrics, and listen for remote commands. pub mod api; pub mod hardware; @@ -34,6 +34,8 @@ pub mod models; pub mod docker; use tokio::task::JoinHandle; use bollard::Docker; +use std::env; +use std::error::Error; diff --git a/WatcherAgent/src/metrics.rs b/WatcherAgent/src/metrics.rs index 3c3d638..9714068 100644 --- a/WatcherAgent/src/metrics.rs +++ b/WatcherAgent/src/metrics.rs @@ -1,16 +1,16 @@ -//! # Metrics Module -//! -//! This module orchestrates the collection and reporting of hardware and network metrics for WatcherAgent. -//! -//! ## Responsibilities -//! - **Metric Collection:** Gathers real-time statistics from all hardware subsystems (CPU, GPU, RAM, disk, network). -//! - **Reporting:** Periodically sends metrics to the backend server using the API module. -//! - **Error Handling:** Robust to hardware failures and network errors, with retry logic and logging. -//! -//! ## Usage -//! The [`Collector`] struct is instantiated in the main loop and runs as a background task, continuously collecting and reporting metrics. +/// # Metrics Module +/// +/// This module orchestrates the collection and reporting of hardware and network metrics for WatcherAgent. +/// +/// ## Responsibilities +/// - **Metric Collection:** Gathers real-time statistics from all hardware subsystems (CPU, GPU, RAM, disk, network). +/// - **Reporting:** Periodically sends metrics to the backend server using the API module. +/// - **Error Handling:** Robust to hardware failures and network errors, with retry logic and logging. +/// +/// ## Usage +/// The [`Collector`] struct is instantiated in the main loop and runs as a background task, continuously collecting and reporting metrics. use std::error::Error; use std::time::Duration; diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index ca00f22..2a0a4c7 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -1,16 +1,16 @@ -//! # Models Module -//! -//! This module defines all data structures (DTOs) used for communication between WatcherAgent and the backend server, as well as hardware metrics and Docker container info. -//! -//! ## Responsibilities -//! - **DTOs:** Define payloads for registration, metrics, heartbeat, and server commands. -//! - **Units:** All struct fields are documented with their units for clarity and API compatibility. -//! - **Docker Info:** Structures for representing Docker container state and statistics. -//! -//! ## Usage -//! These types are serialized/deserialized for HTTP communication and used throughout the agent for data exchange. +/// # Models Module +/// +/// This module defines all data structures (DTOs) used for communication between WatcherAgent and the backend server, as well as hardware metrics and Docker container info. +/// +/// ## Responsibilities +/// - **DTOs:** Define payloads for registration, metrics, heartbeat, and server commands. +/// - **Units:** All struct fields are documented with their units for clarity and API compatibility. +/// - **Docker Info:** Structures for representing Docker container state and statistics. +/// +/// ## Usage +/// These types are serialized/deserialized for HTTP communication and used throughout the agent for data exchange. use serde::{Deserialize, Serialize}; /// Registration data sent to the backend server.