tmux2k/scripts/network_bandwidth.sh

52 lines
1.8 KiB
Bash
Raw Normal View History

2022-08-09 06:28:35 +05:30
#!/usr/bin/env bash
INTERVAL="1" # update interval in seconds
network_name=$(tmux show-option -gqv "@tmux2k-network-bandwidth")
main() {
while true
do
output_download=""
output_upload=""
output_download_unit=""
output_upload_unit=""
2022-08-13 17:11:48 +05:30
initial_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
initial_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
2022-08-09 06:28:35 +05:30
2022-08-13 17:11:48 +05:30
sleep "$INTERVAL"
2022-08-09 06:28:35 +05:30
2022-08-13 17:11:48 +05:30
final_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
final_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
2022-08-09 06:28:35 +05:30
2022-08-13 17:11:48 +05:30
total_download_bps=$(expr "$final_download" - "$initial_download")
total_upload_bps=$(expr "$final_upload" - "$initial_upload")
2022-08-09 06:28:35 +05:30
2022-08-13 17:11:48 +05:30
if [ "$total_download_bps" -gt 1073741824 ]; then
2022-08-09 06:28:35 +05:30
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_download_unit="gB/s"
2022-08-13 17:11:48 +05:30
elif [ "$total_download_bps" -gt 1048576 ]; then
2022-08-09 06:28:35 +05:30
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_download_unit="mB/s"
else
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
output_download_unit="kB/s"
fi
2022-08-13 17:11:48 +05:30
if [ "$total_upload_bps" -gt 1073741824 ]; then
2022-08-09 06:28:35 +05:30
output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_upload_unit="gB/s"
2022-08-13 17:11:48 +05:30
elif [ "$total_upload_bps" -gt 1048576 ]; then
2022-08-09 06:28:35 +05:30
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_upload_unit="mB/s"
else
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
output_upload_unit="kB/s"
fi
echo "$output_download $output_download_unit • ↑ $output_upload $output_upload_unit"
done
}
main