aboutsummaryrefslogtreecommitdiff
blob: ebd398a16414be0e0c66497076ecb80bc6c179f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
#
# Copyright (C) 2015 James Murphy
# Licensed under the terms of the GNU GPL v2 only.
#
# i3blocks blocklet script to monitor bandwidth usage

iface="${BLOCK_INSTANCE}"
iface="${IFACE:-$iface}"
dt="${DT:-3}"
unit="${UNIT:-KB}"
LABEL="${LABEL:-: }" # down arrow up arrow
#LABEL="${LABEL:-<span font='FontAwesome'> </span>}" # down arrow up arrow
printf_command="${PRINTF_COMMAND:-"printf \"${LABEL}%1.0f/%1.0f %s/s\\n\", rx, wx, unit;"}"

function default_interface {
    ip route | awk '/^default via/ {print $5; exit}'
}

function check_proc_net_dev {
    if [ ! -f "/proc/net/dev" ]; then
        echo "/proc/net/dev not found"
        exit 1
    fi
}

function list_interfaces {
    check_proc_net_dev
    echo "Interfaces in /proc/net/dev:"
    grep -o "^[^:]\\+:" /proc/net/dev | tr -d " :"
}

while getopts i:t:u:p:lh opt; do
    case "$opt" in
        i) iface="$OPTARG" ;;
        t) dt="$OPTARG" ;;
        u) unit="$OPTARG" ;;
        p) printf_command="$OPTARG" ;;
        l) list_interfaces && exit 0 ;;
        h) printf \
"Usage: bandwidth3 [-i interface] [-t time] [-u unit] [-p printf_command] [-l] [-h]
Options:
-i\tNetwork interface to measure. Default determined using \`ip route\`.
-t\tTime interval in seconds between measurements. Default: 3
-u\tUnits to measure bytes in. Default: Mb
\tAllowed units: Kb, KB, Mb, MB, Gb, GB, Tb, TB
\tUnits may have optional it/its/yte/ytes on the end, e.g. Mbits, KByte
-p\tAwk command to be called after a measurement is made. 
\tDefault: printf \"<span font='FontAwesome'>  </span>%%-5.1f/%%5.1f %%s/s\\\\n\", rx, wx, unit;
\tExposed variables: rx, wx, tx, unit, iface
-l\tList available interfaces in /proc/net/dev
-h\tShow this help text
" && exit 0;;
    esac
done

check_proc_net_dev

iface="${iface:-$(default_interface)}"
while [ -z "$iface" ]; do
    echo No default interface
    sleep "$dt"
    iface=$(default_interface)
done

case "$unit" in
    Kb|Kbit|Kbits)   bytes_per_unit=$((1024 / 8));;
    KB|KByte|KBytes) bytes_per_unit=$((1024));;
    Mb|Mbit|Mbits)   bytes_per_unit=$((1024 * 1024 / 8));;
    MB|MByte|MBytes) bytes_per_unit=$((1024 * 1024));;
    Gb|Gbit|Gbits)   bytes_per_unit=$((1024 * 1024 * 1024 / 8));;
    GB|GByte|GBytes) bytes_per_unit=$((1024 * 1024 * 1024));;
    Tb|Tbit|Tbits)   bytes_per_unit=$((1024 * 1024 * 1024 * 1024 / 8));;
    TB|TByte|TBytes) bytes_per_unit=$((1024 * 1024 * 1024 * 1024));;
    *) echo Bad unit "$unit" && exit 1;;
esac

scalar=$((bytes_per_unit * dt))
init_line=$(cat /proc/net/dev | grep "^[ ]*$iface:")
if [ -z "$init_line" ]; then
    echo Interface not found in /proc/net/dev: "$iface"
    exit 1
fi

init_received=$(awk '{print $2}' <<< $init_line)
init_sent=$(awk '{print $10}' <<< $init_line)

(while true; do cat /proc/net/dev; sleep "$dt"; done) |\
    stdbuf -oL grep "^[ ]*$iface:" |\
    awk -v scalar="$scalar" -v unit="$unit" -v iface="$iface" '
BEGIN{old_received='"$init_received"';old_sent='"$init_sent"'}
{
    received=$2
    sent=$10
    rx=(received-old_received)/scalar;
    wx=(sent-old_sent)/scalar;
    tx=rx+wr;
    old_received=received;
    old_sent=sent;
    if(rx >= 0 && wx >= 0){
        '"$printf_command"';
        fflush(stdout);
    }
}
'
Powered by cgit v1.2.3 (git 2.41.0)