From 24acf619fe0a2ac05886ba4afdf38225e9805336 Mon Sep 17 00:00:00 2001 From: Tinggu-mansi Date: Mon, 15 Dec 2025 20:22:10 +0530 Subject: [PATCH] feat: Add disk module collector and update documentation --- README.md | 41 +++++++----------------------------- src/lib.rs | 6 ++++-- src/modules/disk.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/modules/mod.rs | 3 ++- 4 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 src/modules/disk.rs diff --git a/README.md b/README.md index 7424a77..604ccde 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ gim is a fast, high-performance, modular system metrics and diagnostics CLI tool ## Features -- **Modular Design**: Easy to extend with new metric collectors -- **Multiple Output Formats**: JSON, table, and raw output modes -- **Real-time Metrics**: CPU and memory usage statistics -- **Clean Architecture**: Well-structured codebase for easy maintenance +- *Modular Design*: Easy to extend with new metric collectors +- *Multiple Output Formats*: JSON, table, and raw output modes +- *Real-time Metrics*: CPU and memory usage statistics +- *Clean Architecture*: Well-structured codebase for easy maintenance ## Use Cases @@ -22,45 +22,20 @@ gim is a fast, high-performance, modular system metrics and diagnostics CLI tool ```bash # Clone the repository -git clone https://github.com/your-repo/gim.git +git clone [https://github.com/your-repo/gim.git](https://github.com/your-repo/gim.git) # Build the project cargo build --release -# Run with default settings +# Run with default settings (now includes CPU, Memory, and Disk) cargo run # Run a specific module cargo run -- --module cpu cargo run -- --module memory +cargo run -- --module disk # <--- Disk Module # Use different output formats cargo run -- --output json cargo run -- --output raw -cargo run -- --module cpu --output json -``` - -## Current Capabilities - -- **CPU Metrics**: Usage percentage, core count -- **Memory Metrics**: Total, used, free, available memory and swap -- **Output Options**: JSON, formatted table, or raw key-value pairs - -## Planned Features - -- Disk and network metric collectors -- TUI dashboard with ratatui -- Configuration file support -- Live monitoring capabilities - -## Documentation - -- [API Documentation](docs/api.md) -- [Architecture](docs/architecture.md) -- [Modules Guide](docs/modules.md) -- [Extending Guide](docs/extending.md) -- [Contributing](CONTRIBUTING.md) - -## License - -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. +cargo run -- --module cpu --output json \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f35d282..a4a4f04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod output; pub mod tui; use crate::core::MetricCollector; -use crate::modules::{cpu::CpuCollector, memory::MemoryCollector}; +use crate::modules::{cpu::CpuCollector, memory::MemoryCollector, disk::DiskCollector}; use crate::output::{OutputFormat, format_output}; pub fn run() { @@ -15,6 +15,7 @@ pub fn run() { let collectors: Vec> = match args.module.as_deref() { Some("cpu") => vec![Box::new(CpuCollector::new())], Some("memory") => vec![Box::new(MemoryCollector::new())], + Some("disk") => vec![Box::new(DiskCollector::new())], // <--- Some(_) => { eprintln!("Unknown module: {}", args.module.unwrap()); std::process::exit(1); @@ -24,6 +25,7 @@ pub fn run() { vec![ Box::new(CpuCollector::new()), Box::new(MemoryCollector::new()), + Box::new(DiskCollector::new()), // <--- default collectors ] } }; @@ -51,4 +53,4 @@ pub fn run() { Err(e) => eprintln!("Error collecting {} metrics: {}", collector.name(), e), } } -} +} \ No newline at end of file diff --git a/src/modules/disk.rs b/src/modules/disk.rs new file mode 100644 index 0000000..b9ebb67 --- /dev/null +++ b/src/modules/disk.rs @@ -0,0 +1,51 @@ +use serde::Serialize; +use sysinfo::{DiskExt, System, SystemExt}; +use crate::metrics::MetricCollector; // मान लो 'metrics' crate में 'MetricCollector' है + +// 1. डेटा स्ट्रक्चर: हम जो डेटा कलेक्ट करेंगे, वह ऐसा दिखेगा +#[derive(Debug, Serialize)] +pub struct DiskMetric { + pub name: String, + pub total_space_gb: f64, + pub used_space_gb: f64, + pub free_space_gb: f64, + pub file_system: String, +} + +// 2. MetricCollector को इम्प्लीमेंट करना +impl MetricCollector for DiskMetric { + // यह फ़ंक्शन sysinfo का इस्तेमाल करके डिस्क की जानकारी कलेक्ट करेगा + fn collect() -> Result, String> { + let mut sys = System::new(); + // Disks की लिस्ट को रिफ्रेश करते हैं + sys.refresh_disks_list(); + + let mut metrics = Vec::new(); + + // हर डिस्क के लिए लूप चलाओ + for disk in sys.disks() { + // बाइट्स को GB में कन्वर्ट करने के लिए + const BYTE_TO_GB: f64 = 1024.0 * 1024.0 * 1024.0; + + let total_bytes = disk.total_space(); + let available_bytes = disk.available_space(); + let used_bytes = total_bytes - available_bytes; + + // डेटा को स्ट्रक्चर में भर दो + let metric = DiskMetric { + name: disk.name().to_string_lossy().into_owned(), + total_space_gb: total_bytes as f64 / BYTE_TO_GB, + used_space_gb: used_bytes as f64 / BYTE_TO_GB, + free_space_gb: available_bytes as f64 / BYTE_TO_GB, + file_system: String::from_utf8_lossy(disk.file_system()).into_owned(), + }; + metrics.push(metric); + } + + if metrics.is_empty() { + Err("Koi disk metrics collect nahi hue.".to_string()) + } else { + Ok(metrics) + } + } +} \ No newline at end of file diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 4c45c9b..92528e4 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,2 +1,3 @@ pub mod cpu; -pub mod memory; \ No newline at end of file +pub mod memory; +pub mod disk; \ No newline at end of file