cpu usage -> cpu time

This commit is contained in:
Robin Appelman 2021-03-27 13:18:57 +01:00
commit 490398318a
2 changed files with 8 additions and 12 deletions

View file

@ -2,15 +2,14 @@ use color_eyre::eyre::WrapErr;
use color_eyre::Result; use color_eyre::Result;
use futures_util::future; use futures_util::future;
use futures_util::stream::{Stream, StreamExt}; use futures_util::stream::{Stream, StreamExt};
use heim::cpu::time;
use heim::disk::{FileSystem, Partition}; use heim::disk::{FileSystem, Partition};
use heim::sensors::TemperatureSensor; use heim::sensors::TemperatureSensor;
use heim::units::{information, ratio, thermodynamic_temperature}; use heim::units::{information, thermodynamic_temperature, time};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use parse_display::Display; use parse_display::Display;
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Duration;
use tokio::time::sleep;
#[derive(Debug, Clone, Hash, Eq, PartialEq, Display)] #[derive(Debug, Clone, Hash, Eq, PartialEq, Display)]
#[display(style = "lowercase")] #[display(style = "lowercase")]
@ -76,12 +75,9 @@ impl Heim {
}) })
} }
pub async fn cpu_usage(&self) -> Result<f32> { pub async fn cpu_time(&self) -> Result<f64> {
let cores = heim::cpu::logical_count().await?; let time = time().await?;
let measurement_1 = heim::cpu::usage().await?; Ok(time.user().get::<time::second>() + time.system().get::<time::second>())
sleep(Duration::from_millis(100)).await;
let measurement_2 = heim::cpu::usage().await?;
Ok((measurement_2 - measurement_1).get::<ratio::percent>() / cores as f32)
} }
pub async fn network_stats(&self) -> Result<impl Stream<Item = IOStats>> { pub async fn network_stats(&self) -> Result<impl Stream<Item = IOStats>> {

View file

@ -27,7 +27,7 @@ async fn get_metrics(heim: Heim, zfs: ZFS) -> Result<String, ReportRejection> {
let (hostname, pools, cpu, memory, network, temperatures, disks, disk_usage): ( let (hostname, pools, cpu, memory, network, temperatures, disks, disk_usage): (
String, String,
Vec<DiskUsage>, Vec<DiskUsage>,
f32, f64,
Memory, Memory,
_, _,
HashMap<TemperatureLabel, f32>, HashMap<TemperatureLabel, f32>,
@ -36,7 +36,7 @@ async fn get_metrics(heim: Heim, zfs: ZFS) -> Result<String, ReportRejection> {
) = try_join! { ) = try_join! {
heim.hostname(), heim.hostname(),
zfs.pools(), zfs.pools(),
heim.cpu_usage(), heim.cpu_time(),
heim.memory(), heim.memory(),
heim.network_stats(), heim.network_stats(),
heim.temperatures(), heim.temperatures(),
@ -47,7 +47,7 @@ async fn get_metrics(heim: Heim, zfs: ZFS) -> Result<String, ReportRejection> {
pin_mut!(disks); pin_mut!(disks);
pin_mut!(disk_usage); pin_mut!(disk_usage);
let mut result = String::with_capacity(256); let mut result = String::with_capacity(256);
writeln!(&mut result, "cpu_usage{{host=\"{}\"}} {:.1}", hostname, cpu).ok(); writeln!(&mut result, "cpu_time{{host=\"{}\"}} {:.1}", hostname, cpu).ok();
writeln!( writeln!(
&mut result, &mut result,
"memory_total{{host=\"{}\"}} {}", "memory_total{{host=\"{}\"}} {}",