ram and cpu name

This commit is contained in:
SadlyNotSappho 2023-03-17 13:06:07 -07:00
parent 7ecb831db3
commit 72cc42770a
3 changed files with 67 additions and 14 deletions

View File

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
byte-unit = "4.0.19"
dotenv-parser = "0.1.3" dotenv-parser = "0.1.3"
regex = "1.7.1" regex = "1.7.1"
serde = {version = "1.0.154", features = ["derive"]} serde = {version = "1.0.154", features = ["derive"]}

View File

@ -1,5 +1,5 @@
use dotenv_parser::parse_dotenv; 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 { pub fn get_os() -> String {
let os_release = read_to_string("/etc/os-release").unwrap(); let os_release = read_to_string("/etc/os-release").unwrap();
@ -12,15 +12,6 @@ pub fn get_os() -> String {
.to_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 { pub fn get_arch() -> String {
let binding = std::process::Command::new("uname") let binding = std::process::Command::new("uname")
.arg("-m") .arg("-m")
@ -28,6 +19,7 @@ pub fn get_arch() -> String {
.expect("uname failed"); .expect("uname failed");
let arch = String::from_utf8_lossy(&binding.stdout); let arch = String::from_utf8_lossy(&binding.stdout);
// ram
arch.to_string().trim().to_string() 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 meminfo = read_to_string("/proc/meminfo").expect("couldnt read meminfo file");
let mut parsed: HashMap<String, String> = HashMap::new(); let mut parsed: HashMap<String, String> = HashMap::new();
@ -65,5 +57,57 @@ pub fn get_ram() {
let li = l.join(": "); let li = l.join(": ");
parsed.insert(name.to_string(), li.trim().to_string()); 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
} }

View File

@ -5,13 +5,21 @@ fn main() {
let kernel = get_kernel_ver(); let kernel = get_kernel_ver();
let uptime = get_uptime(); let uptime = get_uptime();
let ram = get_ram(); let ram = get_ram();
let cpus = get_cpu();
// ram
// storage // storage
// cpu name/usage // cpu name/usage
// gpu name/usage (not sure, might need external packages) // gpu name/usage (not sure, might need external packages)
// packages/package manager(s) // packages/package manager(s)
// user/hostname // 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);
} }