mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 10:14:09 +02:00
keep sensors around
This commit is contained in:
parent
f06b8ab698
commit
aea5a65371
2 changed files with 41 additions and 19 deletions
33
src/lib.rs
33
src/lib.rs
|
|
@ -14,6 +14,7 @@ use std::fmt::Write;
|
|||
use std::io;
|
||||
use std::num::{ParseFloatError, ParseIntError};
|
||||
use std::str::Utf8Error;
|
||||
use std::sync::Mutex;
|
||||
use sysconf::SysconfError;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
|
@ -44,17 +45,33 @@ impl From<SysconfError> for Error {
|
|||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
pub fn get_metrics() -> Result<String> {
|
||||
pub struct Sensors {
|
||||
pub hostname: String,
|
||||
cpu: Mutex<CpuTimeSource>,
|
||||
temp: Mutex<TemperatureSource>,
|
||||
net: Mutex<NetworkSource>,
|
||||
}
|
||||
|
||||
impl Sensors {
|
||||
pub fn new() -> Result<Sensors> {
|
||||
Ok(Sensors {
|
||||
hostname: hostname()?,
|
||||
cpu: Mutex::new(CpuTimeSource::new()?),
|
||||
temp: Mutex::new(TemperatureSource::new()?),
|
||||
net: Mutex::new(NetworkSource::new()?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_metrics(sensors: &Sensors) -> Result<String> {
|
||||
let hostname = &sensors.hostname;
|
||||
let disk_usage = disk_usage()?;
|
||||
let disks = disk_stats()?;
|
||||
let mut cpu_source = CpuTimeSource::new()?;
|
||||
let cpu = cpu_source.read()?;
|
||||
let hostname = hostname()?;
|
||||
let cpu = sensors.cpu.lock().unwrap().read()?;
|
||||
let memory = memory()?;
|
||||
let mut temp_source = TemperatureSource::new()?;
|
||||
let temperatures = temp_source.read()?;
|
||||
let mut net_source = NetworkSource::new()?;
|
||||
let networks = net_source.read()?;
|
||||
let temperatures = sensors.temp.lock().unwrap().read()?;
|
||||
let mut net = sensors.net.lock().unwrap();
|
||||
let networks = net.read()?;
|
||||
let pools = pools();
|
||||
let mut result = String::with_capacity(256);
|
||||
|
||||
|
|
|
|||
27
src/main.rs
27
src/main.rs
|
|
@ -5,9 +5,10 @@ use futures_util::StreamExt;
|
|||
use libmdns::Responder;
|
||||
use palantir::disk::zfs::arcstats;
|
||||
use palantir::docker::{get_docker, stat, Container};
|
||||
use palantir::get_metrics;
|
||||
use palantir::gpu::{gpu_metrics, update_gpu_power};
|
||||
use palantir::power::power_usage;
|
||||
use palantir::{get_metrics, Sensors};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::runtime::Handle;
|
||||
use tokio::spawn;
|
||||
|
|
@ -28,30 +29,29 @@ impl From<Report> for ReportRejection {
|
|||
|
||||
impl Reject for ReportRejection {}
|
||||
|
||||
async fn serve_inner(docker: Option<Docker>) -> Result<String> {
|
||||
let mut metrics = get_metrics()?;
|
||||
let hostname = palantir::sensors::hostname()?;
|
||||
async fn serve_inner(docker: Option<Docker>, sensors: &Sensors) -> Result<String> {
|
||||
let mut metrics = get_metrics(sensors)?;
|
||||
if let Some(docker) = docker {
|
||||
let containers = stat(docker).await?;
|
||||
pin_mut!(containers);
|
||||
while let Some(container) = containers.next().await {
|
||||
let container: Container = container;
|
||||
container.write(&mut metrics, &hostname);
|
||||
container.write(&mut metrics, &sensors.hostname);
|
||||
}
|
||||
}
|
||||
if let Some(power) = power_usage()? {
|
||||
power.write(&mut metrics, &hostname);
|
||||
power.write(&mut metrics, &sensors.hostname);
|
||||
}
|
||||
if let Some(arc) = arcstats()? {
|
||||
arc.write(&mut metrics, &hostname);
|
||||
arc.write(&mut metrics, &sensors.hostname);
|
||||
}
|
||||
gpu_metrics(&mut metrics, &hostname);
|
||||
gpu_metrics(&mut metrics, &sensors.hostname);
|
||||
|
||||
Ok(metrics)
|
||||
}
|
||||
|
||||
async fn serve_metrics(docker: Option<Docker>) -> Result<String, Rejection> {
|
||||
serve_inner(docker)
|
||||
async fn serve_metrics(docker: Option<Docker>, sensors: Arc<Sensors>) -> Result<String, Rejection> {
|
||||
serve_inner(docker, &sensors)
|
||||
.await
|
||||
.map_err(ReportRejection::from)
|
||||
.map_err(warp::reject::custom)
|
||||
|
|
@ -76,6 +76,8 @@ async fn main() -> Result<()> {
|
|||
|
||||
let docker = get_docker().await;
|
||||
let docker = warp::any().map(move || docker.clone());
|
||||
let sensors = Arc::new(Sensors::new()?);
|
||||
let sensors = warp::any().map(move || sensors.clone());
|
||||
|
||||
if !mdns {
|
||||
spawn(setup_mdns(
|
||||
|
|
@ -86,7 +88,10 @@ async fn main() -> Result<()> {
|
|||
|
||||
std::thread::spawn(update_gpu_power);
|
||||
|
||||
let metrics = warp::path!("metrics").and(docker).and_then(serve_metrics);
|
||||
let metrics = warp::path!("metrics")
|
||||
.and(docker)
|
||||
.and(sensors)
|
||||
.and_then(serve_metrics);
|
||||
|
||||
warp::serve(metrics).run(([0, 0, 0, 0], host_port)).await;
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue