]> git.sur5r.net Git - i3/i3status/blob - contrib/net-speed
3b7b30bead5f7a1c1e96b74d45869e7a25755fe4
[i3/i3status] / contrib / net-speed
1 #!/bin/sh
2 # Copyright (c) 2014 Zhong Jianxin <azuwis@gmail.com>
3
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:
10
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13
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
20 # THE SOFTWARE.
21
22 # Thank Stefan Breunig for the original implementation, see
23 # contrib/measure-net-speed.bash.
24
25 # i3status.conf should contain:
26 # general {
27 #   output_format = i3bar
28 # }
29
30 # i3 config looks like this:
31 # bar {
32 #   status_command exec /usr/share/doc/i3status/contrib/net-speed
33 # }
34
35 # Single interface
36 #ifaces="eth0"
37
38 # Multiple interfaces
39 #ifaces="eth0 wlan0"
40
41 # Auto detect
42 ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan)')
43
44 # Interval must be the same as in i3status.conf
45 #interval=5
46
47 # Auto detect
48 if [ -f ~/.i3status.conf ]; then
49   i3status_conf=~/.i3status.conf
50 else
51   i3status_conf="/etc/i3status.conf"
52 fi
53 interval=$(grep -o '^[[:space:]]*interval[[:space:]]*=[[:space:]]*[[:digit:]]\+' $i3status_conf | grep -o '[[:digit:]]\+')
54 if [ x"$interval" = x ]; then
55   interval=5
56 fi
57
58 last_rx=0
59 last_tx=0
60 rate=""
61
62 readable() {
63     local byte=$1
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
69             mib_dec="0${mib_dec}"
70         fi
71         echo "${mib_int}.${mib_dec}M"
72     else
73         echo "${kib}K"
74     fi
75 }
76
77 update_rate() {
78     local rx=0
79     local tx=0
80     for iface in $ifaces; do
81         local tmp_rx
82         local tmp_tx
83         local stat="/sys/class/net/${iface}/statistics"
84         read tmp_rx < "${stat}/rx_bytes"
85         read tmp_tx < "${stat}/tx_bytes"
86         rx=$(( rx + tmp_rx ))
87         tx=$(( tx + tmp_tx ))
88     done
89     rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
90     last_rx=$rx
91     last_tx=$tx
92 }
93
94 # while :; do
95 #     update_rate
96 #     echo "$rate"
97 #     sleep "$interval"
98 # done
99
100 i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
101 do
102     read line
103     update_rate
104     echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1
105 done)