Informazioni di sistema in Rust con systemstat

Mattepuffo's logo
Informazioni di sistema in Rust con systemstat

Informazioni di sistema in Rust con systemstat

systemstat è un modulo per Rust per ottenere informazioni di sistema.

Difficilmente ho trovato un modulo / libreria cosi completo; anche paragonandolo ad altri linguaggi.

Infatti vi riporto direttamente il codice che trovate su GitHub, perchè contiene praticamente tutto.

Ed è anche facile da capire.

Per installarlo agigungete questa dipendenza a Cargo.toml:

[dependencies]
systemstat = "*"

Questo il codice:

extern crate systemstat;

use std::thread;
use std::time::Duration;
use systemstat::{System, Platform, saturating_sub_bytes};

fn main() {
    let sys = System::new();

    match sys.mounts() {
        Ok(mounts) => {
            println!("Mounts:");
            for mount in mounts.iter() {
                println!("{} ---{}---> {} (available {} of {})",
                         mount.fs_mounted_from, mount.fs_type, mount.fs_mounted_on, mount.avail, mount.total);
            }
        }
        Err(x) => println!("Mounts: error: {}", x)
    }

    match sys.block_device_statistics() {
        Ok(stats) => {
            for blkstats in stats.values() {
                println!("{}: {:?}", blkstats.name, blkstats);
            }
        }
        Err(x) => println!("Block statistics error: {}", x.to_string())
    }

    match sys.networks() {
        Ok(netifs) => {
            println!("Networks:");
            for netif in netifs.values() {
                println!("{} ({:?})", netif.name, netif.addrs);
            }
        }
        Err(x) => println!("Networks: error: {}", x)
    }

    match sys.networks() {
        Ok(netifs) => {
            println!("Network interface statistics:");
            for netif in netifs.values() {
                println!("{} statistics: ({:?})", netif.name, sys.network_stats(&netif.name));
            }
        }
        Err(x) => println!("Networks: error: {}", x)
    }

    match sys.battery_life() {
        Ok(battery) =>
            print!("Battery: {}%, {}h{}m remaining",
                   battery.remaining_capacity * 100.0,
                   battery.remaining_time.as_secs() / 3600,
                   battery.remaining_time.as_secs() % 60),
        Err(x) => print!("Battery: error: {}", x)
    }

    match sys.on_ac_power() {
        Ok(power) => println!(", AC power: {}", power),
        Err(x) => println!(", AC power: error: {}", x)
    }

    match sys.memory() {
        Ok(mem) => println!("Memory: {} used / {} ({} bytes) total ({:?})", saturating_sub_bytes(mem.total, mem.free), mem.total, mem.total.as_u64(), mem.platform_memory),
        Err(x) => println!("Memory: error: {}", x)
    }

    match sys.load_average() {
        Ok(loadavg) => println!("Load average: {} {} {}", loadavg.one, loadavg.five, loadavg.fifteen),
        Err(x) => println!("Load average: error: {}", x)
    }

    match sys.uptime() {
        Ok(uptime) => println!("Uptime: {:?}", uptime),
        Err(x) => println!("Uptime: error: {}", x)
    }

    match sys.boot_time() {
        Ok(boot_time) => println!("Boot time: {}", boot_time),
        Err(x) => println!("Boot time: error: {}", x)
    }

    match sys.cpu_load_aggregate() {
        Ok(cpu) => {
            println!("Measuring CPU load...");
            thread::sleep(Duration::from_secs(1));
            let cpu = cpu.done().unwrap();
            println!("CPU load: {}% user, {}% nice, {}% system, {}% intr, {}% idle ",
                     cpu.user * 100.0, cpu.nice * 100.0, cpu.system * 100.0, cpu.interrupt * 100.0, cpu.idle * 100.0);
        }
        Err(x) => println!("CPU load: error: {}", x)
    }

    match sys.cpu_temp() {
        Ok(cpu_temp) => println!("CPU temp: {}", cpu_temp),
        Err(x) => println!("CPU temp: {}", x)
    }

    match sys.socket_stats() {
        Ok(stats) => println!("System socket statistics: {:?}", stats),
        Err(x) => println!("System socket statistics: error: {}", x.to_string())
    }
}

Enjoy!


Condividi su Facebook

Commentami!