如何用Rust获取CPU、内存、硬盘的信息?

目录

一、用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.icon-default.png?t=N7T8https://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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-14 08:16:03       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-14 08:16:03       74 阅读
  3. 在Django里面运行非项目文件

    2024-05-14 08:16:03       61 阅读
  4. Python语言-面向对象

    2024-05-14 08:16:03       71 阅读

热门阅读

  1. Vue.js:轻量级而强大的前端框架

    2024-05-14 08:16:03       30 阅读
  2. 02-登录页面、动态路由、权限等模块开发

    2024-05-14 08:16:03       27 阅读
  3. 23. 合并 K 个升序链表 - 力扣(LeetCode)

    2024-05-14 08:16:03       37 阅读
  4. 【设计模式】桥接模式-学习记录

    2024-05-14 08:16:03       23 阅读
  5. 量子计算入门:原理与编程

    2024-05-14 08:16:03       31 阅读
  6. MySQL和MongoDB区别

    2024-05-14 08:16:03       34 阅读
  7. k8s 配置管理

    2024-05-14 08:16:03       49 阅读
  8. Redis 5.0 Stream数据结构深入分析

    2024-05-14 08:16:03       28 阅读
  9. 力扣:93. 复原 IP 地址

    2024-05-14 08:16:03       31 阅读
  10. 数据库和Redis数据不一致的问题

    2024-05-14 08:16:03       30 阅读
  11. Rust 语言不支持 goto 语句

    2024-05-14 08:16:03       32 阅读
  12. ubuntu 24.04 devilspie 报错解决

    2024-05-14 08:16:03       35 阅读
  13. CircleCI的原理及应用详解(二)

    2024-05-14 08:16:03       26 阅读
  14. 10、Go Gin 连接Redis以及Cookie&Session

    2024-05-14 08:16:03       31 阅读