diff --git a/src/features/mod.rs b/src/features/mod.rs index 30f6eca..44d7279 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -1,5 +1,6 @@ pub mod kernel; pub mod kernel_websocket; +pub mod model; pub mod netstat; pub mod platform; pub mod serial; diff --git a/src/features/model.rs b/src/features/model.rs new file mode 100644 index 0000000..19f8b28 --- /dev/null +++ b/src/features/model.rs @@ -0,0 +1,54 @@ +use std::fs; +use std::process::Command; +use paperclip::actix::Apiv2Schema; +use serde::Serialize; + +#[derive(Debug, Serialize, Apiv2Schema)] +pub struct HardwareModel { + model: String, + arch: String, + cpu_name: String, +} + +impl HardwareModel { + pub fn new() -> Self { + Self { + model: Self::get_model(), + arch: Self::get_arch(), + cpu_name: Self::get_cpu_name(), + } + } + + fn get_model() -> String { + if let Ok(model) = fs::read_to_string("/proc/device-tree/model") { + return model.trim().trim_matches(char::from(0)).to_string(); + } + + fs::read_to_string("/sys/devices/virtual/dmi/id/product_name") + .unwrap_or_else(|_| "Unknown".to_string()) + .trim().trim_matches(char::from(0)) + .to_string() + } + + fn get_arch() -> String { + String::from_utf8(match Command::new("uname").arg("-m").output(){ + Ok(output) => output.stdout, + Err(_) => return "Unknown".to_string(), + }).unwrap().trim().trim_matches(char::from(0)).to_string() + } + + fn get_cpu_name() -> String { + let stdout = String::from_utf8(match Command::new("lscpu") + .output() { + Ok(output) => output.stdout, + Err(_) => return "Unknown".to_string(), + }).unwrap_or_else(|_| "Unknown".to_string()); + + stdout + .lines() + .find(|line| line.starts_with("Model name:")) + .and_then(|line| line.split(':').nth(1)) + .map(|s| s.trim().trim_matches(char::from(0)).to_string()) + .unwrap_or_else(|| "Unknown".to_string()) + } +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 8c9c55a..91c63a6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -22,6 +22,7 @@ pub fn run(server_address: &str) { web::get().to(pages::root), ) .route("/kernel_buffer", web::get().to(pages::kernel_buffer)) + .route("/model", web::get().to(pages::model)) .route("/netstat", web::get().to(pages::netstat)) .route("/platform", web::get().to(pages::platform)) .route("/serial", web::get().to(pages::serial)) diff --git a/src/server/pages.rs b/src/server/pages.rs index c3610ed..e62ef6b 100644 --- a/src/server/pages.rs +++ b/src/server/pages.rs @@ -182,6 +182,16 @@ pub async fn platform(req: HttpRequest) -> HttpResponse { } } +#[api_v2_operation] +/// Provide hardware model information +pub async fn model(req: HttpRequest) -> HttpResponse { + debug!("{:#?}", req); + + HttpResponse::Ok() + .content_type("application/json") + .body(serde_json::to_string_pretty(&features::model::HardwareModel::new()).unwrap()) +} + pub fn websocket_kernel_buffer(req: HttpRequest, stream: web::Payload) -> HttpResponse { debug!("{:#?}", req);