Merge pull request #187 from IGLOU-EU/patch-2
Update network-bandwidth configuration and documentation
This commit is contained in:
commit
8a55e04a66
2 changed files with 101 additions and 37 deletions
12
INSTALL.md
12
INSTALL.md
|
@ -152,6 +152,18 @@ Customize label
|
||||||
set -g @dracula-ram-usage-label "RAM"
|
set -g @dracula-ram-usage-label "RAM"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### network-bandwidth
|
||||||
|
|
||||||
|
You can configure which network interface you want to view the bandwidth,
|
||||||
|
Displaying of the interface name, The interval between each bandwidth update.
|
||||||
|
The most common interfaces name are `eth0` for a wired connection and `wlan0` for a wireless connection.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
set -g @dracula-network-bandwidth eth0
|
||||||
|
set -g @dracula-network-bandwidth-interval 0
|
||||||
|
set -g @dracula-network-bandwidth-show-interface true
|
||||||
|
```
|
||||||
|
|
||||||
#### network-ping options
|
#### network-ping options
|
||||||
|
|
||||||
You can configure which server (hostname, IP) you want to ping and at which rate (in seconds). Default is google.com at every 5 seconds.
|
You can configure which server (hostname, IP) you want to ping and at which rate (in seconds). Default is google.com at every 5 seconds.
|
||||||
|
|
|
@ -1,51 +1,103 @@
|
||||||
#!/usr/bin/env bash
|
#!/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
|
||||||
|
|
||||||
network_name=$(tmux show-option -gqv "@dracula-network-bandwidth")
|
# 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'
|
||||||
|
)
|
||||||
|
|
||||||
|
# interface_get try to automaticaly get the used interface if network_name is empty
|
||||||
|
interface_get() {
|
||||||
|
name="$(tmux show-option -gqv "@dracula-network-bandwidth")"
|
||||||
|
|
||||||
|
if [[ -z $name ]]; then
|
||||||
|
case "$(uname -s)" in
|
||||||
|
Linux)
|
||||||
|
if type ip >/dev/null; then
|
||||||
|
name="$(ip -o route get 192.168.0.0 | awk '{print $5}')"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$name"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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 "$1" "tx")"
|
||||||
|
download="$(interface_bytes "$1" "rx")"
|
||||||
|
|
||||||
|
#wait the interval for Wait for interval to calculate the difference
|
||||||
|
sleep "$INTERVAL"
|
||||||
|
|
||||||
|
upload="$(bc <<<"$(interface_bytes "$1" "tx") - $upload")"
|
||||||
|
download="$(bc <<<"$(interface_bytes "$1" "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() {
|
main() {
|
||||||
while true
|
counter=0
|
||||||
do
|
bandwidth=()
|
||||||
output_download=""
|
|
||||||
output_upload=""
|
|
||||||
output_download_unit=""
|
|
||||||
output_upload_unit=""
|
|
||||||
|
|
||||||
initial_download=$(cat /sys/class/net/$network_name/statistics/rx_bytes)
|
network_name=""
|
||||||
initial_upload=$(cat /sys/class/net/$network_name/statistics/tx_bytes)
|
show_interface="$(tmux show-option -gqv "@dracula-network-bandwidth-show-interface")"
|
||||||
|
interval_update="$(tmux show-option -gqv "@dracula-network-bandwidth-interval")"
|
||||||
|
|
||||||
sleep $INTERVAL
|
if [[ -z $interval_update ]]; then
|
||||||
|
interval_update=0
|
||||||
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
|
fi
|
||||||
|
|
||||||
if [ $total_upload_bps -gt 1073741824 ]; then
|
while true; do
|
||||||
output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
|
if ((counter == 0)); then
|
||||||
output_upload_unit="gB/s"
|
counter=60
|
||||||
elif [ $total_upload_bps -gt 1048576 ]; then
|
network_name="$(interface_get)"
|
||||||
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
|
fi
|
||||||
|
|
||||||
echo "↓ $output_download $output_download_unit • ↑ $output_upload $output_upload_unit"
|
IFS=" " read -ra bandwidth <<<"$(get_bandwidth "$network_name")"
|
||||||
|
|
||||||
|
if [[ $show_interface == "true" ]]; then echo -n "[$network_name] "; fi
|
||||||
|
echo "↓ $(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")"
|
||||||
|
|
||||||
|
((counter = counter - 1))
|
||||||
|
sleep "$interval_update"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#run main driver
|
||||||
main
|
main
|
||||||
|
|
Loading…
Reference in a new issue