From b93ecb13b6970f77fce45b9a477977c2fbf8335e Mon Sep 17 00:00:00 2001 From: Jochen Maes Date: Fri, 3 Nov 2023 06:49:44 +0100 Subject: [PATCH] Adds first dmi parameter: form_factor --- src/gatherers/system.rs | 77 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/gatherers/system.rs b/src/gatherers/system.rs index 77166dd..d6a4c58 100644 --- a/src/gatherers/system.rs +++ b/src/gatherers/system.rs @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use std::{fs, str, usize}; + use sysinfo::{System, SystemExt}; use crate::types::fact::Fact; @@ -28,3 +30,78 @@ impl Fact for SystemData { } } } + +pub struct DMIData {} +impl DMIData {} + +impl Fact for DMIData { + fn gather(&self) -> String { + json!({ + "form_factor": get_form_factor("/sys/devices/virtual/dmi/id/chassis_type"), + }) + .to_string() + } +} + +fn get_form_factor(path: &str) -> &str { + // Get form factor info + // as learned from Ansible, we need to have the list in a specific order, + // see https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.2.0.pdf + + let ff: Vec<&str> = vec![ + "Unknown", + "Other", + "Unknown", + "Desktop", + "Low Profile Desktop", + "Pizza Box", + "Mini Tower", + "Tower", + "Portable", + "Laptop", + "Notebook", + "Hand Held", + "Docking Station", + "All In One", + "Sub Notebook", + "Space-saving", + "Lunch Box", + "Main Server Chassis", + "Expansion Chassis", + "Sub Chassis", + "Bus Expansion Chassis", + "Peripheral Chassis", + "RAID Chassis", + "Rack Mount Chassis", + "Sealed-case PC", + "Multi-system", + "CompactPCI", + "AdvancedTCA", + "Blade", + "Blade Enclosure", + "Tablet", + "Convertible", + "Detachable", + "IoT Gateway", + "Embedded PC", + "Mini PC", + "Stick PC", + ]; + + let filecontent: &str = match fs::read(path) { + Err(_) => ff[0], + Ok(x) => { + let ind: i32 = match str::from_utf8(&x) { + Ok(x) => x.trim().parse::().unwrap(), + Err(_) => 0, + }; + let out: &str = if ff.len() <= ind as usize { + ff[0] + } else { + ff[ind as usize] + }; + out + } + }; + filecontent +} diff --git a/src/main.rs b/src/main.rs index 5ffaa0a..ccac1b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use crate::gatherers::environment::EnvironmentData; use crate::gatherers::ip::{IPData, IPRouteData}; use crate::types::fact::Fact; use clap::Parser; -use gatherers::system::SystemData; +use gatherers::system::{DMIData, SystemData}; use serde_json::Value; use std::collections::{HashMap, HashSet}; use yaml_rust::{YamlEmitter, YamlLoader}; @@ -33,6 +33,12 @@ fn gather_list(gatherers: HashSet, output: &str) -> String { let mut outmap: HashMap = HashMap::new(); for g in gatherers { match g.as_str() { + "dmi" => { + outmap.insert( + "dmi".to_string(), + serde_json::from_str(&DMIData {}.gather()).unwrap(), + ); + } "env" => { outmap.insert( "environment".to_string(), @@ -84,6 +90,7 @@ fn main() { _ => "yaml", }; let all: HashSet = HashSet::from([ + "dmi".to_string(), "env".to_string(), "ipaddr".to_string(), "iproute".to_string(),