]> git.sur5r.net Git - i3/i3status/blob - contrib/net-speed
Basic XDG support and minor tweaks to the net-speed script
[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 if [ -z "$XDG_CONFIG_HOME" ]; then
48   XDG_CONFIG_HOME="${HOME}/.config"
49 fi
50
51 # Auto detect
52 if [ -f "${XDG_CONFIG_HOME}/i3status/config" ]; then
53   i3status_conf="${XDG_CONFIG_HOME}/i3status/config"
54 elif [ -f ~/.i3status.conf ]; then
55   i3status_conf=~/.i3status.conf
56 else
57   i3status_conf="/etc/i3status.conf"
58 fi
59
60 if [ -f "$i3status_conf" ]; then
61   interval=$(grep -o '^[[:space:]]*interval[[:space:]]*=[[:space:]]*[[:digit:]]\+' $i3status_conf | grep -o '[[:digit:]]\+')
62 fi
63
64 if [ x"$interval" = x ]; then
65   interval=5
66 fi
67
68 last_rx=0
69 last_tx=0
70 rate=""
71
72 readable() {
73     local byte=$1
74     local kib=$(( byte >> 10 ))
75     if [ "$kib" -gt 1024 ]; then
76         local mib_int=$(( kib >> 10 ))
77         local mib_dec=$(( kib % 1024 * 976 / 10000 ))
78         if [ "$mib_dec" -lt 10 ]; then
79             mib_dec="0${mib_dec}"
80         fi
81         echo "${mib_int}.${mib_dec}M"
82     else
83         echo "${kib}K"
84     fi
85 }
86
87 update_rate() {
88     local rx=0
89     local tx=0
90     for iface in $ifaces; do
91         local tmp_rx
92         local tmp_tx
93         local stat="/sys/class/net/${iface}/statistics"
94         read tmp_rx < "${stat}/rx_bytes"
95         read tmp_tx < "${stat}/tx_bytes"
96         rx=$(( rx + tmp_rx ))
97         tx=$(( tx + tmp_tx ))
98     done
99     rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
100     last_rx=$rx
101     last_tx=$tx
102 }
103
104 # while :; do
105 #     update_rate
106 #     echo "$rate"
107 #     sleep "$interval"
108 # done
109
110 i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
111 do
112     read line
113     update_rate
114     echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1
115 done)