diff --git a/scripts/i3blocks/bandwidth b/scripts/i3blocks/bandwidth index d9d3ab2..50215c3 100755 --- a/scripts/i3blocks/bandwidth +++ b/scripts/i3blocks/bandwidth @@ -2,6 +2,7 @@ # Copyright (C) 2012 Stefan Breunig # Copyright (C) 2014 kaueraal # Copyright (C) 2015 Thiago Perrotta +# Copyright (C) 2019 Jesús E. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,15 +17,42 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# Get custom IN and OUT labels if provided by command line arguments +while [[ $# -gt 1 ]]; do + key="$1" + case "$key" in + -i|--inlabel) + INLABEL="$2" + shift;; + -o|--outlabel) + OUTLABEL="$2" + shift;; + esac + shift +done + +[[ -z "$INLABEL" ]] && INLABEL="IN " +[[ -z "$OUTLABEL" ]] && OUTLABEL="OUT " + # Use the provided interface, otherwise the device used for the default route. -if [[ -n $BLOCK_INSTANCE ]]; then - INTERFACE=$BLOCK_INSTANCE -else - INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }') +if [[ -z $INTERFACE ]] && [[ -n $BLOCK_INSTANCE ]]; then + INTERFACE=$BLOCK_INSTANCE +elif [[ -z $INTERFACE ]]; then + # Verify tun, vpn connection + if [[ $(ip route | awk '/^default/ { print $5 ; exit }') = "tun0" ]]; then + INTERFACE=$(ip route | awk -v i=5 -v j=3 'FNR == i {print $j}') + else + INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }') + fi fi +# Exit if there is no default route +[[ -z "$INTERFACE" ]] && exit + # Issue #36 compliant. -if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "`cat /sys/class/net/${INTERFACE}/operstate`" = "up" ] +if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || \ + { ! [ "$TREAT_UNKNOWN_AS_UP" = "1" ] && + ! [ "$(cat "/sys/class/net/${INTERFACE}/operstate")" = "up" ]; } then echo "$INTERFACE down" echo "$INTERFACE down" @@ -33,11 +61,11 @@ then fi # path to store the old results in -path="/dev/shm/$(basename $0)-${INTERFACE}" +path="/dev/shm/$(basename "$0")-${INTERFACE}" # grabbing data for each adapter. -read rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes" -read tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes" +read -r rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes" +read -r tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes" # get time time=$(date +%s) @@ -45,46 +73,46 @@ time=$(date +%s) # write current data if file does not exist. Do not exit, this will cause # problems if this file is sourced instead of executed as another process. if ! [[ -f "${path}" ]]; then - echo "${time} ${rx} ${tx}" > "${path}" - chmod 0666 "${path}" + echo "${time} ${rx} ${tx}" > "${path}" + chmod 0666 "${path}" fi # read previous state and update data storage -read old < "${path}" +read -r old < "${path}" echo "${time} ${rx} ${tx}" > "${path}" # parse old data and calc time passed old=(${old//;/ }) -time_diff=$(( $time - ${old[0]} )) +time_diff=$(( time - old[0] )) # sanity check: has a positive amount of time passed [[ "${time_diff}" -gt 0 ]] || exit # calc bytes transferred, and their rate in byte/s -rx_diff=$(( $rx - ${old[1]} )) -tx_diff=$(( $tx - ${old[2]} )) -rx_rate=$(( $rx_diff / $time_diff )) -tx_rate=$(( $tx_diff / $time_diff )) +rx_diff=$(( rx - old[1] )) +tx_diff=$(( tx - old[2] )) +rx_rate=$(( rx_diff / time_diff )) +tx_rate=$(( tx_diff / time_diff )) # shift by 10 bytes to get KiB/s. If the value is larger than # 1024^2 = 1048576, then display MiB/s instead # incoming -echo -n "IN " -rx_kib=$(( $rx_rate >> 10 )) -if [[ "$rx_rate" -gt 1048576 ]]; then - printf '%sM' "`echo "scale=1; $rx_kib / 1024" | bc`" +echo -n "$INLABEL" +rx_kib=$(( rx_rate >> 10 )) +if hash bc 2>/dev/null && [[ "$rx_rate" -gt 1048576 ]]; then + printf '%sM' "$(echo "scale=1; $rx_kib / 1024" | bc)" else - echo -n "${rx_kib}K" + echo -n "${rx_kib}K" fi echo -n " " # outgoing -echo -n "OUT " -tx_kib=$(( $tx_rate >> 10 )) -if [[ "$tx_rate" -gt 1048576 ]]; then - printf '%sM' "`echo "scale=1; $tx_kib / 1024" | bc`" +echo -n "$OUTLABEL" +tx_kib=$(( tx_rate >> 10 )) +if hash bc 2>/dev/null && [[ "$tx_rate" -gt 1048576 ]]; then + printf '%sM\n' "$(echo "scale=1; $tx_kib / 1024" | bc)" else - echo -n "${tx_kib}K" + echo -n "${tx_kib}K" fi