2021-12-09 02:07:58 +00:00
|
|
|
|
// 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;
|
2021-12-17 16:20:05 +00:00
|
|
|
|
use sysinfo::{DiskExt, System, SystemExt};
|
2021-12-09 02:07:58 +00:00
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
2021-12-09 05:02:04 +00:00
|
|
|
|
pub struct Memory {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub size: u64,
|
2021-12-17 16:20:05 +00:00
|
|
|
|
pub available: u64,
|
2021-12-09 02:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Memory {
|
|
|
|
|
pub fn new(name: String, size: u64, available: u64) -> Self {
|
2021-12-17 16:20:05 +00:00
|
|
|
|
Self {
|
|
|
|
|
name,
|
|
|
|
|
size,
|
|
|
|
|
available,
|
|
|
|
|
}
|
2021-12-09 02:07:58 +00:00
|
|
|
|
}
|
2021-12-09 05:02:04 +00:00
|
|
|
|
|
|
|
|
|
pub fn used(&self) -> u64 {
|
|
|
|
|
self.size - self.available
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn percentage(&self) -> f32 {
|
|
|
|
|
self.used() as f32 / self.size as f32
|
|
|
|
|
}
|
2021-12-09 02:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
|
pub struct Machine {
|
2021-12-09 05:02:04 +00:00
|
|
|
|
pub os: String,
|
|
|
|
|
pub kernel: String,
|
|
|
|
|
pub hostname: String,
|
|
|
|
|
pub uptime: u64,
|
|
|
|
|
pub ram: Memory,
|
|
|
|
|
pub swap: Memory,
|
|
|
|
|
pub disks: Vec<Memory>,
|
2021-12-09 02:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Machine {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
let mut sys = System::new_all();
|
|
|
|
|
sys.refresh_all();
|
|
|
|
|
let disks = sys
|
|
|
|
|
.disks()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|x| {
|
|
|
|
|
Memory::new(
|
2021-12-09 05:02:04 +00:00
|
|
|
|
x.mount_point().to_str().unwrap().to_string(),
|
2021-12-09 02:07:58 +00:00
|
|
|
|
x.total_space(),
|
|
|
|
|
x.available_space(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<Memory>>();
|
2021-12-17 16:20:05 +00:00
|
|
|
|
// Size of RAM and Swap is returned in KB, unlike the drives’
|
|
|
|
|
// size which is in byte
|
|
|
|
|
let ram = Memory::new(
|
|
|
|
|
"RAM".into(),
|
|
|
|
|
sys.total_memory() * 1000,
|
|
|
|
|
sys.available_memory() * 1000,
|
|
|
|
|
);
|
|
|
|
|
let swap = Memory::new(
|
|
|
|
|
"Swap".into(),
|
|
|
|
|
sys.total_swap() * 1000,
|
|
|
|
|
sys.free_swap() * 1000,
|
|
|
|
|
);
|
2021-12-09 02:07:58 +00:00
|
|
|
|
Self {
|
|
|
|
|
os: sys.name().unwrap(),
|
|
|
|
|
kernel: sys.kernel_version().unwrap(),
|
|
|
|
|
hostname: sys.host_name().unwrap(),
|
|
|
|
|
uptime: sys.uptime(),
|
|
|
|
|
ram,
|
|
|
|
|
swap,
|
2021-12-17 16:20:05 +00:00
|
|
|
|
disks,
|
2021-12-09 02:07:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-17 16:20:05 +00:00
|
|
|
|
|
|
|
|
|
impl Default for Machine {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|