Show available RAM and measured CPU usage for devices
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -4,12 +4,12 @@ use crate::{
|
||||
actions::{ServiceUnit, reboot_required},
|
||||
process,
|
||||
};
|
||||
use rmm_domain::inventory_document::DeviceType;
|
||||
use rmm_domain::inventory_document::{CpuTimes, DeviceType};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
ffi::{CStr, CString},
|
||||
fs,
|
||||
io::Read,
|
||||
io::{BufRead, BufReader, Read},
|
||||
mem::MaybeUninit,
|
||||
net::{Ipv4Addr, Ipv6Addr},
|
||||
path::Path,
|
||||
@@ -432,6 +432,41 @@ pub fn operating_system() -> Observation<OperatingSystem> {
|
||||
Observation::unavailable("linux.os", Quality::Error, "OS_SOURCE_UNAVAILABLE")
|
||||
}
|
||||
}
|
||||
fn aggregate_jiffies(stat: &str) -> Option<Vec<u64>> {
|
||||
stat.lines()
|
||||
.find(|line| line.starts_with("cpu "))
|
||||
.and_then(|line| {
|
||||
line.split_whitespace()
|
||||
.skip(1)
|
||||
.map(str::parse)
|
||||
.collect::<Result<_, _>>()
|
||||
.ok()
|
||||
})
|
||||
}
|
||||
|
||||
fn linux_cpu_times(jiffies: &[u64]) -> Option<CpuTimes> {
|
||||
// Linux already includes guest and guest_nice in user and nice.
|
||||
let total = jiffies
|
||||
.iter()
|
||||
.take(8)
|
||||
.try_fold(0_u64, |total, ticks| total.checked_add(*ticks))?;
|
||||
let idle = jiffies
|
||||
.get(3)?
|
||||
.checked_add(jiffies.get(4).copied().unwrap_or_default())?;
|
||||
Some(CpuTimes { total, idle })
|
||||
}
|
||||
|
||||
pub fn cpu_times() -> Option<CpuTimes> {
|
||||
let mut line = String::new();
|
||||
BufReader::new(fs::File::open("/proc/stat").ok()?.take(4096))
|
||||
.read_line(&mut line)
|
||||
.ok()?;
|
||||
if !line.ends_with('\n') {
|
||||
return None;
|
||||
}
|
||||
linux_cpu_times(&aggregate_jiffies(&line)?)
|
||||
}
|
||||
|
||||
pub fn cpu() -> Observation<Cpu> {
|
||||
let info = read("/proc/cpuinfo", 1024 * 1024);
|
||||
let stat = read("/proc/stat", 256 * 1024);
|
||||
@@ -460,15 +495,7 @@ pub fn cpu() -> Observation<Cpu> {
|
||||
.filter(|(k, _)| k.trim() == "model name")
|
||||
.map(|(_, v)| v.trim().to_owned())
|
||||
});
|
||||
let aggregate_jiffies: Option<Vec<u64>> =
|
||||
stat.lines().find(|l| l.starts_with("cpu ")).and_then(|l| {
|
||||
l.split_whitespace()
|
||||
.skip(1)
|
||||
.map(str::parse)
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.ok()
|
||||
});
|
||||
match aggregate_jiffies {
|
||||
match aggregate_jiffies(&stat) {
|
||||
Some(aggregate_jiffies) if logical_processors > 0 => Observation::valid(
|
||||
"linux.cpu:/proc/cpuinfo,/proc/stat",
|
||||
Cpu {
|
||||
@@ -1400,6 +1427,16 @@ mod tests {
|
||||
assert_eq!(request.since_filter, "@100.123457");
|
||||
assert_eq!(request.until_filter, "@101.987654");
|
||||
}
|
||||
#[test]
|
||||
fn cpu_time_excludes_double_counted_guest_ticks_and_io_wait() {
|
||||
let before = linux_cpu_times(&[10, 0, 5, 60, 10, 0, 0, 0, 4, 0]).unwrap();
|
||||
let after = linux_cpu_times(&[30, 0, 5, 120, 30, 0, 0, 0, 20, 0]).unwrap();
|
||||
assert_eq!(after.usage_since(before), Some(2_000));
|
||||
assert_eq!(after.usage_since(after), None);
|
||||
assert_eq!(before.usage_since(after), None);
|
||||
assert!(linux_cpu_times(&[10, 0, 0]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn machine_types_distinguish_proxmox_guests_and_probe_failures() {
|
||||
assert_eq!(machine_type(true), Some(DeviceType::Hypervisor));
|
||||
|
||||
Reference in New Issue
Block a user