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

View file

@ -15,7 +15,7 @@ once_cell = "1"
hostname = "0.3" hostname = "0.3"
libc = "0.2" libc = "0.2"
ahash = "0.7" 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" futures-util = "0.3"
[dev-dependencies] [dev-dependencies]

View file

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