Show available RAM and measured CPU usage for devices
Rust development slice / linux (push) Failing after 8s

This commit is contained in:
2026-11-18 18:37:18 +01:00
parent fb2376443c
commit b3fe35b86b
17 changed files with 254 additions and 30 deletions
+47 -1
View File
@@ -4,6 +4,7 @@ use chrono::{DateTime, Utc};
use prost::Message;
use rmm_domain::MAX_MESSAGE;
use rmm_protocol::*;
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use sqlx::Row;
use uuid::Uuid;
@@ -146,7 +147,12 @@ pub async fn ingest(
match &e.payload {
Some(Payload::Heartbeat(h)) => {
Uuid::parse_str(&h.instance_id)?;
sqlx::query("UPDATE agent_installations SET last_contact_at=now() WHERE agent_id=$1 AND revoked_at IS NULL").bind(agent).execute(&mut *tx).await?;
let usage = h.system_usage.as_ref().and_then(system_usage);
sqlx::query("UPDATE agent_installations SET last_contact_at=now(),
system_usage=COALESCE($2,system_usage),
system_usage_observed_at=CASE WHEN $2::jsonb IS NULL THEN system_usage_observed_at ELSE $3 END
WHERE agent_id=$1 AND revoked_at IS NULL")
.bind(agent).bind(usage).bind(observed).execute(&mut *tx).await?;
}
Some(Payload::InventorySnapshot(s)) => {
ensure!(
@@ -197,3 +203,43 @@ pub async fn ingest(
e.connection_generation,
))
}
fn system_usage(sample: &SystemUsage) -> Option<Value> {
let cpu = sample
.cpu_usage_basis_points
.filter(|&value| value <= 10_000);
let memory = sample
.ram_available_bytes
.zip(sample.ram_total_bytes)
.filter(|&(available, total)| total > 0 && available <= total);
if cpu.is_none() && memory.is_none() {
return None;
}
Some(json!({
"cpu_usage_percent": cpu.map(|value| f64::from(value) / 100.0),
"ram_available_bytes": memory.map(|(available, _)| available),
"ram_total_bytes": memory.map(|(_, total)| total),
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_zero_usage_and_ignores_invalid_metrics_without_losing_the_heartbeat() {
let mut sample = SystemUsage {
cpu_usage_basis_points: Some(0),
ram_available_bytes: Some(0),
ram_total_bytes: Some(1),
};
assert_eq!(
system_usage(&sample),
Some(json!({"cpu_usage_percent": 0.0, "ram_available_bytes": 0, "ram_total_bytes": 1}))
);
sample.cpu_usage_basis_points = Some(10_001);
assert!(system_usage(&sample).unwrap()["cpu_usage_percent"].is_null());
sample.ram_available_bytes = Some(2);
assert_eq!(system_usage(&sample), None);
}
}