Compare commits

..

3 Commits

Author SHA1 Message Date
4d8b5c39e5 docker stats nightmare
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m17s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m30s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 4m22s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m24s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-10-30 13:33:47 +01:00
4e28d57460 added documentation to /docker/stats mod.rs 2025-10-30 09:57:24 +01:00
6405ab16e4 added documentation to /docker mod.rs 2025-10-30 09:52:08 +01:00
8 changed files with 240 additions and 48 deletions

View File

@@ -424,6 +424,29 @@ pub async fn send_acknowledgment(
Ok(())
}
/// Sends Docker container metrics to the backend monitoring endpoint.
///
/// This function asynchronously transmits Docker container statistics including
/// CPU usage, memory consumption, network I/O, and container status to the
/// backend server for monitoring and analysis.
///
/// # Arguments
///
/// * `base_url` - The base URL of the backend server (e.g., "http://localhost:8080")
/// * `docker_metrics` - Reference to a [`DockerMetricDto`] containing container metrics data
///
/// # Returns
///
/// * `Ok(())` - If the HTTP request was successfully sent (regardless of HTTP status code)
/// * `Err(Box<dyn Error + Send + Sync>)` - If JSON serialization fails or other errors occur
///
/// # Behavior
///
/// - Constructs the full endpoint URL: `{base_url}/monitoring/docker-metric`
/// - Serializes the metrics data to pretty JSON for debugging output
/// - Sends a POST request with JSON content-type
/// - Logs success/failure messages to stdout/stderr
/// - Always returns `Ok(())` after request attempt (does not validate HTTP response status)
pub async fn send_docker_metrics(
base_url: &str,
docker_metrics: &DockerMetricDto,
@@ -431,7 +454,7 @@ pub async fn send_docker_metrics(
let client = Client::new();
let url = format!("{}/monitoring/docker-metric", base_url);
println!("Docker Metrics: {}", serde_json::to_string_pretty(&docker_metrics)?);
println!("📤 JSON being posted:\n{}", serde_json::to_string_pretty(&docker_metrics)?);
match client.post(&url).json(&docker_metrics).send().await {
Ok(res) => println!(

View File

@@ -154,19 +154,6 @@ pub async fn restart_container(
Ok(())
}
/*
/// 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...
}
*/
/// Gets network statistics for a specific container
pub async fn get_network_stats(
docker: &Docker,

View File

@@ -35,7 +35,15 @@ impl Default for DockerManager {
}
impl DockerManager {
/// Creates a new DockerManager instance
/// Creates a new DockerManager instance with Docker connection
///
/// Establishes a connection to the local Docker daemon using default connection settings.
/// This is the primary constructor for the Docker manager.
///
/// # Returns
///
/// * `Ok(Self)` - Successfully connected Docker manager instance
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker daemon is not running or connection fails
pub fn new() -> Result<Self, Box<dyn Error + Send + Sync>> {
let docker = Docker::connect_with_local_defaults()
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
@@ -44,13 +52,31 @@ impl DockerManager {
}
/// Creates a DockerManager instance with optional Docker connection
///
/// Attempts to connect to Docker but returns None instead of an error if connection fails.
/// Useful for scenarios where Docker availability is optional.
///
/// # Returns
///
/// * `Some(Self)` - If Docker connection is successful
/// * `None` - If Docker connection fails or Docker daemon is not available
pub fn new_optional() -> Option<Self> {
Docker::connect_with_local_defaults()
.map(|docker| Self { docker })
.ok()
}
/// Finds the Docker container running the agent by image name
/// Finds the Docker container running the WatcherAgent by image name
///
/// Searches through all available containers to find the one running the agent.
/// Matches containers whose image name contains "watcher-agent".
///
/// # Returns
///
/// * `Ok(Some(DockerContainer))` - Agent container found with full container details
/// * `Ok(None)` - No agent container found (agent may not be running in Docker)
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
pub async fn get_client_container(
&self,
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
@@ -67,7 +93,14 @@ impl DockerManager {
}))
}
/// Gets the current client version (image name) if running in Docker
/// Gets the current client version from Docker image name
///
/// Extracts the image name (without tag) from the agent's container.
/// Returns "unknown" if the agent is not running in Docker or if version cannot be determined.
///
/// # Returns
///
/// * `String` - Image name portion before ':' tag, or "unknown" if not determinable
pub async fn get_client_version(&self) -> String {
match self.get_client_container().await {
Ok(Some(container)) => container
@@ -90,6 +123,13 @@ impl DockerManager {
}
/// Checks if Docker is available and the agent is running in a container
///
/// Determines whether the agent is operating within a Docker container environment.
/// This is used to enable/disable Docker-specific functionality.
///
/// # Returns
///
/// * `bool` - True if agent is running in Docker container, false otherwise
pub async fn is_dockerized(&self) -> bool {
self.get_client_container()
.await
@@ -97,7 +137,15 @@ impl DockerManager {
.unwrap_or(false)
}
/// Gets all available containers as DTOs for registration
/// Gets all available Docker containers as DTOs
///
/// Retrieves a list of all containers (running and stopped) and converts them
/// to simplified DTOs suitable for registration with the backend server.
///
/// # Returns
///
/// * `Ok(Vec<DockerContainer>)` - List of all containers with id, image, and name
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
pub async fn get_containers(
&self,
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
@@ -114,12 +162,32 @@ impl DockerManager {
}
/// Gets the number of running containers
///
/// Counts all containers that are currently available (both running and stopped).
/// This provides a quick overview of container density on the host.
///
/// # Returns
///
/// * `Ok(usize)` - Total number of containers
/// * `Err(Box<dyn Error + Send + Sync>)` - If container counting fails
pub async fn get_container_count(&self) -> Result<usize, Box<dyn Error + Send + Sync>> {
let containers = container::get_available_containers(&self.docker).await;
Ok(containers.len())
}
/// Restarts a specific container by ID
///
/// Initiates a restart of the specified container. This is typically used
/// in response to backend commands for container management.
///
/// # Arguments
///
/// * `container_id` - The full or partial container ID to restart
///
/// # Returns
///
/// * `Ok(())` - Container restart successfully initiated
/// * `Err(Box<dyn Error + Send + Sync>)` - If container not found or restart fails
pub async fn restart_container(
&self,
container_id: &str,
@@ -127,7 +195,16 @@ impl DockerManager {
container::restart_container(&self.docker, container_id).await
}
/// Collects Docker metrics for all containers
/// Collects comprehensive Docker metrics for all containers
///
/// Gathers CPU usage, memory consumption, network I/O, and status information
/// for all containers. Matches statistics to containers using container ID prefixes.
/// Returns partial data even if some statistics collection fails.
///
/// # Returns
///
/// * `Ok(DockerMetricDto)` - Complete metrics data for all containers
/// * `Err(Box<dyn Error + Send + Sync>)` - If critical failures occur
pub async fn collect_metrics(&self) -> Result<DockerMetricDto, Box<dyn Error + Send + Sync>> {
let containers = self.get_containers().await?;
@@ -238,7 +315,7 @@ impl DockerManager {
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
.into_iter()
.filter_map(|info| {
let _container = match info.container {
let container = match info.container {
Some(c) => c,
None => {
eprintln!("Warning: Container info missing container data, skipping");
@@ -286,7 +363,7 @@ impl DockerManager {
};
Some(DockerCollectMetricDto {
server_id: 0,
id: container.id,
status: status_dto,
cpu: cpu_dto,
ram: ram_dto,
@@ -303,6 +380,15 @@ impl DockerManager {
Ok(dto)
}
/// Creates registration DTO with container information
///
/// Prepares container data for initial registration with the backend server.
/// This includes basic container information without detailed metrics.
///
/// # Returns
///
/// * `Ok(DockerServiceDto)` - Container information ready for registration
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
pub async fn create_registration_dto(
&self,
) -> Result<DockerServiceDto, Box<dyn Error + Send + Sync>> {
@@ -322,16 +408,33 @@ impl DockerManager {
// Keep these as utility functions if needed, but they should use DockerManager internally
impl DockerContainer {
/// Returns the container ID
///
/// # Returns
///
/// * `&str` - Full container ID string
pub fn id(&self) -> &str {
&self.id
}
/// Returns the image name
/// Returns the container image name
///
/// Returns "unknown" if the image name is not available.
///
/// # Returns
///
/// * `&str` - Image name or "unknown" if not available
pub fn image(&self) -> &str {
&self.image.as_deref().unwrap_or("unknown")
}
/// Returns the container name
///
/// Returns "unknown" if the container name is not available.
/// Container names typically start with '/' in Docker.
///
/// # Returns
///
/// * `&str` - Container name or "unknown" if not available
pub fn name(&self) -> &str {
&self.name.as_deref().unwrap_or("unknown")
}

View File

@@ -51,19 +51,19 @@ pub async fn get_single_container_cpu_stats(
if let (Some(cpu_usage), Some(pre_cpu_usage)) =
(&cpu_stats.cpu_usage, &precpu_stats.cpu_usage)
{
let cpu_delta = cpu_usage
let cpu_delta: f64 = cpu_usage
.total_usage
.unwrap_or(0)
.saturating_sub(pre_cpu_usage.total_usage.unwrap_or(0));
.saturating_sub(pre_cpu_usage.total_usage.unwrap_or(0)) as f64;
let system_delta = cpu_stats
let system_delta: f64 = cpu_stats
.system_cpu_usage
.unwrap_or(0)
.saturating_sub(precpu_stats.system_cpu_usage.unwrap_or(0));
.saturating_sub(precpu_stats.system_cpu_usage.unwrap_or(0)) as f64;
let online_cpus = cpu_stats.online_cpus.unwrap_or(1);
let cpu_percent = if system_delta > 0 && online_cpus > 0 {
let cpu_percent = if system_delta > 0.0 && online_cpus > 0 {
(cpu_delta as f64 / system_delta as f64) * online_cpus as f64 * 100.0
} else {
0.0

View File

@@ -46,6 +46,20 @@ use bollard::Docker;
use std::error::Error;
/// Get container statistics for all containers using an existing Docker client
///
/// Collects comprehensive statistics for all containers including CPU usage, network I/O,
/// memory consumption, and container status. This is the primary function for gathering
/// complete container metrics in a single operation.
///
/// # Arguments
///
/// * `docker` - Reference to connected Docker client
///
/// # Returns
///
/// * `Ok((Vec<ContainerCpuInfo>, Vec<ContainerNetworkInfo>, Vec<ContainerMemoryInfo>, Vec<ContainerStatusInfo>))` -
/// Tuple containing vectors of CPU, network, memory, and status information for all containers
/// * `Err(Box<dyn Error + Send + Sync>)` - If any statistics collection fails
pub async fn get_container_stats(
docker: &Docker,
) -> Result<
@@ -66,6 +80,20 @@ pub async fn get_container_stats(
}
/// Get container statistics for a specific container
///
/// Retrieves detailed metrics for a single container identified by its ID.
/// Useful for targeted monitoring or when responding to container-specific commands.
///
/// # Arguments
///
/// * `docker` - Reference to connected Docker client
/// * `container_id` - The ID of the container to inspect
///
/// # Returns
///
/// * `Ok((Option<ContainerCpuInfo>, Option<ContainerNetworkInfo>, Option<ContainerMemoryInfo>, Option<ContainerStatusInfo>))` -
/// Tuple containing optional CPU, network, memory, and status information for the specified container
/// * `Err(Box<dyn Error + Send + Sync>)` - If statistics collection fails
pub async fn get_single_container_stats(
docker: &Docker,
container_id: &str,
@@ -84,6 +112,18 @@ pub async fn get_single_container_stats(
}
/// Get total network statistics across all containers
///
/// Calculates the sum of network receive and transmit bytes across all containers.
/// This provides an overview of total network traffic generated by all containers.
///
/// # Arguments
///
/// * `docker` - Reference to connected Docker client
///
/// # Returns
///
/// * `Ok((u64, u64))` - Tuple containing total received bytes and total transmitted bytes
/// * `Err(Box<dyn Error + Send + Sync>)` - If network statistics collection fails
pub async fn get_total_network_stats(
docker: &Docker,
) -> Result<(u64, u64), Box<dyn Error + Send + Sync>> {
@@ -91,11 +131,35 @@ pub async fn get_total_network_stats(
}
/// Get average CPU usage across all containers
///
/// Calculates the average CPU usage percentage across all running containers.
/// This provides a high-level overview of container CPU utilization.
///
/// # Arguments
///
/// * `docker` - Reference to connected Docker client
///
/// # Returns
///
/// * `Ok(f64)` - Average CPU usage percentage across all containers (0.0-100.0)
/// * `Err(Box<dyn Error + Send + Sync>)` - If CPU statistics collection fails
pub async fn get_average_cpu_usage(docker: &Docker) -> Result<f64, Box<dyn Error + Send + Sync>> {
cpu::get_average_cpu_usage(docker).await
}
/// Get total memory usage across all containers
///
/// Calculates the sum of memory usage across all containers.
/// This provides an overview of total memory consumption by all containers.
///
/// # Arguments
///
/// * `docker` - Reference to connected Docker client
///
/// # Returns
///
/// * `Ok(u64)` - Total memory usage in bytes across all containers
/// * `Err(Box<dyn Error + Send + Sync>)` - If memory statistics collection fails
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
ram::get_total_memory_usage(docker).await
}

View File

@@ -5,6 +5,22 @@ use bollard::query_parameters::{ListContainersOptions, InspectContainerOptions};
use bollard::models::{ContainerSummaryStateEnum, ContainerStateStatusEnum};
/// Get status information for all containers
///
/// # Arguments
///
/// * `docker` - Reference to Docker client
///
/// # Returns
///
/// * `Ok(Vec<ContainerStatusInfo>)` - Vector of container status information
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker API call fails
///
/// # Behavior
///
/// - Lists all containers (including stopped ones)
/// - Converts container state enum to string representation
/// - Converts timestamp from i64 to string
/// - Returns basic status info (finished_at is not available in list view)
pub async fn get_all_containers_status(
docker: &Docker,
) -> Result<Vec<ContainerStatusInfo>, Box<dyn Error + Send + Sync>> {
@@ -52,7 +68,26 @@ pub async fn get_all_containers_status(
Ok(status_infos)
}
/// Get status information for a specific container
///
/// # Arguments
///
/// * `docker` - Reference to Docker client
/// * `container_id` - ID of the container to inspect
///
/// # Returns
///
/// * `Ok(Some(ContainerStatusInfo))` - Status info if container found
/// * `Ok(None)` - If container not found
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker API call fails
///
/// # Behavior
///
/// - First tries to find container in list (faster)
/// - Falls back to container inspect for detailed info
/// - Provides more detailed information including finished_at timestamp
/// - Handles container not found case gracefully
pub async fn get_single_container_status(
docker: &Docker,
container_id: &str,

View File

@@ -225,7 +225,7 @@ pub struct DockerMetricDto {
#[derive(Debug, Serialize, Clone)]
pub struct DockerCollectMetricDto {
pub server_id: u16,
pub id: String,
pub status: DockerContainerStatusDto,
pub cpu: DockerContainerCpuDto,
pub ram: DockerContainerRamDto,

View File

@@ -1,20 +0,0 @@
networks:
watcher-network:
driver: bridge
services:
watcher:
image: git.triggermeelmo.com/watcher/watcher-server:v0.1.11
container_name: watcher
deploy:
resources:
limits:
memory: 200M
restart: unless-stopped
env_file: .env
ports:
- "5000:5000"
volumes:
- ./watcher-volumes/data:/app/persistence
- ./watcher-volumes/dumps:/app/wwwroot/downloads/sqlite
- ./watcher-volumes/logs:/app/logs