Show the detected machine type in device inventory
This commit is contained in:
@@ -4,6 +4,7 @@ use crate::{
|
||||
actions::{ServiceUnit, reboot_required},
|
||||
process,
|
||||
};
|
||||
use rmm_domain::inventory_document::DeviceType;
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
ffi::{CStr, CString},
|
||||
@@ -257,6 +258,8 @@ pub struct UpdateStatus {
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct Hardware {
|
||||
pub readable_dmi: BTreeMap<String, String>,
|
||||
pub device_type: Option<DeviceType>,
|
||||
pub proxmox_ve: bool,
|
||||
}
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct LinuxInventory {
|
||||
@@ -1116,7 +1119,36 @@ pub fn updates() -> Observation<UpdateStatus> {
|
||||
}
|
||||
result
|
||||
}
|
||||
fn virtualization_type(output: &process::Output) -> Option<DeviceType> {
|
||||
if output.timed_out || output.truncated {
|
||||
return None;
|
||||
}
|
||||
match (
|
||||
output.code,
|
||||
std::str::from_utf8(&output.stdout).ok()?.trim(),
|
||||
) {
|
||||
(Some(1), "none") => Some(DeviceType::Physical),
|
||||
(Some(0), name) if !name.is_empty() && name != "none" => Some(DeviceType::Virtual),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn machine_type(proxmox_ve: bool) -> Option<DeviceType> {
|
||||
if proxmox_ve {
|
||||
return Some(DeviceType::Hypervisor);
|
||||
}
|
||||
let output = process::run(
|
||||
"/usr/bin/systemd-detect-virt",
|
||||
&[],
|
||||
Duration::from_secs(2),
|
||||
512,
|
||||
)
|
||||
.ok()?;
|
||||
virtualization_type(&output)
|
||||
}
|
||||
|
||||
pub fn hardware() -> Observation<Hardware> {
|
||||
const SOURCE: &str = "linux.dmi:/sys/class/dmi/id;systemd-detect-virt;pve-manager";
|
||||
let mut readable_dmi = BTreeMap::new();
|
||||
for field in [
|
||||
"sys_vendor",
|
||||
@@ -1132,15 +1164,21 @@ pub fn hardware() -> Observation<Hardware> {
|
||||
readable_dmi.insert(field.into(), value);
|
||||
}
|
||||
}
|
||||
if readable_dmi.is_empty() {
|
||||
Observation::unavailable(
|
||||
"linux.dmi:/sys/class/dmi/id",
|
||||
Quality::Unsupported,
|
||||
"DMI_UNAVAILABLE",
|
||||
)
|
||||
let proxmox_ve =
|
||||
Path::new("/usr/bin/pveversion").is_file() && Path::new("/usr/bin/pvedaemon").is_file();
|
||||
let device_type = machine_type(proxmox_ve);
|
||||
if readable_dmi.is_empty() && device_type.is_none() {
|
||||
Observation::unavailable(SOURCE, Quality::Unsupported, "HARDWARE_UNAVAILABLE")
|
||||
} else {
|
||||
Observation::valid("linux.dmi:/sys/class/dmi/id", Hardware { readable_dmi })
|
||||
.partial("READABLE_DMI_ONLY_NO_SMART_OR_ENCRYPTION_PROOF")
|
||||
Observation::valid(
|
||||
SOURCE,
|
||||
Hardware {
|
||||
readable_dmi,
|
||||
device_type,
|
||||
proxmox_ve,
|
||||
},
|
||||
)
|
||||
.partial("NO_SMART_OR_ENCRYPTION_PROOF")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1362,6 +1400,26 @@ mod tests {
|
||||
assert_eq!(request.since_filter, "@100.123457");
|
||||
assert_eq!(request.until_filter, "@101.987654");
|
||||
}
|
||||
#[test]
|
||||
fn machine_types_distinguish_proxmox_guests_and_probe_failures() {
|
||||
assert_eq!(machine_type(true), Some(DeviceType::Hypervisor));
|
||||
let mut output = process::Output {
|
||||
code: Some(0),
|
||||
stdout: b"kvm\n".to_vec(),
|
||||
stderr: vec![],
|
||||
truncated: false,
|
||||
timed_out: false,
|
||||
interrupted: false,
|
||||
};
|
||||
assert_eq!(virtualization_type(&output), Some(DeviceType::Virtual));
|
||||
output.code = Some(1);
|
||||
assert_eq!(virtualization_type(&output), None);
|
||||
output.stdout = b"none\n".to_vec();
|
||||
assert_eq!(virtualization_type(&output), Some(DeviceType::Physical));
|
||||
output.timed_out = true;
|
||||
assert_eq!(virtualization_type(&output), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn harmless_native_read_providers() {
|
||||
assert_eq!(memory().quality, Quality::Valid);
|
||||
|
||||
Reference in New Issue
Block a user