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,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ WITH classified AS (
|
||||
SELECT a.tenant_id,a.asset_id,a.agent_id,a.created_at,a.revoked_at,a.last_contact_at,
|
||||
a.last_authenticated_at,a.last_authenticated_certificate_sha256,a.certificate_sha256,
|
||||
a.enrollment_generation,a.assignment_version,a.capabilities,a.reported_agent_version,
|
||||
a.system_usage,a.system_usage_observed_at,
|
||||
asset.archived_at,t.name AS customer_name,
|
||||
CASE
|
||||
WHEN a.revoked_at IS NOT NULL THEN 'revoked'
|
||||
|
||||
@@ -352,6 +352,17 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
||||
let data = row.get::<Option<Value>, _>("data").unwrap_or(Value::Null);
|
||||
let cpu = observed(&data, "linux.cpu");
|
||||
let windows = observed(&data, "windows.hardware");
|
||||
let linux_memory = observed(&data, "linux.memory");
|
||||
let usage = row
|
||||
.get::<Option<Value>, _>("system_usage")
|
||||
.unwrap_or(Value::Null);
|
||||
let usage_at: Option<DateTime<Utc>> = row.get("system_usage_observed_at");
|
||||
let inventory_at: Option<DateTime<Utc>> = row.get("observed_at");
|
||||
let ram_available = usage["ram_available_bytes"].as_u64();
|
||||
let ram_total = usage["ram_total_bytes"]
|
||||
.as_u64()
|
||||
.or_else(|| linux_memory["total_bytes"].as_u64())
|
||||
.or_else(|| windows["memory_total_bytes"].as_u64());
|
||||
let device_type =
|
||||
device_type(observed(&data, "linux.hardware")).or_else(|| device_type(windows));
|
||||
let last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at");
|
||||
@@ -372,9 +383,14 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
|
||||
"disk_free_bytes":data["root_available_bytes"].as_u64(),"disk_total_bytes":data["root_total_bytes"].as_u64(),
|
||||
"cpu_model":cpu["model"].as_str().or_else(||windows["cpu_model"].as_str()),
|
||||
"cpu_logical_count":cpu["logical_processors"].as_u64().or_else(||windows["logical_processors_visible"].as_u64()),
|
||||
"ram_total_bytes":observed(&data,"linux.memory")["total_bytes"].as_u64().or_else(||windows["memory_total_bytes"].as_u64()),
|
||||
"cpu_usage_percent":usage["cpu_usage_percent"],
|
||||
"cpu_usage_observed_at":usage["cpu_usage_percent"].is_number().then_some(usage_at).flatten(),
|
||||
"ram_total_bytes":ram_total,
|
||||
"ram_available_bytes":ram_available.or_else(|| linux_memory["available_bytes"].as_u64().or_else(||windows["memory_available_bytes"].as_u64())
|
||||
.filter(|available| ram_total.is_some_and(|total| *available <= total))),
|
||||
"ram_observed_at":if ram_available.is_some() {usage_at} else {inventory_at},
|
||||
"last_contact_at":last,"agent_version":row.get::<Option<String>,_>("reported_agent_version"),
|
||||
"presence":presence,"observed_at":row.get::<Option<DateTime<Utc>>,_>("observed_at"),
|
||||
"presence":presence,"observed_at":inventory_at,
|
||||
"received_at":row.get::<Option<DateTime<Utc>>,_>("received_at"),
|
||||
"source_id":row.get::<Option<String>,_>("source_id"),"quality":if data.is_null(){"unknown"}else{"valid"},
|
||||
"completeness":"partial","health":"unknown"
|
||||
|
||||
Reference in New Issue
Block a user