systeminfo example

This commit is contained in:
jb-alvarado 2023-11-06 16:19:26 +01:00
parent 63b9c0664a
commit 22fb1d78d2
4 changed files with 176 additions and 0 deletions

78
Cargo.lock generated
View File

@ -911,6 +911,30 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]] [[package]]
name = "crossbeam-queue" name = "crossbeam-queue"
version = "0.3.8" version = "0.3.8"
@ -1187,6 +1211,7 @@ dependencies = [
"simplelog", "simplelog",
"sqlx", "sqlx",
"static-files", "static-files",
"sysinfo",
"tokio", "tokio",
] ]
@ -1962,6 +1987,15 @@ version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.17" version = "0.3.17"
@ -2047,6 +2081,15 @@ dependencies = [
"walkdir", "walkdir",
] ]
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.4" version = "0.4.4"
@ -2375,6 +2418,26 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"
@ -3144,6 +3207,21 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sysinfo"
version = "0.29.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a18d114d420ada3a891e6bc8e96a2023402203296a47cdd65083377dad18ba5"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]] [[package]]
name = "system-configuration" name = "system-configuration"
version = "0.5.1" version = "0.5.1"

View File

@ -37,6 +37,7 @@ serde_json = "1.0"
serde_yaml = "0.9" serde_yaml = "0.9"
simplelog = { version = "0.12", features = ["paris"] } simplelog = { version = "0.12", features = ["paris"] }
static-files = "0.2" static-files = "0.2"
sysinfo = "0.29"
sqlx = { version = "0.7", features = ["runtime-tokio", "sqlite"] } sqlx = { version = "0.7", features = ["runtime-tokio", "sqlite"] }
tokio = { version = "1.29", features = ["full"] } tokio = { version = "1.29", features = ["full"] }

View File

@ -0,0 +1,97 @@
use std::cmp;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use sysinfo::{CpuExt, DiskExt, NetworkExt, System, SystemExt};
pub fn byte_convert(num: f64) -> String {
let negative = if num.is_sign_positive() { "" } else { "-" };
let num = num.abs();
let units = ["B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
if num < 1_f64 {
return format!("{}{} {}", negative, num, "B");
}
let delimiter = 1024_f64;
let exponent = cmp::min(
(num.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.3}", num / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{}{} {}", negative, pretty_bytes, unit)
}
fn main() {
let mut sys = System::new_all();
loop {
sys.refresh_all();
let mut usage = 0.0;
let count = sys.cpus().len() as f32;
for cpu in sys.cpus() {
usage += cpu.cpu_usage();
}
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
println!("System name: {:?}", sys.name());
println!("System kernel version: {:?}", sys.kernel_version());
println!("System OS version: {:?}\n", sys.os_version());
println!("total memory: {}", byte_convert(sys.total_memory() as f64));
println!("used memory : {}", byte_convert(sys.used_memory() as f64));
println!(
"free memory : {}\n",
byte_convert((sys.total_memory() - sys.used_memory()) as f64)
);
println!("total swap : {}", byte_convert(sys.total_swap() as f64));
println!("used swap : {}", byte_convert(sys.used_swap() as f64));
println!(
"free swap : {}\n",
byte_convert((sys.total_swap() - sys.used_swap()) as f64)
);
for disk in sys.disks() {
if disk.mount_point() == Path::new("/") {
println!(
"disk: {:?} | available space {}",
disk.name(),
byte_convert(disk.available_space() as f64),
);
}
}
println!();
let load_avg = sys.load_average();
for (interface_name, data) in sys.networks() {
if interface_name == "wlp5s0" {
println!(
"{}: (in / total) {} / {} | (out / total) {} / {}",
interface_name,
byte_convert(data.received() as f64),
byte_convert(data.total_received() as f64),
byte_convert(data.transmitted() as f64),
byte_convert(data.total_transmitted() as f64),
);
}
}
println!();
println!("CPU Usage: {:.2}%", usage * count / 100.0);
println!(
"Load: {}% {}% {}%\n",
load_avg.one, load_avg.five, load_avg.fifteen
);
sleep(Duration::from_secs(1))
}
}

View File