1 //! For each interface, display the number of bytes sent and received, along with a data rate
2
main()3 fn main() {
4 let delay = std::time::Duration::from_secs(2);
5
6 let mut prev_stats = procfs::net::dev_status().unwrap();
7 let mut prev_now = std::time::Instant::now();
8 loop {
9 std::thread::sleep(delay);
10 let now = std::time::Instant::now();
11 let dev_stats = procfs::net::dev_status().unwrap();
12
13 // calculate diffs from previous
14 let dt = (now - prev_now).as_millis() as f32 / 1000.0;
15
16 let mut stats: Vec<_> = dev_stats.values().collect();
17 stats.sort_by_key(|s| &s.name);
18 println!();
19 println!(
20 "{:>16}: {:<20} {:<20} ",
21 "Interface", "bytes recv", "bytes sent"
22 );
23 println!(
24 "{:>16} {:<20} {:<20}",
25 "================", "====================", "===================="
26 );
27 for stat in stats {
28 println!(
29 "{:>16}: {:<20} {:>6.1} kbps {:<20} {:>6.1} kbps ",
30 stat.name,
31 stat.recv_bytes,
32 (stat.recv_bytes - prev_stats.get(&stat.name).unwrap().recv_bytes) as f32
33 / dt
34 / 1000.0,
35 stat.sent_bytes,
36 (stat.sent_bytes - prev_stats.get(&stat.name).unwrap().sent_bytes) as f32
37 / dt
38 / 1000.0
39 );
40 }
41
42 prev_stats = dev_stats;
43 prev_now = now;
44 }
45 }
46