2 # Copyright (c) 2014 Zhong Jianxin <azuwis@gmail.com>
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # Thank Stefan Breunig for the original implementation, see
23 # contrib/measure-net-speed.bash.
25 # i3status.conf should contain:
27 # output_format = i3bar
30 # i3 config looks like this:
32 # status_command exec /usr/share/doc/i3status/contrib/net-speed
42 ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan)')
44 # Interval must be the same as in i3status.conf
48 if [ -f ~/.i3status.conf ]; then
49 i3status_conf=~/.i3status.conf
51 i3status_conf="/etc/i3status.conf"
53 interval=$(grep -o '^[[:space:]]*interval[[:space:]]*=[[:space:]]*[[:digit:]]\+' $i3status_conf | grep -o '[[:digit:]]\+')
54 if [ x"$interval" = x ]; then
64 local kib=$(( byte >> 10 ))
65 if [ "$kib" -gt 1024 ]; then
66 local mib_int=$(( kib >> 10 ))
67 local mib_dec=$(( kib % 1024 * 976 / 10000 ))
68 if [ "$mib_dec" -lt 10 ]; then
71 echo "${mib_int}.${mib_dec}M"
80 for iface in $ifaces; do
83 local stat="/sys/class/net/${iface}/statistics"
84 read tmp_rx < "${stat}/rx_bytes"
85 read tmp_tx < "${stat}/tx_bytes"
89 rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
100 i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
104 echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1