netdiag: parse and display MIB-II error/drop counters

IfaceStats struct gains rx_errors, tx_errors, rx_dropped, tx_dropped.
Stats output now shows error/drop line when any of these are non-zero:
    eth0    rx: ...B tx: ...B
            errors: rx=N tx=N  drops: rx=N tx=N

Mirrors Linux ifconfig output format.
Suppresses the extra line when all counters are 0 (clean state).
This commit is contained in:
Red Bear OS
2026-07-08 19:20:33 +03:00
parent 1036881f03
commit a158fe5844
+22 -5
View File
@@ -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<IfaceStats> {
@@ -38,12 +42,19 @@ fn read_iface_stats(dev: &str) -> Option<IfaceStats> {
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
);
}
}
}