Show the detected machine type in device inventory

This commit is contained in:
2026-11-18 17:39:43 +01:00
parent 6963ad332c
commit c599baa494
9 changed files with 231 additions and 16 deletions
+29 -1
View File
@@ -10,6 +10,7 @@ use axum::{
http::{HeaderMap, HeaderValue, StatusCode},
};
use chrono::{DateTime, Utc};
use rmm_domain::inventory_document::DeviceType;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sqlx::{Postgres, Row, Transaction, postgres::PgRow};
@@ -289,7 +290,17 @@ fn volumes(data: &Value) -> Value {
}
items.into()
}
fn device_type(hardware: &Value) -> Option<DeviceType> {
DeviceType::deserialize(&hardware["device_type"]).ok()
}
fn os_label(data: &Value) -> Option<String> {
let linux_hardware = observed(data, "linux.hardware");
if linux_hardware["proxmox_ve"].as_bool() == Some(true)
&& device_type(linux_hardware) == Some(DeviceType::Hypervisor)
{
return Some("Proxmox VE".to_owned());
}
let raw = data["os"].as_str()?;
if let Some(pretty) = observed(data, "linux.os")["release"]["PRETTY_NAME"].as_str() {
return Some(pretty.to_owned());
@@ -341,6 +352,8 @@ 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 device_type =
device_type(observed(&data, "linux.hardware")).or_else(|| device_type(windows));
let last = row.get::<Option<DateTime<Utc>>, _>("last_contact_at");
let revoked = row.get::<Option<DateTime<Utc>>, _>("revoked_at");
let presence = match (revoked, last.map(|t| (now - t).num_seconds())) {
@@ -355,7 +368,7 @@ fn project(row: &PgRow, now: DateTime<Utc>, full: bool) -> Value {
"agent_id":row.get::<Uuid,_>("agent_id"),"asset_id":row.get::<Uuid,_>("asset_id"),
"status":row.get::<String,_>("status"),"lifecycle":row.get::<String,_>("lifecycle"),
"archived_at":row.get::<Option<DateTime<Utc>>,_>("archived_at"),"revoked_at":revoked,
"hostname":data["hostname"].as_str(),"os":os_label(&data),
"hostname":data["hostname"].as_str(),"device_type":device_type,"os":os_label(&data),
"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()),
@@ -411,6 +424,21 @@ mod tests {
assert!(os_label(&Value::Null).is_none());
}
#[test]
fn proxmox_os_label_requires_a_valid_hypervisor_observation() {
let mut snapshot = json!({
"os": "Debian",
"observations": [{"scope":"linux.hardware","quality":"valid",
"data":{"device_type":"hypervisor","proxmox_ve":true}}]
});
assert_eq!(os_label(&snapshot).as_deref(), Some("Proxmox VE"));
snapshot["observations"][0]["data"]["device_type"] = json!("virtual");
assert_eq!(os_label(&snapshot).as_deref(), Some("Debian"));
snapshot["observations"][0]["quality"] = json!("error");
assert_eq!(device_type(observed(&snapshot, "linux.hardware")), None);
assert_eq!(device_type(&json!({"device_type":"desktop"})), None);
}
#[tokio::test]
#[ignore = "requires the disposable RLS database from tests/local_access_database.py"]
async fn visibility_is_filtered_before_the_cursor_limit() {