Skip to content

介绍

获取系统信息

rust
use sysinfo::{Components, Disks, Networks, System};

fn main() {
    let mut sys = System::new_all();
    sys.refresh_all();

    // memory
    println!("=> memory:");
    println!("{}(used)/:{}(total)", sys.used_memory(), sys.total_memory());

    // system information:
    println!("System: {:?}", System::name().unwrap());
    println!("Kernel: {:?}", System::kernel_version().unwrap());
    println!("OS: {:?}", System::os_version().unwrap());
    println!("Host:{:?}", System::host_name().unwrap());

    // number of CPUs:
    println!("CPUs: {}", sys.cpus().len());

    // list all process
    // for (pid, process) in sys.processes() {
    //     println!("[{pid}] {:?} {:?}", process.name(), process.disk_usage());
    // }

    // list all disk's information:
    println!("=> disks:");
    let disks = Disks::new_with_refreshed_list();
    for disk in &disks {
        println!("{:?}", disk);
    }

    // list all networks
    let networks = Networks::new_with_refreshed_list();
    println!("=> networks:");
    for (interface_name, data) in &networks {
        println!(
            "{interface_name}: {} B (down) / {} B (up)",
            data.total_received(),
            data.total_transmitted(),
        );
    }

    // Components temperature:
    let components = Components::new_with_refreshed_list();
    println!("=> components:");
    for component in &components {
        println!("{:?}", component);
    }
}
toml
[dependencies]
sysinfo = "0.32.0"

Released under the MIT License.