// Gather and display system information // Copyright (C) 2021 Lucien Cartier-Tilet // // 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 . use serde::Serialize; use sysinfo::{DiskExt, System, SystemExt}; #[derive(Debug, Serialize)] pub struct Memory { pub name: String, pub size: u64, pub available: u64, } impl Memory { pub fn new(name: String, size: u64, available: u64) -> Self { Self { name, size, available, } } pub fn used(&self) -> u64 { self.size - self.available } pub fn percentage(&self) -> f32 { self.used() as f32 / self.size as f32 } } #[derive(Debug, Serialize)] pub struct Machine { pub os: String, pub kernel: String, pub hostname: String, pub uptime: u64, pub ram: Memory, pub swap: Memory, pub disks: Vec, } 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.mount_point().to_str().unwrap().to_string(), x.total_space(), x.available_space(), ) }) .collect::>(); // 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, ); Self { os: sys.name().unwrap(), kernel: sys.kernel_version().unwrap(), hostname: sys.host_name().unwrap(), uptime: sys.uptime(), ram, swap, disks, } } } impl Default for Machine { fn default() -> Self { Self::new() } }