22 Commits

Author SHA1 Message Date
c745125f20 check if checks are needed or needs is enough
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m4s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m47s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m30s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m20s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-03 13:47:00 +02:00
758fa7608f added toolcache
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m10s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m1s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m47s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m8s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-03 13:17:48 +02:00
ee6b947f29 update
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m23s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m54s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m35s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m4s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 4s
2025-10-03 12:22:12 +02:00
18dd1ef528 keine verdreckte doc 2025-10-02 00:38:23 +02:00
8fa7866cc2 creating directory for doc
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m3s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m42s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 4m22s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m8s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-01 22:57:06 +02:00
238ad87119 replaced documentation post with seperate step
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m5s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m47s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 4m20s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m4s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-01 22:42:35 +02:00
d584e21fd9 replaced documentation post with seperate step 2025-10-01 22:33:00 +02:00
ac79a2e0b7 added right path for cargo doc push 2025-10-01 22:21:59 +02:00
1798c1270b added right path for cargo doc push 2025-10-01 22:21:44 +02:00
97d1019b69 added right path for cargo doc push
Some checks failed
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m5s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Failing after 2m50s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Failing after 3m31s
Rust Cross-Platform Build / Build and Push Docker Image (push) Has been skipped
Rust Cross-Platform Build / Create Tag (push) Has been skipped
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
2025-10-01 21:49:02 +02:00
e30ceb75d9 added cargo doc push
Some checks failed
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m6s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Failing after 3m2s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Failing after 3m39s
Rust Cross-Platform Build / Build and Push Docker Image (push) Has been skipped
Rust Cross-Platform Build / Create Tag (push) Has been skipped
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
2025-10-01 19:49:48 +02:00
4681e0c694 fixed id type
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m7s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m56s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m40s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m11s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-01 19:00:34 +02:00
49f1af392d fixed units
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m8s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m5s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m56s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m24s
Rust Cross-Platform Build / Create Tag (push) Successful in 6s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
2025-10-01 13:13:18 +02:00
f78e48900a fixed comments 2025-10-01 12:11:34 +02:00
8c49a63a50 added commentation 2025-10-01 12:07:53 +02:00
d994be757e get clientcontainer; container findable with id image name ahs to be implemented 2025-10-01 11:36:12 +02:00
d7a58e00da added listing all running containers
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m13s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m20s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 4m7s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m22s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-09-29 14:54:03 +02:00
d2efc64487 moved container functions into new mod 2025-09-28 18:54:17 +02:00
1f23c303c1 removed overloading metrics print statements
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m10s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m6s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m43s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m14s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-09-28 17:46:42 +02:00
1cc85bfa14 added debugging for docker image
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m10s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m6s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m54s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m12s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-09-28 01:00:26 +02:00
8bac357dc6 added debugging for docker image
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m10s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m9s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m54s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m12s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
Rust Cross-Platform Build / Create Tag (push) Successful in 4s
2025-09-28 00:36:52 +02:00
7154c01f7a added search for docker image in different locations
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m6s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m45s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m30s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m19s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-09-28 00:24:47 +02:00
15 changed files with 728 additions and 91 deletions

View File

@@ -20,6 +20,8 @@ jobs:
detect-project: detect-project:
name: Detect Rust Project name: Detect Rust Project
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
outputs: outputs:
project-dir: ${{ steps.detect.outputs.project-dir }} project-dir: ${{ steps.detect.outputs.project-dir }}
project-name: ${{ steps.detect.outputs.project-name }} project-name: ${{ steps.detect.outputs.project-name }}
@@ -56,6 +58,8 @@ jobs:
needs: [detect-project] needs: [detect-project]
if: ${{ !failure() && !cancelled() }} if: ${{ !failure() && !cancelled() }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -79,6 +83,8 @@ jobs:
set-tag: set-tag:
name: Set Tag Name name: Set Tag Name
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
outputs: outputs:
tag_name: ${{ steps.set_tag.outputs.tag_name }} tag_name: ${{ steps.set_tag.outputs.tag_name }}
steps: steps:
@@ -139,6 +145,8 @@ jobs:
needs: [detect-project, test] needs: [detect-project, test]
if: ${{ !failure() && !cancelled() }} if: ${{ !failure() && !cancelled() }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
strategy: strategy:
matrix: matrix:
include: include:
@@ -198,6 +206,8 @@ jobs:
needs.build.result == 'success' && needs.build.result == 'success' &&
github.event_name != 'pull_request' github.event_name != 'pull_request'
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
environment: production environment: production
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@@ -233,10 +243,12 @@ jobs:
tag: tag:
name: Create Tag name: Create Tag
needs: [docker-build, build, set-tag] needs: [docker-build, build, set-tag]
if: | #if: |
github.event_name == 'push' && # github.event_name == 'push' &&
needs.docker-build.result == 'success' # needs.docker-build.result == 'success'
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
with: with:
@@ -257,7 +269,7 @@ jobs:
summary: summary:
name: Workflow Summary name: Workflow Summary
needs: [test, audit, build, docker-build] needs: [test, build, docker-build]
if: always() if: always()
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@@ -19,8 +19,7 @@ nvml-wrapper = "0.11"
nvml-wrapper-sys = "0.9.0" nvml-wrapper-sys = "0.9.0"
anyhow = "1.0.98" anyhow = "1.0.98"
# Docker .env loading regex = "1.11.3"
# config = "0.13"
# Docker API access # Docker API access
bollard = "0.19" bollard = "0.19"

View File

@@ -1,8 +1,22 @@
/// # 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 std::time::Duration;
use crate::hardware::HardwareInfo; use crate::hardware::HardwareInfo;
use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage, Acknowledgment}; use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage, Acknowledgment};
use crate::serverclientcomm::handle_server_message; use crate::docker::serverclientcomm::handle_server_message;
use anyhow::Result; use anyhow::Result;
use reqwest::{Client, StatusCode}; use reqwest::{Client, StatusCode};
@@ -11,6 +25,18 @@ use tokio::time::sleep;
use bollard::Docker; use bollard::Docker;
/// Registers this agent with the backend server and retrieves its server ID and IP address.
///
/// This function collects local hardware information, prepares a registration payload, and sends it to the backend. It will retry registration until successful, handling network errors and server-side failures gracefully.
///
/// # Arguments
/// * `base_url` - The base URL of the backend server (e.g., `https://server.example.com`).
///
/// # Returns
/// * `Result<(i32, String), Box<dyn Error + Send + Sync>>` - Tuple of server ID and registered IP address on success.
///
/// # Errors
/// Returns an error if unable to register after repeated attempts.
pub async fn register_with_server( pub async fn register_with_server(
base_url: &str, base_url: &str,
) -> Result<(i32, String), Box<dyn Error + Send + Sync>> { ) -> Result<(i32, String), Box<dyn Error + Send + Sync>> {
@@ -36,7 +62,7 @@ pub async fn register_with_server(
cpu_type: hardware.cpu.name.clone().unwrap_or_default(), cpu_type: hardware.cpu.name.clone().unwrap_or_default(),
cpu_cores: (hardware.cpu.cores).unwrap_or_default(), cpu_cores: (hardware.cpu.cores).unwrap_or_default(),
gpu_type: hardware.gpu.name.clone().unwrap_or_default(), gpu_type: hardware.gpu.name.clone().unwrap_or_default(),
ram_size: hardware.memory.total.unwrap_or_default(), ram_size: hardware.memory.total_size.unwrap_or_default(),
}; };
// Try to register (will retry on failure) // Try to register (will retry on failure)
@@ -64,6 +90,16 @@ pub async fn register_with_server(
} }
} }
/// Looks up the server ID for the given IP address from the backend server.
///
/// This function will retry until a valid response is received, handling network errors and server-side failures.
///
/// # Arguments
/// * `base_url` - The base URL of the backend server.
/// * `ip` - The local IP address to look up.
///
/// # Returns
/// * `Result<(i32, String), Box<dyn Error + Send + Sync>>` - Tuple of server ID and registered IP address.
async fn get_server_id_by_ip( async fn get_server_id_by_ip(
base_url: &str, base_url: &str,
ip: &str, ip: &str,
@@ -115,6 +151,16 @@ async fn get_server_id_by_ip(
} }
} }
/// Periodically sends heartbeat signals to the backend server to indicate agent liveness.
///
/// This function runs in a background task and will retry on network errors.
///
/// # Arguments
/// * `base_url` - The base URL of the backend server.
/// * `ip` - The IP address of the agent.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if heartbeats are sent successfully.
pub async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
let client = Client::builder() let client = Client::builder()
.danger_accept_invalid_certs(true) .danger_accept_invalid_certs(true)
@@ -138,6 +184,16 @@ pub async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Erro
} }
} }
/// Sends collected hardware and network metrics to the backend server.
///
/// This function is called periodically from the metrics collection loop. It logs the result and retries on network errors.
///
/// # Arguments
/// * `base_url` - The base URL of the backend server.
/// * `metrics` - The metrics data to send (see [`MetricDto`]).
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if metrics are sent successfully.
pub async fn send_metrics( pub async fn send_metrics(
base_url: &str, base_url: &str,
metrics: &MetricDto, metrics: &MetricDto,
@@ -158,6 +214,16 @@ pub async fn send_metrics(
Ok(()) Ok(())
} }
/// Polls the backend server for remote commands and executes them.
///
/// This function runs in a background task, polling the server for new messages. It acknowledges receipt and execution of each command, and handles errors gracefully.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
/// * `base_url` - The base URL of the backend server.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if commands are handled successfully.
pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
let url = format!("{}/api/message", base_url); let url = format!("{}/api/message", base_url);
let client = reqwest::Client::new(); let client = reqwest::Client::new();
@@ -210,6 +276,19 @@ pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(),
} }
} }
/// Sends an acknowledgment to the backend server for a received or executed command message.
///
/// This function is used internally by [`listening_to_server`] to confirm receipt and execution status of commands.
///
/// # Arguments
/// * `client` - Reference to a reqwest HTTP client.
/// * `base_url` - The base URL of the backend server.
/// * `message_id` - The ID of the message being acknowledged.
/// * `status` - Status string (e.g., "received", "success", "error").
/// * `details` - Additional details about the acknowledgment.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if acknowledgment is sent successfully.
async fn send_acknowledgment( async fn send_acknowledgment(
client: &reqwest::Client, client: &reqwest::Client,
base_url: &str, base_url: &str,

View File

@@ -0,0 +1,93 @@
//! Docker container utilities for WatcherAgent
//!
//! Provides functions to list and process Docker containers using the Bollard library.
//!
use crate::models::DockerContainer;
use bollard::query_parameters::{ListContainersOptions};
use bollard::Docker;
/// Returns a list of available Docker containers.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
///
/// # Returns
/// * `Vec<DockerContainer>` - Vector of Docker container info.
pub async fn get_available_container(docker: &Docker) -> Vec<DockerContainer> {
println!("=== DOCKER CONTAINER LIST ===");
let options = Some(ListContainersOptions {
all: true,
..Default::default()
});
let containers_list = match docker.list_containers(options).await {
Ok(containers) => {
println!("Available containers ({}):", containers.len());
containers.into_iter()
.filter_map(|container| {
container.id.as_ref()?; // Skip if no ID
let id = container.id?;
let short_id = if id.len() > 12 { &id[..12] } else { &id };
//let short_id: u32 = short_string_id.trim().parse().unwrap();
let name = container.names
.and_then(|names| names.into_iter().next())
.map(|name| name.trim_start_matches('/').to_string())
.unwrap_or_else(|| "unknown".to_string());
let image = container.image
.as_ref()
.map(|img| img.to_string())
.unwrap_or_else(|| "unknown".to_string());
let status = container.status
.as_ref()
.map(|s| match s.to_lowercase().as_str() {
s if s.contains("up") || s.contains("running") => "running".to_string(),
s if s.contains("exited") || s.contains("stopped") => "stopped".to_string(),
_ => s.to_string(),
})
.unwrap_or_else(|| "unknown".to_string());
println!(" - ID: {}, Image: {}, Name: {}", short_id, container.image.unwrap(), name);
Some(DockerContainer {
ID: short_id.to_string(),
image,
Name: name,
Status: status,
_net_in: 0.0,
_net_out: 0.0,
_cpu_load: 0.0,
})
})
.collect()
}
Err(e) => {
eprintln!("Failed to list containers: {}", e);
Vec::new()
}
};
containers_list
}
/*
/// Extracts a Docker container ID from a string line.
///
/// # Arguments
/// * `line` - The input string containing a container ID or related info.
///
/// # Returns
/// * `Option<String>` - The extracted container ID if found.
pub fn extract_client_container_id(line: &str) -> Option<String> {
// ...existing code...
}
*/

View File

@@ -0,0 +1,80 @@
/// # 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;
use std::error::Error;
use crate::models::DockerContainer;
/// Aggregated Docker statistics for all managed containers.
///
/// # Fields
/// - `number`: Number of running containers (optional)
/// - `net_in_total`: Total network receive rate in **bytes per second (B/s)** (optional)
/// - `net_out_total`: Total network transmit rate in **bytes per second (B/s)** (optional)
/// - `dockers`: List of [`DockerContainer`] statistics (optional)
#[derive(Debug, Clone)]
pub struct DockerInfo {
pub number: Option<u16>,
pub net_in_total: Option<f64>,
pub net_out_total: Option<f64>,
pub dockers: Option<Vec<DockerContainer>>,
}
impl DockerInfo {
/// Collects Docker statistics for all managed containers.
///
/// # Returns
/// * `Result<DockerInfo, Box<dyn Error + Send + Sync>>` - Aggregated Docker statistics or error if collection fails.
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
Ok(Self { number: None, net_in_total: None, net_out_total: None, dockers: None })
}
}
impl DockerContainer {
/*
/// Restarts the specified Docker container by ID.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if restarted successfully, error otherwise.
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
// ...existing code...
}
*/
/// Returns the container ID for a given [`DockerContainer`].
///
/// # Arguments
/// * `container` - Reference to a [`DockerContainer`]
///
/// # Returns
/// * `Result<u32, Box<dyn Error + Send + Sync>>` - Container ID as integer.
pub async fn get_docker_container_id(container: DockerContainer) -> Result<String, Box<dyn Error + Send + Sync>> {
Ok(container.ID)
}
/// Returns the image name for a given [`DockerContainer`].
///
/// # Arguments
/// * `container` - Reference to a [`DockerContainer`]
///
/// # Returns
/// * `Result<String, Box<dyn Error + Send + Sync>>` - Image name as string.
pub async fn get_docker_container_image(container: DockerContainer) -> Result<String, Box<dyn Error + Send + Sync>> {
Ok(container.image)
}
}

View File

@@ -1,11 +1,24 @@
use crate::models::{ServerMessage};
//! Server-client communication utilities for WatcherAgent
//!
//! Handles server commands, Docker image updates, and container management using the Bollard library.
//!
use crate::models::{DockerContainer, ServerMessage};
use crate::docker::container::{get_available_container};
use std::error::Error; use std::error::Error;
use bollard::Docker; use bollard::Docker;
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions}; use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions};
use futures_util::StreamExt; use futures_util::StreamExt;
/// Handles a message from the backend server and dispatches the appropriate action.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
/// * `msg` - The server message to handle.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if handled successfully, error otherwise.
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
let msg = msg.clone(); let msg = msg.clone();
println!("Handling server message: {:?}", msg); println!("Handling server message: {:?}", msg);
@@ -40,6 +53,14 @@ pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Resul
} }
} }
/// Pulls a new Docker image and restarts the current container.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
/// * `image` - The name of the Docker image to pull.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if updated successfully, error otherwise.
pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
println!("Updating to {}", image); println!("Updating to {}", image);
@@ -74,50 +95,37 @@ pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box
Ok(()) Ok(())
} }
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> { /// Finds the Docker container running the agent by image name.
// Get the current container ID from /proc/self/cgroup ///
let container_id = match std::fs::read_to_string("/proc/self/cgroup") { /// # Arguments
Ok(content) => { /// * `docker` - Reference to a Bollard Docker client.
content ///
.lines() /// # Returns
.find_map(|line| { /// * `Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>>` - The agent's container info if found.
if line.contains("docker") { pub async fn get_client_container(docker: &Docker) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
line.split('/').last().map(|s| s.trim().to_string()) let containers = get_available_container(docker).await;
} else { let client_image = "watcher-agent";
None
} // Find container with the specific image
}) if let Some(container) = containers.iter().find(|c| c.image == client_image) {
} Ok(Some(container.clone()))
Err(e) => { } else {
eprintln!("Error reading cgroup file: {}", e); Ok(None)
return Ok(None);
}
};
let container_id = match container_id {
Some(id) => id,
None => {
eprintln!("Could not find container ID in cgroup");
return Ok(None);
}
};
// Inspect the current container to get its image
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await {
Ok(container_info) => {
Ok(container_info.config.map(|config| config.image.unwrap_or_else(|| "unknown".to_string())))
}
Err(e) => {
eprintln!("Error inspecting container: {}", e);
Ok(None)
}
} }
} }
/// Restarts the agent's own Docker container.
///
/// # Arguments
/// * `docker` - Reference to a Bollard Docker client.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if restarted successfully, error otherwise.
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Ok(container_id) = std::env::var("HOSTNAME") { if let Ok(Some(container)) = get_client_container(docker).await {
let container_id = container.clone().ID;
println!("Restarting container {}", container_id); println!("Restarting container {}", container_id);
if let Err(e) = docker.restart_container(&container_id, Some(RestartContainerOptions { signal: None, t: Some(0) })) if let Err(e) = docker.restart_container(&container_id.to_string(), Some(RestartContainerOptions { signal: None, t: Some(0) }))
.await .await
{ {
eprintln!("Failed to restart container: {}", e); eprintln!("Failed to restart container: {}", e);

View File

@@ -2,6 +2,29 @@ use anyhow::Result;
use std::error::Error; use std::error::Error;
use sysinfo::System; 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.0100.0**)
/// - `current_temp`: CPU temperature in **degrees Celsius (°C)**
/// - `uptime`: System uptime in **seconds (s)**
///
/// CPU statistics for the host system.
///
/// # Fields
/// - `name`: CPU model name (string)
/// - `cores`: Number of physical CPU cores (integer)
/// - `current_load`: CPU usage as a percentage (**0.0100.0**)
/// - `current_temp`: CPU temperature in **degrees Celsius (°C)**
/// - `uptime`: System uptime in **seconds (s)**
/// - `host_name`: Hostname of the system (string)
#[derive(Debug)] #[derive(Debug)]
pub struct CpuInfo { pub struct CpuInfo {
pub name: Option<String>, pub name: Option<String>,
@@ -12,6 +35,10 @@ pub struct CpuInfo {
pub host_name: Option<String>, pub host_name: Option<String>,
} }
/// Collects CPU information (model, cores, load, temperature, uptime).
///
/// # Returns
/// * `Result<CpuInfo, Box<dyn Error + Send + Sync>>` - CPU statistics or error if unavailable.
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> { pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
let mut sys = System::new_all(); let mut sys = System::new_all();
@@ -33,12 +60,23 @@ pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
}) })
} }
/// Queries system for current CPU load (percentage).
///
/// # Arguments
/// * `sys` - Mutable reference to sysinfo::System
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU load as percentage.
pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> { pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_cpu_all(); sys.refresh_cpu_all();
tokio::task::yield_now().await; // Allow other tasks to run tokio::task::yield_now().await; // Allow other tasks to run
Ok(sys.global_cpu_usage() as f64) Ok(sys.global_cpu_usage() as f64)
} }
/// Attempts to read CPU temperature from system sensors (Linux only).
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU temperature in degrees Celsius (°C).
pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error + Send + Sync>> { pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error + Send + Sync>> {
println!("Attempting to get CPU temperature..."); println!("Attempting to get CPU temperature...");

View File

@@ -7,6 +7,27 @@ use sysinfo::{Component, Components, Disk, Disks};
use serde::Serialize; 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)**.
///
/// Summary of disk statistics for the system.
///
/// # Fields
/// - `total_size`: Total disk size in bytes (all disks > 100MB)
/// - `total_used`: Total used disk space in bytes
/// - `total_available`: Total available disk space in bytes
/// - `total_usage`: Usage percentage (0.0100.0)
/// - `detailed_info`: Vector of [`DiskInfoDetailed`] for each disk
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct DiskInfo { pub struct DiskInfo {
pub total_size: Option<f64>, pub total_size: Option<f64>,
@@ -16,6 +37,12 @@ pub struct DiskInfo {
pub detailed_info: Vec<DiskInfoDetailed>, pub detailed_info: Vec<DiskInfoDetailed>,
} }
/// Collects disk information for all detected disks, including usage and temperature.
///
/// This function enumerates all disks, calculates usage statistics, and attempts to associate temperature sensors with disk components.
///
/// # Returns
/// * `Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>>` - Disk statistics and details, or error if collection fails.
pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>> { pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>> {
let disks = Disks::new_with_refreshed_list(); let disks = Disks::new_with_refreshed_list();
let mut detailed_info = Vec::new(); let mut detailed_info = Vec::new();
@@ -37,28 +64,12 @@ pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Sen
component_disk_label: String::new(), component_disk_label: String::new(),
component_disk_temperature: 0.0, component_disk_temperature: 0.0,
}); });
println!(
"Disk_Name: {:?}:\n---- Disk_Kind: {},\n---- Total: {},\n---- Available: {},\n---- Used: {}, \n---- Mount_Point: {:?}",
disk.name(),
disk.kind(),
disk.total_space(),
disk.available_space(),
disk_used,
disk.mount_point()
);
} }
// Get component temperatures // Get component temperatures
let components = Components::new_with_refreshed_list(); let components = Components::new_with_refreshed_list();
for component in &components { for component in &components {
if let Some(temperature) = component.temperature() { if let Some(temperature) = component.temperature() {
println!(
"Component_Label: {}, Temperature: {}°C",
component.label(),
temperature
);
// Update detailed info with temperature data if it matches a disk component // Update detailed info with temperature data if it matches a disk component
for disk_info in &mut detailed_info { for disk_info in &mut detailed_info {
if component.label().contains(&disk_info.disk_name) { if component.label().contains(&disk_info.disk_name) {

View File

@@ -2,6 +2,29 @@ use anyhow::Result;
use nvml_wrapper::Nvml; use nvml_wrapper::Nvml;
use std::error::Error; 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.0100.0**)
/// - `current_temp`: GPU temperature in **degrees Celsius (°C)**
/// - `vram_total`: Total VRAM in **bytes**
/// - `vram_used`: Used VRAM in **bytes**
///
/// GPU statistics for the host system.
///
/// # Fields
/// - `name`: GPU model name (string)
/// - `current_load`: GPU usage as a percentage (**0.0100.0**)
/// - `current_temp`: GPU temperature in **degrees Celsius (°C)**
/// - `vram_total`: Total VRAM in **bytes**
/// - `vram_used`: Used VRAM in **bytes**
#[derive(Debug)] #[derive(Debug)]
pub struct GpuInfo { pub struct GpuInfo {
pub name: Option<String>, pub name: Option<String>,
@@ -11,6 +34,12 @@ pub struct GpuInfo {
pub vram_used: Option<f64>, pub vram_used: Option<f64>,
} }
/// Collects GPU information (load, temperature, VRAM) using NVML.
///
/// This function attempts to query the first NVIDIA GPU using NVML. If unavailable, it returns a fallback with only the detected GPU name.
///
/// # Returns
/// * `Result<GpuInfo, Box<dyn Error + Send + Sync>>` - GPU statistics or fallback if unavailable.
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> { pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
match get_gpu_metrics() { match get_gpu_metrics() {
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => { Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
@@ -37,6 +66,10 @@ pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
} }
} }
/// Queries NVML for GPU metrics: temperature, load, VRAM used/total.
///
/// # Returns
/// * `Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>>` - Tuple of (temperature °C, load %, VRAM used bytes, VRAM total bytes).
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> { pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
let nvml = Nvml::init(); let nvml = Nvml::init();
if let Ok(nvml) = nvml { if let Ok(nvml) = nvml {

View File

@@ -3,25 +3,56 @@ use std::error::Error;
use anyhow::Result; use anyhow::Result;
use sysinfo::System; 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 **bytes**
///
/// Memory statistics for the host system.
///
/// # Fields
/// - `total`: Total RAM in **bytes**
/// - `used`: Used RAM in **bytes**
/// - `free`: Free RAM in **bytes**
#[derive(Debug)] #[derive(Debug)]
pub struct MemoryInfo { pub struct MemoryInfo {
pub total: Option<f64>, pub total_size: Option<f64>,
pub used: Option<f64>, pub used: Option<f64>,
pub free: Option<f64>, pub free: Option<f64>,
pub current_load: Option<f64>,
} }
pub async fn get_memory_info() -> Result<MemoryInfo> { /// Collects memory information (total, used, free RAM).
///
/// # Returns
/// * `Result<MemoryInfo>` - Memory statistics or error if unavailable.
pub async fn get_memory_info() -> Result<MemoryInfo, Box<dyn Error + Send + Sync>> {
let mut sys = System::new(); let mut sys = System::new();
sys.refresh_memory(); sys.refresh_memory();
Ok(MemoryInfo { Ok(MemoryInfo {
total: Some(sys.total_memory() as f64), total_size: Some(sys.total_memory() as f64),
used: Some(sys.used_memory() as f64), used: Some(sys.used_memory() as f64),
free: Some(sys.free_memory() as f64), free: Some(sys.free_memory() as f64),
current_load: Some(get_memory_usage(&mut sys).unwrap() as f64)
}) })
} }
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> { /// Computes memory usage percentage from sysinfo::System.
///
/// # Arguments
/// * `sys` - Mutable reference to sysinfo::System
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - Memory usage as percentage.
pub fn get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_memory(); sys.refresh_memory();
Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0) Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0)
} }

View File

@@ -14,6 +14,23 @@ pub use memory::get_memory_info;
pub use network::get_network_info; pub use network::get_network_info;
pub use network::NetworkMonitor; 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.
///
/// Aggregated hardware statistics for the host system.
///
/// # Fields
/// - `cpu`: CPU statistics (see [`CpuInfo`])
/// - `gpu`: GPU statistics (see [`GpuInfo`])
/// - `memory`: Memory statistics (see [`MemoryInfo`])
/// - `disk`: Disk statistics (see [`DiskInfo`])
/// - `network`: Network statistics (see [`NetworkInfo`])
/// - `network_monitor`: Rolling monitor for network bandwidth
#[derive(Debug)] #[derive(Debug)]
pub struct HardwareInfo { pub struct HardwareInfo {
pub cpu: cpu::CpuInfo, pub cpu: cpu::CpuInfo,
@@ -25,6 +42,10 @@ pub struct HardwareInfo {
} }
impl HardwareInfo { impl HardwareInfo {
/// Collects all hardware statistics asynchronously.
///
/// # Returns
/// * `Result<HardwareInfo, Box<dyn Error + Send + Sync>>` - Aggregated hardware statistics or error if any subsystem fails.
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> { pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
let mut network_monitor = network::NetworkMonitor::new(); let mut network_monitor = network::NetworkMonitor::new();
Ok(Self { Ok(Self {

View File

@@ -2,6 +2,24 @@ use std::error::Error;
use std::result::Result; use std::result::Result;
use std::time::Instant; 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 statistics for the host system.
///
/// # Fields
/// - `interfaces`: List of network interface names (strings)
/// - `rx_rate`: Receive bandwidth in **bytes per second (B/s)**
/// - `tx_rate`: Transmit bandwidth in **bytes per second (B/s)**
#[derive(Debug)] #[derive(Debug)]
pub struct NetworkInfo { pub struct NetworkInfo {
pub interfaces: Option<Vec<String>>, pub interfaces: Option<Vec<String>>,
@@ -9,6 +27,13 @@ pub struct NetworkInfo {
pub tx_rate: Option<f64>, pub tx_rate: Option<f64>,
} }
/// Rolling monitor for network bandwidth statistics.
///
/// # Fields
/// - `prev_rx`: Previous received bytes
/// - `prev_tx`: Previous transmitted bytes
/// - `last_update`: Timestamp of last update
#[derive(Debug)] #[derive(Debug)]
pub struct NetworkMonitor { pub struct NetworkMonitor {
prev_rx: u64, prev_rx: u64,
@@ -23,6 +48,7 @@ impl Default for NetworkMonitor {
} }
impl NetworkMonitor { impl NetworkMonitor {
/// Creates a new `NetworkMonitor` for bandwidth tracking.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
prev_rx: 0, prev_rx: 0,
@@ -31,6 +57,10 @@ impl NetworkMonitor {
} }
} }
/// Updates the network usage statistics and returns current rx/tx rates.
///
/// # Returns
/// * `Result<(f64, f64), Box<dyn Error>>` - Tuple of (rx_rate, tx_rate) in bytes per second.
pub fn update_usage(&mut self) -> Result<(f64, f64), Box<dyn Error>> { pub fn update_usage(&mut self) -> Result<(f64, f64), Box<dyn Error>> {
let (current_rx, current_tx) = get_network_bytes()?; let (current_rx, current_tx) = get_network_bytes()?;
let elapsed = self.last_update.elapsed().as_secs_f64(); let elapsed = self.last_update.elapsed().as_secs_f64();
@@ -55,6 +85,13 @@ impl NetworkMonitor {
} }
} }
/// Collects network information (interfaces, rx/tx rates) using a monitor.
///
/// # Arguments
/// * `monitor` - Mutable reference to a `NetworkMonitor`
///
/// # Returns
/// * `Result<NetworkInfo, Box<dyn Error>>` - Network statistics or error if unavailable.
pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result<NetworkInfo, Box<dyn Error>> { pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result<NetworkInfo, Box<dyn Error>> {
let (rx_rate, tx_rate) = monitor.update_usage()?; let (rx_rate, tx_rate) = monitor.update_usage()?;
Ok(NetworkInfo { Ok(NetworkInfo {

View File

@@ -1,22 +1,57 @@
/// WatcherAgent - A Rust-based system monitoring agent
/// This agent collects hardware metrics and sends them to a backend server. /// # WatcherAgent
/// It supports CPU, GPU, RAM, disk, and network metrics. ///
/// **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 api;
pub mod hardware; pub mod hardware;
pub mod metrics; pub mod metrics;
pub mod models; pub mod models;
pub mod serverclientcomm; 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 tokio::task::JoinHandle;
use bollard::Docker; use bollard::Docker;
use std::env;
use std::error::Error;
use crate::serverclientcomm::{get_current_image};
/// 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>( async fn flatten<T>(
handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>, handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>,
) -> Result<T, Box<dyn Error + Send + Sync>> { ) -> Result<T, Box<dyn Error + Send + Sync>> {
@@ -27,6 +62,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] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// Initialize Docker client // Initialize Docker client
@@ -34,8 +87,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.map_err(|e| format!("Failed to connect to Docker: {}", e))?; .map_err(|e| format!("Failed to connect to Docker: {}", e))?;
// Get current image version // Get current image version
let client_version = match get_current_image(&docker).await { let client_version = match docker::serverclientcomm::get_client_container(&docker).await {
Ok(Some(version)) => version, Ok(Some(version)) => version.image,
Ok(None) => { Ok(None) => {
eprintln!("Warning: No image version found"); eprintln!("Warning: No image version found");
"unknown".to_string() "unknown".to_string()
@@ -56,7 +109,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let server_url = &args[1]; let server_url = &args[1];
println!("Server URL: {:?}", server_url); println!("Server URL: {:?}", server_url);
// Registration // Registration with backend server
let (server_id, ip) = match api::register_with_server(&server_url).await { let (server_id, ip) = match api::register_with_server(&server_url).await {
Ok((id, ip)) => (id, ip), Ok((id, ip)) => (id, ip),
Err(e) => { Err(e) => {

View File

@@ -1,3 +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.
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
@@ -6,13 +19,31 @@ use crate::hardware::network::NetworkMonitor;
use crate::hardware::HardwareInfo; use crate::hardware::HardwareInfo;
use crate::models::MetricDto; use crate::models::MetricDto;
/// Main orchestrator for hardware and network metric collection and reporting.
///
/// The `Collector` struct manages the state required to collect metrics and send them to the backend server. It maintains a network monitor for bandwidth tracking, the agent's server ID, and its IP address.
///
/// # Fields
/// - `network_monitor`: Tracks network usage rates (rx/tx).
/// - `server_id`: Unique server ID assigned by the backend.
/// - `ip_address`: IP address of the agent.
pub struct Collector { pub struct Collector {
network_monitor: NetworkMonitor, network_monitor: NetworkMonitor,
server_id: i32, server_id: i32,
ip_address: String, ip_address: String,
} }
impl Collector { impl Collector {
/// Creates a new `Collector` instance for metric collection and reporting.
///
/// # Arguments
/// * `server_id` - The server ID assigned by the backend.
/// * `ip_address` - The IP address of the agent.
///
/// # Returns
/// A new `Collector` ready to collect and report metrics.
pub fn new(server_id: i32, ip_address: String) -> Self { pub fn new(server_id: i32, ip_address: String) -> Self {
Self { Self {
network_monitor: NetworkMonitor::new(), network_monitor: NetworkMonitor::new(),
@@ -21,6 +52,15 @@ impl Collector {
} }
} }
/// Runs the main metrics collection loop, periodically sending metrics to the backend server.
///
/// This function continuously collects hardware and network metrics, sends them to the backend, and handles errors gracefully. It uses a configurable interval and retries on failures.
///
/// # Arguments
/// * `base_url` - The base URL of the backend server.
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if metrics are sent successfully.
pub async fn run(&mut self, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn run(&mut self, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
loop { loop {
println!( println!(
@@ -40,6 +80,12 @@ impl Collector {
} }
} }
/// Collects hardware and network metrics from all subsystems.
///
/// This function queries the hardware module for CPU, GPU, RAM, disk, and network statistics, and packages them into a [`MetricDto`] for reporting.
///
/// # Returns
/// * `Result<MetricDto, Box<dyn Error + Send + Sync>>` - The collected metrics or an error if hardware info is unavailable.
pub async fn collect(&mut self) -> Result<MetricDto, Box<dyn Error + Send + Sync>> { pub async fn collect(&mut self) -> Result<MetricDto, Box<dyn Error + Send + Sync>> {
let hardware = match HardwareInfo::collect().await { let hardware = match HardwareInfo::collect().await {
Ok(hw) => hw, Ok(hw) => hw,
@@ -59,11 +105,11 @@ impl Collector {
gpu_load: hardware.gpu.current_load.unwrap_or_default(), gpu_load: hardware.gpu.current_load.unwrap_or_default(),
gpu_temp: hardware.gpu.current_temp.unwrap_or_default(), gpu_temp: hardware.gpu.current_temp.unwrap_or_default(),
gpu_vram_size: hardware.gpu.vram_total.unwrap_or_default(), gpu_vram_size: hardware.gpu.vram_total.unwrap_or_default(),
gpu_vram_usage: hardware.gpu.vram_used.unwrap_or_default(), gpu_vram_load: hardware.gpu.current_load.unwrap_or_default(),
ram_load: hardware.memory.used.unwrap_or_default(), ram_load: hardware.memory.current_load.unwrap_or_default(),
ram_size: hardware.memory.total.unwrap_or_default(), ram_size: hardware.memory.total_size.unwrap_or_default(),
disk_size: hardware.disk.total_size.unwrap_or_default(), disk_size: hardware.disk.total_size.unwrap_or_default(),
disk_usage: hardware.disk.total_used.unwrap_or_default(), disk_usage: hardware.disk.total_usage.unwrap_or_default(),
disk_temp: 0.0, // not supported disk_temp: 0.0, // not supported
net_rx: hardware.network.rx_rate.unwrap_or_default(), net_rx: hardware.network.rx_rate.unwrap_or_default(),
net_tx: hardware.network.tx_rate.unwrap_or_default(), net_tx: hardware.network.tx_rate.unwrap_or_default(),

View File

@@ -1,6 +1,27 @@
/// # 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}; use serde::{Deserialize, Serialize};
// Data structures matching the C# DTOs /// Registration data sent to the backend server.
///
/// ## Units
/// - `id`: Unique server identifier (integer)
/// - `ip_address`: IPv4 or IPv6 address (string)
/// - `cpu_type`: CPU model name (string)
/// - `cpu_cores`: Number of physical CPU cores (integer)
/// - `gpu_type`: GPU model name (string)
/// - `ram_size`: Total RAM size in **megabytes (MB)**
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct RegistrationDto { pub struct RegistrationDto {
#[serde(rename = "id")] #[serde(rename = "id")]
@@ -17,6 +38,24 @@ pub struct RegistrationDto {
pub ram_size: f64, pub ram_size: f64,
} }
/// Hardware and network metrics data sent to the backend server.
///
/// ## Units
/// - `server_id`: Unique server identifier (integer)
/// - `ip_address`: IPv4 or IPv6 address (string)
/// - `cpu_load`: CPU usage as a percentage (**0.0100.0**)
/// - `cpu_temp`: CPU temperature in **degrees Celsius (°C)**
/// - `gpu_load`: GPU usage as a percentage (**0.0100.0**)
/// - `gpu_temp`: GPU temperature in **degrees Celsius (°C)**
/// - `gpu_vram_size`: Total GPU VRAM in **bytes**
/// - `gpu_vram_load`: GPU Usage of VRAM as a percentage (**0.0100.0**)
/// - `ram_load`: RAM usage as a percentage (**0.0100.0**)
/// - `ram_size`: Total RAM in **bytes**
/// - `disk_size`: Total disk size in **bytes**
/// - `disk_usage`: Used disk space in **bytes**
/// - `disk_temp`: Disk temperature in **degrees Celsius (°C)** (if available)
/// - `net_rx`: Network receive rate in **bytes per second (B/s)**
/// - `net_tx`: Network transmit rate in **bytes per second (B/s)**
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct MetricDto { pub struct MetricDto {
#[serde(rename = "serverId")] #[serde(rename = "serverId")]
@@ -33,8 +72,8 @@ pub struct MetricDto {
pub gpu_temp: f64, pub gpu_temp: f64,
#[serde(rename = "gpu_Vram_Size")] #[serde(rename = "gpu_Vram_Size")]
pub gpu_vram_size: f64, pub gpu_vram_size: f64,
#[serde(rename = "gpu_Vram_Usage")] #[serde(rename = "gpu_Vram_Load")]
pub gpu_vram_usage: f64, pub gpu_vram_load: f64,
#[serde(rename = "ram_Load")] #[serde(rename = "ram_Load")]
pub ram_load: f64, pub ram_load: f64,
#[serde(rename = "ram_Size")] #[serde(rename = "ram_Size")]
@@ -51,6 +90,13 @@ pub struct MetricDto {
pub net_tx: f64, pub net_tx: f64,
} }
/// Detailed disk information for each detected disk.
///
/// ## Units
/// - `disk_total_space`: Total disk space in **bytes**
/// - `disk_available_space`: Available disk space in **bytes**
/// - `disk_used_space`: Used disk space in **bytes**
/// - `component_disk_temperature`: Disk temperature in **degrees Celsius (°C)**
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct DiskInfoDetailed { pub struct DiskInfoDetailed {
pub disk_name: String, pub disk_name: String,
@@ -63,6 +109,11 @@ pub struct DiskInfoDetailed {
pub component_disk_temperature: f32, pub component_disk_temperature: f32,
} }
/// Response containing server ID and IP address.
///
/// ## Units
/// - `id`: Unique server identifier (integer)
/// - `ip_address`: IPv4 or IPv6 address (string)
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct IdResponse { pub struct IdResponse {
pub id: i32, pub id: i32,
@@ -70,12 +121,24 @@ pub struct IdResponse {
pub ip_address: String, pub ip_address: String,
} }
/// Heartbeat message data sent to the backend server.
///
/// ## Units
/// - `ip_address`: IPv4 or IPv6 address (string)
#[derive(Serialize)] #[derive(Serialize)]
pub struct HeartbeatDto { pub struct HeartbeatDto {
#[serde(rename = "IpAddress")] #[serde(rename = "IpAddress")]
pub ip_address: String, pub ip_address: String,
} }
/// Hardware summary data for diagnostics and registration.
///
/// ## Units
/// - `cpu_type`: CPU model name (string)
/// - `cpu_cores`: Number of physical CPU cores (integer)
/// - `gpu_type`: GPU model name (string)
/// - `ram_size`: Total RAM size in **megabytes (MB)**
/// - `ip_address`: IPv4 or IPv6 address (string)
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct HardwareDto { pub struct HardwareDto {
pub cpu_type: String, pub cpu_type: String,
@@ -85,6 +148,12 @@ pub struct HardwareDto {
pub ip_address: String, pub ip_address: String,
} }
/// Command message received from the backend server.
///
/// ## Fields
/// - `message_type`: Type of command (e.g., "update_image", "restart_container", "stop_agent")
/// - `data`: Command payload (arbitrary JSON)
/// - `message_id`: Unique identifier for acknowledgment
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct ServerMessage { pub struct ServerMessage {
// Define your message structure here // Define your message structure here
@@ -93,9 +162,36 @@ pub struct ServerMessage {
pub message_id: String, // Add an ID for acknowledgment pub message_id: String, // Add an ID for acknowledgment
} }
/// Acknowledgment payload sent to the backend server for command messages.
///
/// ## Fields
/// - `message_id`: Unique identifier of the acknowledged message
/// - `status`: Status string ("success", "error", etc.)
/// - `details`: Additional details or error messages
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]
pub struct Acknowledgment { pub struct Acknowledgment {
pub message_id: String, pub message_id: String,
pub status: String, // "success" or "error" pub status: String, // "success" or "error"
pub details: String, pub details: String,
}
/// Docker container information for agent and managed containers.
///
/// ## Fields
/// - `ID`: Container ID (first 12 hex digits, integer)
/// - `image`: Docker image name (string)
/// - `Name`: Container name (string)
/// - `Status`: Container status ("running", "stopped", etc.)
/// - `_net_in`: Network receive rate in **bytes per second (B/s)**
/// - `_net_out`: Network transmit rate in **bytes per second (B/s)**
/// - `_cpu_load`: CPU usage as a percentage (**0.0100.0**)
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainer {
pub ID: String,
pub image: String,
pub Name: String,
pub Status: String, // "running";"stopped";others
pub _net_in: f64,
pub _net_out: f64,
pub _cpu_load: f64,
} }