Skip to content

Commit

Permalink
fetched cpus usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MohdMohsin97 committed Jul 16, 2024
0 parents commit a3fb454
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "system-monitoring-cli"
version = "0.1.0"
edition = "2021"

[dependencies]
103 changes: 103 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use std::fs::File;
use std::io::{self, BufRead, Error, ErrorKind, Result};
use std::thread::sleep;
use std::time::Duration;

#[derive(Debug)]
struct CpuTimes {
cpu: String,
user: u64,
nice: u64,
system: u64,
idle: u64,
iowait: u64,
irq: u64,
softirq: u64,
steal: u64,
guest: u64,
guest_nice: u64,
}

fn read_cpu_times() -> Result<Vec<CpuTimes>> {
if let Ok(file) = File::open("/proc/stat") {
let reader = io::BufReader::new(file);

let mut cpu_times: Vec<CpuTimes> = Vec::new();
for line in reader.lines() {
let line = line?;
if line.starts_with("cpu") {
let times = parse_cputimes(line);
cpu_times.push(times);
}
}
return Ok(cpu_times);
}
Err(Error::new(ErrorKind::NotFound, "CPU data not found"))
}

fn parse_cputimes(line: String) -> CpuTimes {
let parts: Vec<&str> = line.split_whitespace().collect();
let times = CpuTimes {
cpu: parts[0].to_owned(),
user: parts[1].parse().unwrap(),
nice: parts[2].parse().unwrap(),
system: parts[3].parse().unwrap(),
idle: parts[4].parse().unwrap(),
iowait: parts[5].parse().unwrap(),
irq: parts[6].parse().unwrap(),
softirq: parts[7].parse().unwrap(),
steal: parts[8].parse().unwrap(),
guest: parts[9].parse().unwrap(),
guest_nice: parts[10].parse().unwrap(),
};
times
}

fn calculate_cpu_usage<'a>(
prev_times: &'a Vec<CpuTimes>,
curr_times: &'a Vec<CpuTimes>,
) -> Vec<(&'a str, f64)> {
let mut cpu_percentages: Vec<(&str, f64)> = Vec::new();

for i in 0..prev_times.len() {
let prev = &prev_times[i];
let curr = &curr_times[i];

let prev_idle = prev.idle + prev.iowait;
let curr_idle = curr.idle + curr.iowait;

let prev_non_idle =
prev.user + prev.nice + prev.system + prev.irq + prev.softirq + prev.steal;
let curr_non_idle =
curr.user + curr.nice + curr.system + curr.irq + curr.softirq + curr.steal;

let prev_total = prev_idle + prev_non_idle;
let curr_total = curr_idle + curr_non_idle;

let totald = curr_total - prev_total;
let idled = curr_idle - prev_idle;

let cpu_percentage = (totald - idled) as f64 / totald as f64 * 100.0;

cpu_percentages.push((&prev.cpu, cpu_percentage));
}

cpu_percentages
}

fn main() {
loop {
// First snapshot
let prev = read_cpu_times().expect("Failed to read CPU times");
// Wait for a second
sleep(Duration::new(1, 0));
// Second snapshot
let curr = read_cpu_times().expect("Failed to read CPU times");

let cpu_usage = calculate_cpu_usage(&prev, &curr);

for usage in cpu_usage {
println!("{} usage is {:.2}", usage.0 , usage.1);
}
}
}

0 comments on commit a3fb454

Please sign in to comment.