netr 0.3.0

Display network interface throughput by second and by minute along with a graph. This is quick and easy to use via a mobile handset or similar device where typing is cumbersome.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use getopts::Options;
#[cfg(not(target_os = "linux"))]
use libc::*;
use std::collections::BTreeMap;
#[cfg(not(target_os = "linux"))]
use std::ffi;
use std::thread;

use chrono::{DateTime, Local, Timelike};

use netr::*;

fn print_line_stats(
    i: &str,
    in_total: i64,
    out_total: i64,
    isec: i64,
    osec: i64,
    imin: i64,
    omin: i64,
) {
    println!(
        "{:17} {:8} {:8} | {:8} | {:8} | {:8} | {:8}",
        i,
        human_unit(in_total),
        human_unit(out_total),
        human_unit(isec),
        human_unit(osec),
        human_unit(imin),
        human_unit(omin),
    );
}

fn print_counters(config: &Config, _total: &str, h: &BTreeMap<String, DeviceHist>) {
    for hist in h {
        if hist.0 == "total" && config.print_total.is_none() {
            continue;
        }
        let hist = hist.1.history.last();
        if hist.is_none() {
            continue;
        }
        let hist = hist.unwrap();

        // If format string is provided, use it
        if let Some(format_str) = &config.format {
            print!("{}", format_output(format_str, hist));
            continue;
        }

        match config.counters {
            Some(StatType::Bytes) => {
                println!("{}\n{}", hist.ibytes, hist.obytes);
            }
            Some(StatType::Packets) => {
                println!("{}\n{}", hist.ipackets, hist.opackets);
            }
            Some(StatType::All) => {
                println!("{:?}", hist);
            }
            None => {
                return;
            }
        }
    }
}

fn print_stats(total: &str, h: &BTreeMap<String, DeviceHist>) {
    let cls = "\x1Bc";
    println!(
        "{}{:17} {:8} {:8} | {:8} | {:8} | {:8} | {:8}  ",
        cls, "interface", "in", "out", "in/sec", "out/sec", "in/min", "out/min"
    );

    let mut list: Vec<String> = vec![];
    for k in h.keys() {
        if k == total {
            continue;
        }
        list.push(k.to_string());
    }
    list.push(total.to_string());

    for k in &list {
        let dl = h.get(k).unwrap();
        let history_len = dl.history.len();

        let in_total = dl.history[history_len - 1].ibytes;
        let out_total = dl.history[history_len - 1].obytes;

        let isec = if history_len > 1 {
            dl.history[history_len - 1].ibytes - dl.history[history_len - 2].ibytes
        } else {
            0
        };

        let osec = if history_len > 1 {
            dl.history[history_len - 1].obytes - dl.history[history_len - 2].obytes
        } else {
            0
        };

        let imin = if history_len > 1 {
            dl.history[history_len - 1].ibytes - dl.history[0].ibytes
        } else {
            0_i64
        };

        let omin = if history_len > 1 {
            dl.history[history_len - 1].obytes - dl.history[0].obytes
        } else {
            0_i64
        };

        print_line_stats(k, in_total, out_total, isec, osec, imin, omin);
    }
    println!();
}

fn print_graph(
    total: &str,
    h: &BTreeMap<String, DeviceHist>,
    height: i32,
    indent: usize,
    print_max: bool,
) {
    if !h.contains_key(total) {
        return;
    }

    let mut max_delta: i64 = 0;
    let history_list = &h.get(total).unwrap().history;

    if history_list.len() < 2 {
        return;
    }

    // get the limit for the graph
    for hist in 1..history_list.len() {
        let cur_total = history_list[hist].ibytes + history_list[hist].obytes;
        let last_total = history_list[hist - 1].ibytes + history_list[hist - 1].obytes;
        if cur_total - last_total > max_delta {
            max_delta = cur_total - last_total;
        }
    }

    // iterate lines as we draw down
    for line in 1..height {
        print!("{:indent$}", "", indent = indent);
        for hist in 1..history_list.len() {
            let last_total = history_list[hist - 1].ibytes + history_list[hist - 1].obytes;
            let last_in_diff = history_list[hist].ibytes - history_list[hist - 1].ibytes;
            let total = history_list[hist].ibytes + history_list[hist].obytes;
            let diff = total - last_total;

            print!(
                "{}",
                if diff != 0 && diff >= (max_delta / height as i64) * (height as i64 - line as i64)
                {
                    if last_in_diff >= (max_delta / height as i64) * (height as i64 - line as i64) {
                        "I"
                    } else {
                        "O"
                    }
                } else {
                    " "
                }
            );
        }

        if line == 1 && print_max {
            print!(
                "{:padding$} {:>9}",
                "",
                human_unit(max_delta),
                padding = 61 - history_list.len()
            );
        }

        println!();
    }
}

fn print_minute_marker(
    total: &str,
    h: &BTreeMap<String, DeviceHist>,
    time: chrono::DateTime<Local>,
    indent: usize,
) {
    let padding = h.get(total).unwrap().history.len() as u32;
    if padding > time.second() {
        let padding = padding - time.second() - 2;
        println!(
            "{:indent$}{:padding$}^ {:02}:{:02}",
            "",
            "",
            time.hour(),
            time.minute(),
            padding = padding as usize
        );
    }
}

fn run_loops(config: &Config) {
    let total = "total";
    let indent = 10;
    let mut ifs: BTreeMap<String, DeviceHist> = BTreeMap::new();

    loop {
        let now = std::time::Instant::now();
        let mut dh: BTreeMap<String, bool> = BTreeMap::new();
        let devs = read_net();

        if !ifs.contains_key(total) {
            ifs.insert(
                total.to_string(),
                DeviceHist {
                    history: Vec::new(),
                },
            );
        }

        let mut ibytes: i64 = 0;
        let mut obytes: i64 = 0;
        let mut ipackets: i64 = 0;
        let mut opackets: i64 = 0;

        for dev in devs.iter() {
            if config.filter.is_some() {
                let c = config.clone().filter.unwrap();
                if !c.matches(&dev.name) {
                    continue;
                }
            }

            if !ifs.contains_key(&dev.name) {
                ifs.insert(
                    dev.name.to_string(),
                    DeviceHist {
                        history: Vec::new(),
                    },
                );
            }

            let h = ifs.get_mut(&dev.name).unwrap();
            let hh = &mut h.history;

            hh.push(Device {
                name: dev.name.to_string(),
                ibytes: dev.ibytes,
                obytes: dev.obytes,
                ipackets: dev.ipackets,
                opackets: dev.opackets,
            });
            dh.insert(dev.name.to_string(), true);
            while hh.len() > 61 {
                hh.remove(0);
            }

            ibytes += dev.ibytes;
            obytes += dev.obytes;
            ipackets += dev.ipackets;
            opackets += dev.opackets;
        }

        let total_if = ifs.get_mut(total).unwrap();
        let total_history = &mut total_if.history;

        total_history.push(Device {
            name: total.to_string(),
            ibytes,
            obytes,
            ipackets,
            opackets,
        });
        while total_history.len() > 61 {
            total_history.remove(0);
        }
        dh.insert(total.to_string(), true);

        let mut remove_v: Vec<String> = Vec::new();
        for ifp in ifs.keys() {
            if !dh.contains_key(ifp) {
                remove_v.push(ifp.to_string());
            }
        }
        for v in remove_v {
            ifs.remove(&v);
        }

        if config.counters.is_some() {
            print_counters(config, total, &ifs);
            break;
        }

        print_stats(total, &ifs);
        print_graph(total, &ifs, 10, indent, true);

        let time: DateTime<Local> = Local::now();
        print_minute_marker(total, &ifs, time, indent);

        loop {
            thread::sleep(std::time::Duration::from_millis(100));
            if now.elapsed() >= std::time::Duration::from_millis(1000) {
                break;
            }
        }
    }
}
fn banner() -> String {
    format!(
        "{} version {}",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION")
    )
}

fn print_version() {
    println!("{}", &banner());
}

fn help(opts: &getopts::Options) {
    println!("{}", opts.usage(&banner()));
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let mut config = Config::new();
    let mut opts = Options::new();
    opts.parsing_style(getopts::ParsingStyle::FloatingFrees);
    opts.optopt("e", "exclude", "exclude this pattern", "REGEX");
    opts.optopt("i", "include", "include this pattern", "REGEX");
    opts.optopt(
        "f",
        "format",
        "format output using %{if}, %{bytes}, %{packets}",
        "FORMAT",
    );
    opts.optflag("c", "counters", "print counters and exit");
    opts.optflag(
        "",
        "printtotal",
        "enable print total in counter mode (default off)",
    );
    opts.optflag(
        "",
        "packets",
        "print packets instead of bytes in counter mode",
    );
    opts.optflag("h", "help", "display help");
    opts.optflag("v", "version", "display version");

    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(f) => {
            println!("{}", f);
            std::process::exit(1);
        }
    };

    if matches.opt_present("version") {
        print_version();
        std::process::exit(0);
    }

    if matches.opt_present("help") {
        help(&opts);
        std::process::exit(0);
    }

    if matches.opt_present("exclude") {
        let pattern = matches.opt_str("exclude").unwrap();
        let filter = match FilterOpts::create(&pattern, !matches.opt_present("exclude")) {
            Some(x) => x,
            None => {
                eprintln!("Cannot create regex from {}", pattern);
                help(&opts);
                std::process::exit(1);
            }
        };

        config.filter = Some(filter);
    }

    if matches.opt_present("include") {
        if matches.opt_present("exclude") {
            eprintln!("Cannot --exclude and --include at the same time");
            help(&opts);
            std::process::exit(1);
        }

        let pattern = matches.opt_str("include").unwrap();
        let filter = match FilterOpts::create(&pattern, matches.opt_present("include")) {
            Some(x) => x,
            None => {
                eprintln!("Cannot create regex from {}", matches.free[0].clone());
                help(&opts);
                std::process::exit(1);
            }
        };

        config.filter = Some(filter);
    }

    if matches.opt_present("printtotal") {
        config.print_total = Some(true);
    }

    if matches.opt_present("counters") {
        config.counters = Some(StatType::Bytes);
    }

    if matches.opt_present("packets") {
        config.counters = Some(StatType::Packets);
    }

    if matches.opt_present("format") {
        let format_str = matches.opt_str("format").unwrap();
        config.format = Some(format_str);
        // Enable counters mode when format is specified
        if config.counters.is_none() {
            config.counters = Some(StatType::Bytes);
        }
    }

    run_loops(&config);
}