目录
一、用Rust获取CPU、内存、硬盘的信息
二、知识点 systemstat
一、用Rust获取CPU、内存、硬盘的信息
首先,需要添加systemstat
库到Cargo.toml
文件:
[dependencies]
systemstat = "0.8.2"
在Rust代码中使用它:
extern crate systemstat;use std::io;
use systemstat::{MemInfo, System, CpuStats};fn main() -> io::Result<()> {let mut cpu_stats = CpuStats::new()?;cpu_stats.update()?;let mem_info = MemInfo::new()?;let system = System::new()?;println!("CPU Usage: {:.2}%", cpu_stats.usage());println!("Total Memory: {} MiB", mem_info.total() / 1_048_576);println!("Free Memory: {} MiB", mem_info.free() / 1_048_576);println!("Total Disk Space: {} GiB", system.disks_total_space() / 1_073_741_824);println!("Free Disk Space: {} GiB", system.disks_free_space() / 1_073_741_824);Ok(())
}
二、知识点 systemstat
systemstat - RustThis library provides a way to access system information such as CPU load, mounted filesystems, network interfaces, etc.https://docs.rs/systemstat/latest/systemstat/
GitHub - valpackett/systemstat: Rust library for getting system information | also on https://codeberg.org/valpackett/systemstat
systemstat
A Rust library for getting system information/statistics:
- CPU load
- load average
- memory usage
- uptime / boot time
- battery life
- filesystem mounts (and disk usage)
- disk I/O statistics
- network interfaces
- network traffic statistics
- CPU temperature
Unlike sys-info-rs, this one is written purely in Rust.
Supported platforms (roughly ordered by completeness of support):
- FreeBSD
- Linux
- OpenBSD
- Windows
- macOS
- NetBSD
- more coming soon