From ac95b3e0699548255411cb304f0885ebbca98229 Mon Sep 17 00:00:00 2001 From: Adrien Kara Date: Fri, 6 Jan 2023 00:42:01 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(network=5Fbandwid?= =?UTF-8?q?th):=20more=20flexibility=20and=20less=20spaghetti?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use the internationally recommended unit symbol - Commentary for next contributor - Remove spaghetti code - More flexible for future evolution Signed-off-by: Adrien Kara --- scripts/network_bandwidth.sh | 100 +++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index d9b5f70..4d9e782 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -1,51 +1,69 @@ #!/usr/bin/env bash -INTERVAL="1" # update interval in seconds +# INTERVAL is equal to 1s because we want to express the bandwidth in sec +readonly INTERVAL=1 + +# UPLOAD and DOWNLOAD index +readonly UPLOAD=0 +readonly DOWNLOAD=1 + +# SIZE index are the multiple of the unit byte and value the internationally recommended unit symbol in sec +readonly SIZE=( + [1]='B/s' + [1024]='kB/s' + [1048576]='MB/s' + [1073741824]='GB/s' +) network_name=$(tmux show-option -gqv "@dracula-network-bandwidth") +# interface_bytes give interface name and signal tx/rx return Bytes +interface_bytes() { + cat "/sys/class/net/$1/statistics/$2_bytes" +} + +# get_bandwidth return the number of bytes exchanged for tx and rx +get_bandwidth() { + upload="$(interface_bytes "$network_name" "tx")" + download="$(interface_bytes "$network_name" "rx")" + + #wait the interval for Wait for interval to calculate the difference + sleep "$INTERVAL" + + upload="$(bc <<<"$(interface_bytes "$network_name" "tx") - $upload")" + download="$(bc <<<"$(interface_bytes "$network_name" "rx") - $download")" + + #set to 0 by default useful for non-existent interface + echo "${upload:-0} ${download:-0}" +} + +# bandwidth_to_unit convert bytes into its highest unit and add unit symbol in sec +bandwidth_to_unit() { + local size=1 + for i in "${!SIZE[@]}"; do + if (($1 < i)); then + break + fi + + size="$i" + done + + local result="0.00" + if (($1 != 0)); then + result="$(bc <<<"scale=2; $1 / $size")" + fi + + echo "$result ${SIZE[$size]}" +} + main() { - while true - do - output_download="" - output_upload="" - output_download_unit="" - output_upload_unit="" + bandwidth=() - initial_download=$(cat /sys/class/net/$network_name/statistics/rx_bytes) - initial_upload=$(cat /sys/class/net/$network_name/statistics/tx_bytes) - - sleep $INTERVAL - - final_download=$(cat /sys/class/net/$network_name/statistics/rx_bytes) - final_upload=$(cat /sys/class/net/$network_name/statistics/tx_bytes) - - total_download_bps=$(expr $final_download - $initial_download) - total_upload_bps=$(expr $final_upload - $initial_upload) - - if [ $total_download_bps -gt 1073741824 ]; then - output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') - output_download_unit="gB/s" - elif [ $total_download_bps -gt 1048576 ]; then - 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 - - if [ $total_upload_bps -gt 1073741824 ]; then - output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') - output_upload_unit="gB/s" - elif [ $total_upload_bps -gt 1048576 ]; then - 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" + while true; do + IFS=" " read -ra bandwidth <<<"$(get_bandwidth)" + echo "↓ $(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")" done } + +#run main driver main