diff --git a/netdiag/src/main.rs b/netdiag/src/main.rs index 573dbad833..6300bbac2f 100644 --- a/netdiag/src/main.rs +++ b/netdiag/src/main.rs @@ -31,6 +31,10 @@ struct IfaceStats { rx_packets: u64, tx_bytes: u64, tx_packets: u64, + rx_errors: u64, + tx_errors: u64, + rx_dropped: u64, + tx_dropped: u64, } fn read_iface_stats(dev: &str) -> Option { @@ -38,12 +42,19 @@ fn read_iface_stats(dev: &str) -> Option { if raw.is_empty() { return None; } - let mut s = IfaceStats { rx_bytes: 0, rx_packets: 0, tx_bytes: 0, tx_packets: 0 }; + let mut s = IfaceStats { + rx_bytes: 0, rx_packets: 0, tx_bytes: 0, tx_packets: 0, + rx_errors: 0, tx_errors: 0, rx_dropped: 0, tx_dropped: 0, + }; for line in raw.lines() { - if line.starts_with("rx_bytes=") { s.rx_bytes = parse_counter(line); } - else if line.starts_with("rx_packets=") { s.rx_packets = parse_counter(line); } - else if line.starts_with("tx_bytes=") { s.tx_bytes = parse_counter(line); } - else if line.starts_with("tx_packets=") { s.tx_packets = parse_counter(line); } + if let Some(v) = line.strip_prefix("rx_bytes=") { s.rx_bytes = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("rx_packets=") { s.rx_packets = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("tx_bytes=") { s.tx_bytes = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("tx_packets=") { s.tx_packets = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("rx_errors=") { s.rx_errors = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("tx_errors=") { s.tx_errors = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("rx_dropped=") { s.rx_dropped = v.trim().parse().unwrap_or(0); } + else if let Some(v) = line.strip_prefix("tx_dropped=") { s.tx_dropped = v.trim().parse().unwrap_or(0); } } Some(s) } @@ -164,6 +175,12 @@ fn show_stats() { " {:<6} rx: {:>10} B ({:>6} pkts) tx: {:>10} B ({:>6} pkts)", dev, s.rx_bytes, s.rx_packets, s.tx_bytes, s.tx_packets ); + if s.rx_errors != 0 || s.tx_errors != 0 || s.rx_dropped != 0 || s.tx_dropped != 0 { + println!( + " {:>7} errors: rx={} tx={} drops: rx={} tx={}", + "", s.rx_errors, s.tx_errors, s.rx_dropped, s.tx_dropped + ); + } } }