91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
pub mod cpu;
|
|
pub mod network;
|
|
pub mod ram;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[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
|
|
pub async fn get_container_stats(
|
|
docker: &Docker,
|
|
) -> Result<
|
|
(
|
|
Vec<ContainerCpuInfo>,
|
|
Vec<ContainerNetworkInfo>,
|
|
Vec<ContainerMemoryInfo>,
|
|
),
|
|
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?;
|
|
|
|
Ok((cpu_infos, net_infos, mem_infos))
|
|
}
|
|
|
|
/// Get container statistics for a specific container
|
|
pub async fn get_single_container_stats(
|
|
docker: &Docker,
|
|
container_id: &str,
|
|
) -> Result<
|
|
(
|
|
Option<ContainerCpuInfo>,
|
|
Option<ContainerNetworkInfo>,
|
|
Option<ContainerMemoryInfo>,
|
|
),
|
|
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?;
|
|
|
|
Ok((cpu_info, net_info, mem_info))
|
|
}
|
|
|
|
/// Get total network statistics across all containers
|
|
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
|
|
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
|
|
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
|
ram::get_total_memory_usage(docker).await
|
|
}
|