pumo-system-info/src/sysinfo.rs

72 lines
2.1 KiB
Rust

// Gather and display system information
// Copyright (C) 2021 Lucien Cartier-Tilet <lucien@phundrak.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use serde::Serialize;
use sysinfo::{System, SystemExt, DiskExt};
#[derive(Debug, Serialize)]
struct Memory {
name: String,
size: u64,
available: u64
}
impl Memory {
pub fn new(name: String, size: u64, available: u64) -> Self {
Self { name, size, available }
}
}
#[derive(Debug, Serialize)]
pub struct Machine {
os: String,
kernel: String,
hostname: String,
uptime: u64,
ram: Memory,
swap: Memory,
disks: Vec<Memory>,
}
impl Machine {
pub fn new() -> Self {
let mut sys = System::new_all();
sys.refresh_all();
let disks = sys
.disks()
.iter()
.map(|x| {
Memory::new(
x.name().to_str().unwrap().to_string(),
x.total_space(),
x.available_space(),
)
})
.collect::<Vec<Memory>>();
let ram = Memory::new("RAM".into(), sys.available_memory(), sys.available_memory());
let swap = Memory::new("Swap".into(), sys.total_swap(), sys.free_swap());
Self {
os: sys.name().unwrap(),
kernel: sys.kernel_version().unwrap(),
hostname: sys.host_name().unwrap(),
uptime: sys.uptime(),
ram,
swap,
disks
}
}
}