moved stats into own folder
Some checks failed
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Failing after 1m9s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Has been skipped
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Has been skipped
Rust Cross-Platform Build / Set Tag Name (push) Has been skipped
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 2s
Some checks failed
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Failing after 1m9s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Has been skipped
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Has been skipped
Rust Cross-Platform Build / Set Tag Name (push) Has been skipped
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 2s
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
use super::ContainerMemoryInfo;
|
||||
use bollard::query_parameters::{ListContainersOptions, StatsOptions};
|
||||
use bollard::Docker;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Get memory statistics for all containers
|
||||
pub async fn get_all_containers_memory_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<Vec<ContainerMemoryInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut mem_infos = Vec::new();
|
||||
|
||||
for container in containers {
|
||||
let id = container.id.unwrap_or_default();
|
||||
|
||||
// Skip if no ID
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(mem_info) = get_single_container_memory_stats(docker, &id).await? {
|
||||
mem_infos.push(mem_info);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(mem_infos)
|
||||
}
|
||||
|
||||
/// Get memory statistics for a specific container
|
||||
pub async fn get_single_container_memory_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<Option<ContainerMemoryInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let mut stats_stream = docker.stats(
|
||||
container_id,
|
||||
Some(StatsOptions {
|
||||
stream: false,
|
||||
one_shot: true,
|
||||
}),
|
||||
);
|
||||
|
||||
if let Some(stats) = stats_stream.try_next().await? {
|
||||
if let Some(memory_stats) = &stats.memory_stats {
|
||||
let memory_usage = memory_stats.usage.unwrap_or(0);
|
||||
let memory_limit = memory_stats.limit.unwrap_or(1); // Avoid division by zero
|
||||
|
||||
let memory_usage_percent = if memory_limit > 0 {
|
||||
(memory_usage as f64 / memory_limit as f64) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
return Ok(Some(ContainerMemoryInfo {
|
||||
container_id: container_id.to_string(),
|
||||
memory_usage,
|
||||
memory_limit,
|
||||
memory_usage_percent,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Get total memory usage across all containers
|
||||
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
||||
let mem_infos = get_all_containers_memory_stats(docker).await?;
|
||||
let total_memory: u64 = mem_infos.iter().map(|mem| mem.memory_usage).sum();
|
||||
Ok(total_memory)
|
||||
}
|
Reference in New Issue
Block a user