Added Networks and some style changes

This commit is contained in:
2024-05-26 16:36:21 +01:00
parent 0ec598eadb
commit abc0428d25
2 changed files with 73 additions and 40 deletions

View File

@@ -1,9 +1,9 @@
extern crate rocket;
use readable::byte::Byte;
use readable::up::UptimeFull;
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 reqwest;
use std::collections::HashMap;
@@ -37,11 +37,19 @@ struct RamInfo {
used_swap: String,
}
#[derive(Serialize, Debug)]
struct NetworkInfo {
name: String,
total_sent: String,
total_received: String,
}
#[derive(Serialize, Debug)]
struct SysInfo {
ram: RamInfo,
disks: Vec<DiskInfo>,
components: Vec<ComponentInfo>,
network_ifs: Vec<NetworkInfo>,
hostname: Option<String>,
operating_system: Option<String>,
uptime: String,
@@ -53,7 +61,11 @@ fn get_system_info() -> SysInfo {
let mut sys = System::new_all();
sys.refresh_all();
let cpu_load = System::load_average();
let cpu_load = (format!("{:.2}", cpu_load.one), format!("{:.2}", cpu_load.five), format!("{:.2}", cpu_load.fifteen));
let cpu_load = (
format!("{:.2}", cpu_load.one),
format!("{:.2}", cpu_load.five),
format!("{:.2}", cpu_load.fifteen),
);
let disks = Disks::new_with_refreshed_list();
let mut disks_context = Vec::new();
@@ -79,6 +91,15 @@ fn get_system_info() -> SysInfo {
})
}
let networks = Networks::new_with_refreshed_list();
let mut network_ifs = Vec::new();
for (interface_name, network) in &networks {
network_ifs.push(NetworkInfo {
name: interface_name.to_string(),
total_sent: Byte::from(network.total_transmitted()).to_string(),
total_received: Byte::from(network.total_received()).to_string()
});
}
SysInfo {
disks: disks_context,
@@ -87,8 +108,9 @@ fn get_system_info() -> SysInfo {
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()
used_swap: Byte::from(sys.used_swap()).to_string(),
},
network_ifs: network_ifs,
hostname: System::host_name(),
operating_system: System::long_os_version(),
uptime: UptimeFull::from(System::uptime()).to_string(),