166 lines
5.7 KiB
Rust
166 lines
5.7 KiB
Rust
pub mod cpu;
|
|
pub mod network;
|
|
pub mod ram;
|
|
pub mod status;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
pub struct ContainerStatusInfo {
|
|
pub container_id: Option<String>,
|
|
pub status: Option<String>, // "running", "stopped", "paused", "exited", etc.
|
|
pub state: Option<String>, // More detailed state information
|
|
pub started_at: Option<String>,
|
|
pub finished_at: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
pub struct ContainerCpuInfo {
|
|
pub container_id: Option<String>,
|
|
pub cpu_usage_percent: Option<f64>,
|
|
pub system_cpu_usage: Option<u64>,
|
|
pub container_cpu_usage: Option<u64>,
|
|
pub online_cpus: Option<u32>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
pub struct ContainerNetworkInfo {
|
|
pub container_id: Option<String>,
|
|
pub rx_bytes: Option<u64>,
|
|
pub tx_bytes: Option<u64>,
|
|
pub rx_packets: Option<u64>,
|
|
pub tx_packets: Option<u64>,
|
|
pub rx_errors: Option<u64>,
|
|
pub tx_errors: Option<u64>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
pub struct ContainerMemoryInfo {
|
|
pub container_id: Option<String>,
|
|
pub memory_usage: Option<u64>,
|
|
pub memory_limit: Option<u64>,
|
|
pub memory_usage_percent: Option<f64>,
|
|
}
|
|
|
|
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<
|
|
(
|
|
Vec<ContainerCpuInfo>,
|
|
Vec<ContainerNetworkInfo>,
|
|
Vec<ContainerMemoryInfo>,
|
|
Vec<ContainerStatusInfo>,
|
|
),
|
|
Box<dyn Error + Send + Sync>,
|
|
> {
|
|
let cpu_infos = cpu::get_all_containers_cpu_stats(docker).await?;
|
|
let net_infos = network::get_all_containers_network_stats(docker).await?;
|
|
let mem_infos = ram::get_all_containers_memory_stats(docker).await?;
|
|
let status_infos = status::get_all_containers_status(docker).await?;
|
|
|
|
Ok((cpu_infos, net_infos, mem_infos, status_infos))
|
|
}
|
|
|
|
/// 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,
|
|
) -> Result<(
|
|
Option<ContainerCpuInfo>,
|
|
Option<ContainerNetworkInfo>,
|
|
Option<ContainerMemoryInfo>,
|
|
Option<ContainerStatusInfo>,
|
|
), Box<dyn Error + Send + Sync>> {
|
|
let cpu_info = cpu::get_single_container_cpu_stats(docker, container_id).await?;
|
|
let net_info = network::get_single_container_network_stats(docker, container_id).await?;
|
|
let mem_info = ram::get_single_container_memory_stats(docker, container_id).await?;
|
|
let status_info = status::get_single_container_status(docker, container_id).await?;
|
|
|
|
Ok((cpu_info, net_info, mem_info, status_info))
|
|
}
|
|
|
|
/// 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>> {
|
|
network::get_total_network_stats(docker).await
|
|
}
|
|
|
|
/// 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
|
|
}
|