Skip to content

Commit ffca248

Browse files
authored
Merge pull request #3 from arybhatt4533/add-disk-module
Add Disk Usage Metric Collector
2 parents 4ae2e93 + 6308a84 commit ffca248

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

src/cli/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
use clap::Parser;
23

34
#[derive(Parser)]

src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@ pub mod output;
55
pub mod tui;
66

77
use crate::core::MetricCollector;
8+
use crate::modules::disk::DiskCollector;
89
use crate::modules::{cpu::CpuCollector, memory::MemoryCollector};
910
use crate::output::{OutputFormat, format_output};
1011

1112
pub fn run() {
1213
let args = cli::parse_args();
1314

1415
// get list of collectors based on args
15-
let collectors: Vec<Box<dyn MetricCollector>> = match args.module.as_deref() {
16-
Some("cpu") => vec![Box::new(CpuCollector::new())],
17-
Some("memory") => vec![Box::new(MemoryCollector::new())],
18-
Some(_) => {
19-
eprintln!("Unknown module: {}", args.module.unwrap());
20-
std::process::exit(1);
21-
}
22-
None => {
23-
// default to all collectors
24-
vec![
25-
Box::new(CpuCollector::new()),
26-
Box::new(MemoryCollector::new()),
27-
]
28-
}
29-
};
16+
let collectors: Vec<Box<dyn MetricCollector>> = match args.module.as_deref() {
17+
Some("cpu") => vec![Box::new(CpuCollector::new())],
18+
Some("memory") => vec![Box::new(MemoryCollector::new())],
19+
Some("disk") => vec![Box::new(DiskCollector::new())],
20+
Some(_) => {
21+
eprintln!("Unknown module: {}", args.module.unwrap());
22+
std::process::exit(1);
23+
}
24+
None => {
25+
vec![
26+
Box::new(CpuCollector::new()),
27+
Box::new(MemoryCollector::new()),
28+
Box::new(DiskCollector::new()), // default me disk bhi add
29+
]
30+
}
31+
};
32+
3033

3134
// determine output format
3235
let format = match args.output.as_deref() {

src/modules/disk.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use sysinfo::{DiskExt, System, SystemExt};
2+
use std::collections::HashMap;
3+
use crate::core::{MetricCollector, MetricData, MetricValue};
4+
5+
pub struct DiskCollector;
6+
7+
impl DiskCollector {
8+
pub fn new() -> Self {
9+
DiskCollector
10+
}
11+
}
12+
13+
impl MetricCollector for DiskCollector {
14+
fn collect(&self) -> Result<MetricData, Box<dyn std::error::Error>> {
15+
let mut sys = System::new_all();
16+
sys.refresh_disks_list();
17+
18+
let mut total: u64 = 0;
19+
let mut used: u64 = 0;
20+
let mut free: u64 = 0;
21+
22+
for disk in sys.disks() {
23+
total += disk.total_space();
24+
free += disk.available_space();
25+
}
26+
27+
used = total - free;
28+
29+
let mut map = HashMap::new();
30+
map.insert("total".to_string(), MetricValue::Integer(total as i64));
31+
map.insert("used".to_string(), MetricValue::Integer(used as i64));
32+
map.insert("free".to_string(), MetricValue::Integer(free as i64));
33+
34+
Ok(MetricData {
35+
timestamp: std::time::SystemTime::now(),
36+
metrics: map,
37+
})
38+
}
39+
40+
fn name(&self) -> &'static str {
41+
"disk"
42+
}
43+
}

src/modules/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod cpu;
2-
pub mod memory;
2+
pub mod memory;
3+
pub mod disk;

0 commit comments

Comments
 (0)