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, pub status: Option, // "running", "stopped", "paused", "exited", etc. pub state: Option, // More detailed state information pub started_at: Option, pub finished_at: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ContainerCpuInfo { pub container_id: Option, pub cpu_usage_percent: Option, pub system_cpu_usage: Option, pub container_cpu_usage: Option, pub online_cpus: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ContainerNetworkInfo { pub container_id: Option, pub rx_bytes: Option, pub tx_bytes: Option, pub rx_packets: Option, pub tx_packets: Option, pub rx_errors: Option, pub tx_errors: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ContainerMemoryInfo { pub container_id: Option, pub memory_usage: Option, pub memory_limit: Option, pub memory_usage_percent: Option, } 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, Vec, Vec, Vec))` - /// Tuple containing vectors of CPU, network, memory, and status information for all containers /// * `Err(Box)` - If any statistics collection fails pub async fn get_container_stats( docker: &Docker, ) -> Result< ( Vec, Vec, Vec, Vec, ), Box, > { 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, Option, Option, Option))` - /// Tuple containing optional CPU, network, memory, and status information for the specified container /// * `Err(Box)` - If statistics collection fails pub async fn get_single_container_stats( docker: &Docker, container_id: &str, ) -> Result<( Option, Option, Option, Option, ), Box> { 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)` - If network statistics collection fails pub async fn get_total_network_stats( docker: &Docker, ) -> Result<(u64, u64), Box> { 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)` - If CPU statistics collection fails pub async fn get_average_cpu_usage(docker: &Docker) -> Result> { 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)` - If memory statistics collection fails pub async fn get_total_memory_usage(docker: &Docker) -> Result> { ram::get_total_memory_usage(docker).await }