Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
599a048015 | ||
|
|
f6543eb228 |
+13
-8
@@ -81,7 +81,8 @@ fn query_cpu_temp() -> String {
|
||||
}
|
||||
}
|
||||
|
||||
// Sums rx/tx byte counters across all interfaces except loopback.
|
||||
// Sums rx/tx byte counters across physical interfaces only. Virtual ones
|
||||
// (loopback, bridges, veth, VPN tunnels) would count the same traffic twice.
|
||||
fn read_net_bytes() -> (u64, u64) {
|
||||
let mut down = 0u64;
|
||||
let mut up = 0u64;
|
||||
@@ -96,7 +97,8 @@ fn read_net_bytes() -> (u64, u64) {
|
||||
let Some((iface, rest)) = line.split_once(':') else {
|
||||
continue;
|
||||
};
|
||||
if iface.trim() == "lo" {
|
||||
// Only physical interfaces have a backing device in sysfs.
|
||||
if !Path::new("/sys/class/net").join(iface.trim()).join("device").exists() {
|
||||
continue;
|
||||
}
|
||||
let fields: Vec<&str> = rest.split_whitespace().collect();
|
||||
@@ -110,10 +112,13 @@ fn read_net_bytes() -> (u64, u64) {
|
||||
(down, up)
|
||||
}
|
||||
|
||||
// Fixed-width, fixed-unit (KiB/s) formatting so the bar doesn't change size
|
||||
// as traffic fluctuates.
|
||||
fn format_net(down_rate: u64, up_rate: u64) -> String {
|
||||
format!("net:↓{down_rate:>5} ↑{up_rate:>5} KiB/s")
|
||||
// Fixed-width, fixed-unit (Mbit/s, decimal like ISP rates) formatting so the
|
||||
// bar doesn't change size as traffic fluctuates.
|
||||
fn format_net(down_bytes_per_sec: u64, up_bytes_per_sec: u64) -> String {
|
||||
let to_mbps = |b: u64| b as f64 * 8.0 / 1_000_000.0;
|
||||
let down = to_mbps(down_bytes_per_sec);
|
||||
let up = to_mbps(up_bytes_per_sec);
|
||||
format!("net:↓{down:>5.1} ↑{up:>5.1} Mbit/s")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -157,8 +162,8 @@ fn main() {
|
||||
loop {
|
||||
std::thread::sleep(INTERVAL);
|
||||
let (down, up) = read_net_bytes();
|
||||
let down_rate = down.saturating_sub(prev_down) / INTERVAL.as_secs() / 1024;
|
||||
let up_rate = up.saturating_sub(prev_up) / INTERVAL.as_secs() / 1024;
|
||||
let down_rate = down.saturating_sub(prev_down) / INTERVAL.as_secs();
|
||||
let up_rate = up.saturating_sub(prev_up) / INTERVAL.as_secs();
|
||||
tx.send(Update::Net(format_net(down_rate, up_rate))).unwrap();
|
||||
prev_down = down;
|
||||
prev_up = up;
|
||||
|
||||
Reference in New Issue
Block a user