Adds first dmi parameter: form_factor
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jochen Maes 2023-11-03 06:49:44 +01:00
parent f186860a45
commit b93ecb13b6
2 changed files with 85 additions and 1 deletions

View File

@ -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::<i32>().unwrap(),
Err(_) => 0,
};
let out: &str = if ff.len() <= ind as usize {
ff[0]
} else {
ff[ind as usize]
};
out
}
};
filecontent
}

View File

@ -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<String>, output: &str) -> String {
let mut outmap: HashMap<String, Value> = 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<String> = HashSet::from([
"dmi".to_string(),
"env".to_string(),
"ipaddr".to_string(),
"iproute".to_string(),