This commit is contained in:
Robin Appelman 2026-04-06 22:51:25 +02:00
commit 22c82c59af
7 changed files with 926 additions and 924 deletions

View file

@ -1,5 +1,6 @@
use bollard::container::{Stats, StatsOptions};
use bollard::models::ContainerSummary;
use bollard::plugin::ContainerStatsResponse;
use bollard::query_parameters::StatsOptions;
use bollard::Docker;
use color_eyre::Result;
use futures_util::future::ready;
@ -45,27 +46,36 @@ impl Container {
.ok();
}
fn from(stats: Stats, container: ContainerSummary) -> Self {
fn from(stats: ContainerStatsResponse, container: ContainerSummary) -> Self {
let cpu = stats.cpu_stats.unwrap_or_default();
Container {
name: stats.name,
name: stats.name.unwrap_or_default(),
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
memory: stats
.memory_stats
.unwrap_or_default()
.usage
.unwrap_or_default(),
cpu_time: cpu
.cpu_usage
.unwrap_or_default()
.total_usage
.unwrap_or_default() as f64
/ 1_000_000_000.0
/ stats.cpu_stats.online_cpus.unwrap_or(1) as f64,
/ cpu.online_cpus.unwrap_or(1) as f64,
network_sent: stats
.networks
.as_ref()
.into_iter()
.flat_map(HashMap::values)
.map(|stats| stats.tx_bytes)
.map(|stats| stats.tx_bytes.unwrap_or_default())
.sum(),
network_received: stats
.networks
.as_ref()
.into_iter()
.flat_map(HashMap::values)
.map(|stats| stats.rx_bytes)
.map(|stats| stats.rx_bytes.unwrap_or_default())
.sum(),
}
}
@ -73,24 +83,20 @@ impl Container {
pub async fn get_docker() -> Option<Docker> {
match Docker::connect_with_local_defaults() {
Ok(docker) => docker
.list_containers::<String>(None)
.await
.ok()
.map(|_| docker),
Ok(docker) => docker.list_containers(None).await.ok().map(|_| docker),
Err(_) => None,
}
}
pub async fn stat(docker: Docker) -> Result<impl Stream<Item = Container>> {
let containers = docker.list_containers::<String>(None).await?;
let containers = docker.list_containers(None).await?;
Ok(containers
.into_iter()
.map(move |container| {
let docker = docker.clone();
async move {
let id = container.id.as_ref().unwrap();
let stats: Stats = docker
let stats = docker
.stats(
id,
Some(StatsOptions {

View file

@ -1,4 +1,4 @@
use std::fs::{read_dir, read_to_string, File};
use std::fs::{File, read_dir, read_to_string};
use std::io;
use std::io::{ErrorKind, Read, Seek};
use std::path::{Path, PathBuf};
@ -26,9 +26,8 @@ impl FileSource {
Ok(FileSource {
path: path.into(),
buff: String::with_capacity(32),
file: File::open(path).map_err(|e| {
file: File::open(path).inspect_err(|_| {
warn!("failed to open sensor {}", path.display());
e
})?,
})
}
@ -66,9 +65,8 @@ impl FileSource {
}
pub fn reopen(&mut self) -> io::Result<()> {
self.file = File::open(&self.path).map_err(|e| {
self.file = File::open(&self.path).inspect_err(|_| {
warn!("failed to open sensor {}", self.path.display());
e
})?;
Ok(())
}

View file

@ -1,11 +1,11 @@
use bollard::Docker;
use clap::Parser;
use color_eyre::{Report, Result};
use futures_util::pin_mut;
use futures_util::StreamExt;
use futures_util::pin_mut;
use libmdns::Responder;
use sidewindow::docker::{get_docker, stat, Container};
use sidewindow::{get_metrics, Sensors};
use sidewindow::docker::{Container, get_docker, stat};
use sidewindow::{Sensors, get_metrics};
use std::sync::Arc;
use std::time::Duration;
use tokio::runtime::Handle;
@ -121,12 +121,7 @@ async fn setup_mdns(hostname: String, port: u16) {
}
};
let _svc = mdns.register(
"_prometheus-http._tcp".into(),
hostname,
port,
&["/metrics"],
);
let _svc = mdns.register("_prometheus-http._tcp", &hostname, port, &["/metrics"]);
loop {
sleep(Duration::from_secs(60 * 60)).await;