Created
July 12, 2018 01:53
-
-
Save rust-play/4aca60d78dd13d5047ec0f921c1b7cbd to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate systemstat; | |
use std::thread; | |
use std::time::Duration; | |
use systemstat::{System, Platform}; | |
fn main() { | |
let sys = System::new(); | |
match sys.mounts() { | |
Ok(mounts) => { | |
println!("\nMounts:"); | |
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!("\nMounts: error: {}", x) | |
} | |
match sys.block_device_statistics() { | |
Ok(stats) => { | |
for blkstats in stats.values() { | |
println!("{}: {:?}", blkstats.name, blkstats); | |
} | |
} | |
Err(x) => println!("\nBlock statistics error: {}", x.to_string()) | |
} | |
match sys.networks() { | |
Ok(netifs) => { | |
println!("\nNetworks:"); | |
for netif in netifs.values() { | |
println!("{} ({:?})", netif.name, netif.addrs); | |
} | |
} | |
Err(x) => println!("\nNetworks: error: {}", x) | |
} | |
match sys.networks() { | |
Ok(netifs) => { | |
println!("\nNetwork interface statistics:"); | |
for netif in netifs.values() { | |
println!("{} statistics: ({:?})", netif.name, sys.network_stats(&netif.name)); | |
} | |
} | |
Err(x) => println!("\nNetworks: error: {}", x) | |
} | |
match sys.battery_life() { | |
Ok(battery) => | |
print!("\nBattery: {}%, {}h{}m remaining", | |
battery.remaining_capacity*100.0, | |
battery.remaining_time.as_secs() / 3600, | |
battery.remaining_time.as_secs() % 60), | |
Err(x) => print!("\nBattery: 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!("\nMemory: {} used / {} ({} bytes) total ({:?})", mem.total - mem.free, mem.total, mem.total.as_usize(), mem.platform_memory), | |
Err(x) => println!("\nMemory: error: {}", x) | |
} | |
match sys.load_average() { | |
Ok(loadavg) => println!("\nLoad average: {} {} {}", loadavg.one, loadavg.five, loadavg.fifteen), | |
Err(x) => println!("\nLoad average: error: {}", x) | |
} | |
match sys.uptime() { | |
Ok(uptime) => println!("\nUptime: {:?}", uptime), | |
Err(x) => println!("\nUptime: error: {}", x) | |
} | |
match sys.boot_time() { | |
Ok(boot_time) => println!("\nBoot time: {}", boot_time), | |
Err(x) => println!("\nBoot time: error: {}", x) | |
} | |
match sys.cpu_load_aggregate() { | |
Ok(cpu)=> { | |
println!("\nMeasuring 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!("\nCPU load: error: {}", x) | |
} | |
match sys.cpu_temp() { | |
Ok(cpu_temp) => println!("\nCPU temp: {}", cpu_temp), | |
Err(x) => println!("\nCPU temp: {}", x) | |
} | |
match sys.socket_stats() { | |
Ok(stats) => println!("\nSystem socket statistics: {:?}", stats), | |
Err(x) => println!("\nError: {}", x.to_string()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment