ram and cpu name
This commit is contained in:
parent
7ecb831db3
commit
72cc42770a
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
byte-unit = "4.0.19"
|
||||
dotenv-parser = "0.1.3"
|
||||
regex = "1.7.1"
|
||||
serde = {version = "1.0.154", features = ["derive"]}
|
||||
|
|
68
src/lib.rs
68
src/lib.rs
|
@ -1,5 +1,5 @@
|
|||
use dotenv_parser::parse_dotenv;
|
||||
use std::{fs::read_to_string, collections::HashMap};
|
||||
use std::{collections::HashMap, fs::read_to_string};
|
||||
|
||||
pub fn get_os() -> String {
|
||||
let os_release = read_to_string("/etc/os-release").unwrap();
|
||||
|
@ -12,15 +12,6 @@ pub fn get_os() -> String {
|
|||
.to_string()
|
||||
}
|
||||
|
||||
pub fn get_cpu() {
|
||||
let binding = std::process::Command::new("lscpu")
|
||||
.output()
|
||||
.expect("lscpu failed");
|
||||
let lscpu = String::from_utf8_lossy(&binding.stdout);
|
||||
|
||||
println!("{}", &lscpu)
|
||||
}
|
||||
|
||||
pub fn get_arch() -> String {
|
||||
let binding = std::process::Command::new("uname")
|
||||
.arg("-m")
|
||||
|
@ -28,6 +19,7 @@ pub fn get_arch() -> String {
|
|||
.expect("uname failed");
|
||||
let arch = String::from_utf8_lossy(&binding.stdout);
|
||||
|
||||
// ram
|
||||
arch.to_string().trim().to_string()
|
||||
}
|
||||
|
||||
|
@ -54,7 +46,7 @@ pub fn get_uptime() -> time_hms::TimeHms {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn get_ram() {
|
||||
pub fn get_ram() -> (String, String, String) {
|
||||
let meminfo = read_to_string("/proc/meminfo").expect("couldnt read meminfo file");
|
||||
|
||||
let mut parsed: HashMap<String, String> = HashMap::new();
|
||||
|
@ -65,5 +57,57 @@ pub fn get_ram() {
|
|||
let li = l.join(": ");
|
||||
parsed.insert(name.to_string(), li.trim().to_string());
|
||||
}
|
||||
println!("{:?}", parsed.get("MemAvailable").unwrap())
|
||||
|
||||
let available_kb = parsed.get("MemAvailable").unwrap();
|
||||
let total_kb = parsed.get("MemTotal").unwrap();
|
||||
|
||||
let available = byte_unit::Byte::from_str(available_kb)
|
||||
.unwrap()
|
||||
.get_appropriate_unit(true);
|
||||
let total = byte_unit::Byte::from_str(total_kb)
|
||||
.unwrap()
|
||||
.get_appropriate_unit(true);
|
||||
|
||||
(
|
||||
available.to_string(),
|
||||
total.to_string(),
|
||||
byte_unit::Byte::from_bytes(
|
||||
total.get_byte().get_bytes() - available.get_byte().get_bytes(),
|
||||
)
|
||||
.get_appropriate_unit(true)
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_cpu() -> HashMap<String, u16> {
|
||||
let cpuinfo = read_to_string("/proc/cpuinfo").expect("couldnt read cpuinfo file");
|
||||
let mut parsed: HashMap<String, u16> = HashMap::new();
|
||||
|
||||
let cores_base = cpuinfo.trim().split("\n\n");
|
||||
let mut cores: Vec<HashMap<String, String>> = vec![];
|
||||
|
||||
for core in cores_base {
|
||||
let mut c: HashMap<String, String> = HashMap::new();
|
||||
|
||||
for line in core.lines() {
|
||||
let mut l = line.split(": ").collect::<Vec<&str>>();
|
||||
let name = l.drain(0..1).collect::<Vec<&str>>()[0];
|
||||
let li = l.join(": ");
|
||||
c.insert(name.trim().to_string(), li.trim().to_string());
|
||||
};
|
||||
|
||||
cores.push(c);
|
||||
};
|
||||
|
||||
for core in cores {
|
||||
let names = parsed.keys().collect::<Vec<&String>>();
|
||||
|
||||
if names.contains(&core.get("model name").unwrap()) {
|
||||
parsed.insert(core.get("model name").unwrap().to_string(), parsed.get(core.get("model name").unwrap()).unwrap() + 1);
|
||||
} else {
|
||||
parsed.insert(core.get("model name").unwrap().to_string(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
parsed
|
||||
}
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -5,13 +5,21 @@ fn main() {
|
|||
let kernel = get_kernel_ver();
|
||||
let uptime = get_uptime();
|
||||
let ram = get_ram();
|
||||
let cpus = get_cpu();
|
||||
|
||||
// ram
|
||||
// storage
|
||||
// cpu name/usage
|
||||
// gpu name/usage (not sure, might need external packages)
|
||||
// packages/package manager(s)
|
||||
// user/hostname
|
||||
|
||||
println!("OS Name: {os}\nArchitecture: {arch}\nKernel Version: {kernel}\nUptime: {}:{}:{}", uptime.h(), uptime.m(), uptime.s());
|
||||
println!("OS Name: {os}\nArchitecture: {arch}\nKernel Version: {kernel}\nUptime: {}:{}:{}\nRAM: {} remaining ({}/{} used)", uptime.h(), uptime.m(), uptime.s(), ram.0, ram.2, ram.1);
|
||||
let cpu_name = cpus
|
||||
.keys()
|
||||
.collect::<Vec<&String>>()
|
||||
.iter()
|
||||
.map(|n| n.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ");
|
||||
println!("CPU Name(s): {}", cpu_name);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue