docker image

This commit is contained in:
Robin Appelman 2021-03-30 21:52:34 +02:00
commit 67179619b7
3 changed files with 22 additions and 20 deletions

1
Cargo.lock generated
View file

@ -76,6 +76,7 @@ dependencies = [
[[package]]
name = "bollard"
version = "0.10.1"
source = "git+https://github.com/icewind1991/bollard?branch=stat-one-shot#6e8f4d4d0cd10fe223625747289a0e84cafdefcf"
dependencies = [
"base64",
"bollard-stubs",

View file

@ -15,7 +15,7 @@ once_cell = "1"
hostname = "0.3"
libc = "0.2"
ahash = "0.7"
bollard = { version = "0.10", path = "../bollard" }
bollard = { version = "0.10", git = "https://github.com/icewind1991/bollard", branch = "stat-one-shot" }
futures-util = "0.3"
[dev-dependencies]

View file

@ -1,4 +1,5 @@
use bollard::container::{Stats, StatsOptions};
use bollard::models::ContainerSummaryInner;
use bollard::Docker;
use color_eyre::Result;
use futures_util::future::ready;
@ -8,37 +9,37 @@ use std::fmt::Write;
#[derive(Debug)]
pub struct Container {
name: String,
image: String,
memory: u64,
cpu_time: f64,
}
impl From<Stats> for Container {
fn from(stats: Stats) -> Self {
Container {
name: stats.name,
memory: stats.memory_stats.usage.unwrap_or_default(),
cpu_time: stats.cpu_stats.cpu_usage.total_usage as f64
/ 1_000_000_000.0
/ stats.cpu_stats.online_cpus.unwrap_or(1) as f64,
}
}
}
impl Container {
pub fn write<W: Write>(&self, mut w: W, hostname: &str) {
writeln!(
&mut w,
"container_memory{{host=\"{}\", container=\"{}\"}} {}",
hostname, self.name, self.memory
"container_memory{{host=\"{}\", container=\"{}\", image=\"{}\"}} {}",
hostname, self.name, self.image, self.memory
)
.ok();
writeln!(
&mut w,
"container_cpu_time{{host=\"{}\", container=\"{}\"}} {:.3}",
hostname, self.name, self.cpu_time
"container_cpu_time{{host=\"{}\", container=\"{}\", image=\"{}\"}} {:.3}",
hostname, self.name, self.image, self.cpu_time
)
.ok();
}
fn from(stats: Stats, container: ContainerSummaryInner) -> Self {
Container {
name: stats.name,
image: container.image.unwrap_or_default(),
memory: stats.memory_stats.usage.unwrap_or_default(),
cpu_time: stats.cpu_stats.cpu_usage.total_usage as f64
/ 1_000_000_000.0
/ stats.cpu_stats.online_cpus.unwrap_or(1) as f64,
}
}
}
pub async fn get_docker() -> Option<Docker> {
@ -59,10 +60,10 @@ pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> {
.map(move |container| {
let docker = docker.clone();
async move {
let id = container.id.unwrap();
let id = container.id.as_ref().unwrap();
let stats: Stats = docker
.stats(
&id,
id,
Some(StatsOptions {
stream: false,
one_shot: true,
@ -71,7 +72,7 @@ pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> {
.next()
.await?
.ok()?;
Some(stats.into())
Some(Container::from(stats, container))
}
})
.collect::<FuturesUnordered<_>>()