testsAndMisc/linux_configuration/i3-configuration/i3blocks/network_monitor.sh

90 lines
2.5 KiB
Bash
Raw Normal View History

2024-11-15 11:49:36 +01:00
#!/bin/bash
# Function to detect all active network interfaces
detect_interfaces() {
2025-11-01 15:36:22 +01:00
local iface_path iface state
for iface_path in /sys/class/net/*; do
iface=$(basename "$iface_path")
if [[ $iface == "lo" || ! -d "/sys/class/net/$iface" ]]; then
continue
fi
if [[ -r "/sys/class/net/$iface/operstate" ]]; then
state=$(< "/sys/class/net/$iface/operstate")
if [[ $state == "up" ]]; then
printf '%s\n' "$iface"
fi
fi
done
}
# Detect all active network interfaces
2025-11-01 15:36:22 +01:00
mapfile -t interfaces < <(detect_interfaces)
# If no active interfaces are found, exit
2025-11-01 15:36:22 +01:00
if [ "${#interfaces[@]}" -eq 0 ]; then
echo "No active network interfaces found"
exit 1
fi
2024-11-15 11:49:36 +01:00
# Initialize total RX and TX bytes
total_rx_now=0
total_tx_now=0
2024-11-15 11:49:36 +01:00
# Initialize last recorded RX and TX bytes
total_last_rx=0
total_last_tx=0
2024-11-15 11:49:36 +01:00
# Initialize time variables without forking: read from /proc/uptime
read -r uptime_s _ < /proc/uptime
current_time=${uptime_s%%.*}
last_time=$current_time
# Iterate over each interface and accumulate RX and TX bytes
2025-11-01 15:36:22 +01:00
for interface in "${interfaces[@]}"; do
rx_path="/sys/class/net/$interface/statistics/rx_bytes"
tx_path="/sys/class/net/$interface/statistics/tx_bytes"
2025-11-01 15:36:22 +01:00
if ! read -r rx_now < "$rx_path"; then
rx_now=0
fi
if ! read -r tx_now < "$tx_path"; then
tx_now=0
fi
2025-11-01 15:36:22 +01:00
state_file="/tmp/network_monitor_$interface"
if [ -f "$state_file" ]; then
read -r last_rx last_tx last_time < "$state_file"
else
last_rx=$rx_now
last_tx=$tx_now
fi
2025-11-01 15:36:22 +01:00
total_rx_now=$((total_rx_now + rx_now))
total_tx_now=$((total_tx_now + tx_now))
total_last_rx=$((total_last_rx + last_rx))
total_last_tx=$((total_last_tx + last_tx))
# Save current RX and TX bytes for the next check (using uptime as source)
printf '%s %s %s\n' "$rx_now" "$tx_now" "$current_time" > "$state_file"
done
2024-11-15 11:49:36 +01:00
# Calculate time difference
time_diff=$((current_time - last_time))
# Calculate total download and upload speeds in bytes per second
2025-11-01 15:36:22 +01:00
if ((time_diff > 0)); then
total_rx_rate=$(((total_rx_now - total_last_rx) / time_diff))
total_tx_rate=$(((total_tx_now - total_last_tx) / time_diff))
2024-11-15 11:49:36 +01:00
else
2025-11-01 15:36:22 +01:00
total_rx_rate=0
total_tx_rate=0
2024-11-15 11:49:36 +01:00
fi
# Convert speeds to human-readable format
2025-11-01 15:36:22 +01:00
rx_rate_human=$(numfmt --to=iec --suffix=B/s --padding=8 "$total_rx_rate")
tx_rate_human=$(numfmt --to=iec --suffix=B/s --padding=8 "$total_tx_rate")
2024-11-15 11:49:36 +01:00
# Store the result of printf into a string and echo it
printf " %s  %s\n" "$rx_rate_human" "$tx_rate_human"
2025-11-01 15:36:22 +01:00
echo "#50FA7B"