Skip to content

Commit

Permalink
Add system_info attributes to group system hardware info
Browse files Browse the repository at this point in the history
  • Loading branch information
knd2122 committed Jan 25, 2025
1 parent cd63e56 commit 64a43df
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/bin/hardware_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!("System Summary:");
println!("==============");
println!("Hostname: {}", server_info.hostname);
println!("System UUID: {}", server_info.summary.system_uuid);
println!("System Serial: {}", server_info.summary.system_serial);
println!("System UUID: {}", server_info.summary.system_info.uuid);
println!("System Serial: {}", server_info.summary.system_info.serial);
println!("CPU: {}", server_info.summary.cpu_summary);
println!(
"Total: {} Cores, {} Threads",
Expand Down Expand Up @@ -266,11 +266,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Handle posting if enabled
if opt.post {
// Validate endpoint when posting is enabled
if opt.endpoint.trim().is_empty() {
return Err("Endpoint URL is required when --post is enabled".into());
}

let labels: HashMap<String, String> = opt.labels.into_iter().collect();
post_data(
server_info,
Expand Down
31 changes: 23 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ pub struct MotherboardInfo {
pub type_: String,
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct SystemInfo {
pub uuid: String,
pub serial: String,
pub product_name: String,
pub product_manufacturer: String,
}

/// Summary of key system components
#[derive(Debug, Serialize, Deserialize)]
pub struct SystemSummary {
/// System UUID
pub system_uuid: String,
/// System Serial Number
pub system_serial: String,
/// System information
pub system_info: SystemInfo,
/// Total system memory capacity
pub total_memory: String,
/// Memory speed and type
Expand Down Expand Up @@ -689,15 +689,26 @@ impl ServerInfo {
return Ok(SystemInfo {
uuid: "Unknown".to_string(),
serial: "Unknown".to_string(),
product_name: "Unknown".to_string(),
product_manufacturer: "Unknown".to_string(),
});
}

let uuid = Self::extract_dmidecode_value(&stdout, "UUID")
.unwrap_or_else(|_| "Unknown".to_string());
let serial = Self::extract_dmidecode_value(&stdout, "Serial Number")
.unwrap_or_else(|_| "Unknown".to_string());
let product_name = Self::extract_dmidecode_value(&stdout, "Product Name")
.unwrap_or_else(|_| "Unknown".to_string());
let product_manufacturer = Self::extract_dmidecode_value(&stdout, "Manufacturer")
.unwrap_or_else(|_| "Unknown".to_string());

Ok(SystemInfo { uuid, serial })
Ok(SystemInfo {
uuid,
serial,
product_name,
product_manufacturer,
})
}

/// Collects all server information
Expand Down Expand Up @@ -1027,6 +1038,12 @@ impl ServerInfo {
let total_storage_tb = Self::calculate_total_storage_tb(&hardware.storage)?;

Ok(SystemSummary {
system_info: SystemInfo {
uuid: system_info.uuid.clone(),
serial: system_info.serial.clone(),
product_name: system_info.product_name.clone(),
product_manufacturer: system_info.product_manufacturer.clone(),
},
total_memory: hardware.memory.total.clone(),
memory_config: format!("{} @ {}", hardware.memory.type_, hardware.memory.speed),
total_storage_tb,
Expand All @@ -1040,8 +1057,6 @@ impl ServerInfo {
numa_topology: Self::collect_numa_topology()?,
cpu_topology,
cpu_summary,
system_uuid: system_info.uuid.clone(),
system_serial: system_info.serial.clone(),
})
}

Expand Down
11 changes: 9 additions & 2 deletions src/posting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ pub async fn post_data(

// Write payload to file if path is provided
if let Some(path) = write_payload_to {
std::fs::write(path, serde_json::to_string_pretty(&payload)?)?;
println!("\nPayload has been saved to {}", path);
match std::fs::write(path, serde_json::to_string_pretty(&payload)?) {
Ok(_) => println!("Successfully saved payload to {}", path),
Err(e) => eprintln!("Failed to write payload to {}: {}", path, e),
}
}

// Validate endpoint when posting is enabled
if endpoint.trim().is_empty() {
return Err("Endpoint URL is required when --post is enabled".into());
}

let mut request = reqwest::Client::new().post(endpoint).json(&payload);
Expand Down

0 comments on commit 64a43df

Please sign in to comment.