first commit

This commit is contained in:
2024-05-08 21:59:36 +01:00
commit 710f76b80a
7 changed files with 2617 additions and 0 deletions

103
src/main.rs Normal file
View File

@@ -0,0 +1,103 @@
extern crate rocket;
use rocket::serde::Serialize;
use rocket::{get, launch, routes};
use rocket_dyn_templates::{context, Template};
use readable::byte::Byte;
use readable::up::UptimeFull;
use std::path::Path;
use sysinfo::{Components, Disks, Networks, System};
#[derive(Serialize, Debug)]
struct ComponentInfo {
name: String,
temperature: u8, // Surely nothing's getting higher than 256 degrees
}
#[derive(Serialize, Debug)]
struct DiskInfo {
name: String,
total_space: String,
available_space: String,
mount_point: String,
}
#[derive(Serialize, Debug)]
struct RamInfo {
total_memory: String,
used_memory: String,
total_swap: String,
used_swap: String,
}
#[derive(Serialize, Debug)]
struct SysInfo {
ram: RamInfo,
disks: Vec<DiskInfo>,
components: Vec<ComponentInfo>,
hostname: Option<String>,
operating_system: Option<String>,
uptime: String,
average_load: (String, String, String),
restart_needed: bool,
}
fn get_system_info() -> SysInfo {
let mut sys = System::new_all();
sys.refresh_all();
let cpu_load = System::load_average();
let cpu_load = (format!("{:.3}", cpu_load.one), format!("{:.3}", cpu_load.five), format!("{:.3}", cpu_load.fifteen));
let disks = Disks::new_with_refreshed_list();
let mut disks_context = Vec::new();
for disk in disks.list() {
disks_context.push(DiskInfo {
name: disk.name().to_str().unwrap().to_string(),
total_space: Byte::from(disk.total_space()).to_string(),
available_space: Byte::from(disk.available_space()).to_string(),
mount_point: disk.mount_point().display().to_string(),
});
}
let components = Components::new_with_refreshed_list();
let mut components_context = Vec::new();
for component in &components {
components_context.push(ComponentInfo {
name: component.label().to_string(),
temperature: component.temperature() as u8,
})
}
SysInfo {
disks: disks_context,
components: components_context,
ram: RamInfo {
total_memory: Byte::from(sys.total_memory()).to_string(),
used_memory: Byte::from(sys.used_memory()).to_string(),
total_swap: Byte::from(sys.total_swap()).to_string(),
used_swap: Byte::from(sys.used_swap()).to_string()
},
hostname: System::host_name(),
operating_system: System::long_os_version(),
uptime: UptimeFull::from(System::uptime()).to_string(),
average_load: cpu_load,
restart_needed: Path::new("/var/run/reboot-required").exists(),
}
}
#[get("/")]
fn index() -> Template {
let system_context = get_system_info();
let context = context! {
system: system_context,
};
Template::render("index", &context)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![index])
.attach(Template::fairing())
}