2021-04-30 15:59:22 +02:00
|
|
|
#!/usr/bin/env bash
|
2021-04-10 05:35:06 +02:00
|
|
|
|
2023-01-06 00:42:01 +01:00
|
|
|
# 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'
|
|
|
|
)
|
2021-04-10 05:35:06 +02:00
|
|
|
|
2023-01-06 19:46:04 +01:00
|
|
|
# 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"
|
|
|
|
}
|
2021-04-10 05:35:06 +02:00
|
|
|
|
2023-01-06 00:42:01 +01:00
|
|
|
# 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() {
|
2023-01-06 19:46:04 +01:00
|
|
|
upload="$(interface_bytes "$1" "tx")"
|
|
|
|
download="$(interface_bytes "$1" "rx")"
|
2023-01-06 00:42:01 +01:00
|
|
|
|
|
|
|
#wait the interval for Wait for interval to calculate the difference
|
|
|
|
sleep "$INTERVAL"
|
|
|
|
|
2023-01-06 19:46:04 +01:00
|
|
|
upload="$(bc <<<"$(interface_bytes "$1" "tx") - $upload")"
|
|
|
|
download="$(bc <<<"$(interface_bytes "$1" "rx") - $download")"
|
2023-01-06 00:42:01 +01:00
|
|
|
|
|
|
|
#set to 0 by default useful for non-existent interface
|
|
|
|
echo "${upload:-0} ${download:-0}"
|
|
|
|
}
|
2021-10-10 05:44:40 +02:00
|
|
|
|
2023-01-06 00:42:01 +01:00
|
|
|
# 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
|
2021-10-10 05:44:40 +02:00
|
|
|
fi
|
|
|
|
|
2023-01-06 00:42:01 +01:00
|
|
|
size="$i"
|
2021-07-06 10:22:36 +02:00
|
|
|
done
|
2023-01-06 00:42:01 +01:00
|
|
|
|
|
|
|
local result="0.00"
|
|
|
|
if (($1 != 0)); then
|
|
|
|
result="$(bc <<<"scale=2; $1 / $size")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$result ${SIZE[$size]}"
|
2021-04-10 05:35:06 +02:00
|
|
|
}
|
2023-01-06 00:42:01 +01:00
|
|
|
|
|
|
|
main() {
|
2023-01-06 19:46:04 +01:00
|
|
|
counter=0
|
2023-01-06 00:42:01 +01:00
|
|
|
bandwidth=()
|
|
|
|
|
2023-01-06 19:46:04 +01:00
|
|
|
network_name=""
|
|
|
|
show_interface="$(tmux show-option -gqv "@dracula-network-bandwidth-show-interface")"
|
|
|
|
interval_update="$(tmux show-option -gqv "@dracula-network-bandwidth-interval")"
|
|
|
|
|
|
|
|
if [[ -z $interval_update ]]; then
|
|
|
|
interval_update=0
|
|
|
|
fi
|
|
|
|
|
2023-10-01 10:35:42 +02:00
|
|
|
if ! command -v bc &> /dev/null
|
|
|
|
then
|
|
|
|
echo "command bc could not be found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-01-06 00:42:01 +01:00
|
|
|
while true; do
|
2023-01-06 19:46:04 +01:00
|
|
|
if ((counter == 0)); then
|
|
|
|
counter=60
|
|
|
|
network_name="$(interface_get)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
IFS=" " read -ra bandwidth <<<"$(get_bandwidth "$network_name")"
|
|
|
|
|
|
|
|
if [[ $show_interface == "true" ]]; then echo -n "[$network_name] "; fi
|
2023-01-06 00:42:01 +01:00
|
|
|
echo "↓ $(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")"
|
2023-01-06 19:46:04 +01:00
|
|
|
|
|
|
|
((counter = counter - 1))
|
|
|
|
sleep "$interval_update"
|
2023-01-06 00:42:01 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#run main driver
|
2021-04-10 05:35:06 +02:00
|
|
|
main
|