summaryrefslogtreecommitdiff
path: root/contrib/net-speed.sh
diff options
context:
space:
mode:
authorMoritz Warning <moritzwarning@web.de>2017-01-02 16:06:06 +0100
committerMichael Stapelberg <stapelberg@users.noreply.github.com>2017-01-02 16:06:06 +0100
commit3f6d71feb9b620f28396e7c980323f92081dc2fb (patch)
tree0ef43e8ec7ca9497c251e2af8f46a47c197e311d /contrib/net-speed.sh
parent9832a0d93a65eba36d5aaa99ec5d7b94a29bb3ba (diff)
Consolidate measure speed script (#188)
* remove old net speed scripts * add reworked net-speed script * add space between rate and speed suffix it is easier to parse visually * only use intervals greater zero * add ifaces examples * catch negativ byte count happens when the counter resets
Diffstat (limited to 'contrib/net-speed.sh')
-rwxr-xr-xcontrib/net-speed.sh79
1 files changed, 79 insertions, 0 deletions
diff --git a/contrib/net-speed.sh b/contrib/net-speed.sh
new file mode 100755
index 0000000..71a563f
--- /dev/null
+++ b/contrib/net-speed.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+# Authors:
+# - Moritz Warning <moritzwarning@web.de> (2016)
+# - Zhong Jianxin <azuwis@gmail.com> (2014)
+#
+# See file LICENSE at the project root directory for license information.
+#
+# i3status.conf should contain:
+# general {
+# output_format = i3bar
+# }
+#
+# i3 config looks like this:
+# bar {
+# status_command exec /usr/share/doc/i3status/contrib/net-speed.sh
+# }
+#
+# Single interface:
+# ifaces="eth0"
+#
+# Multiple interfaces:
+# ifaces="eth0 wlan0"
+#
+
+# Auto detect interfaces
+ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|wlp)')
+
+last_time=0
+last_rx=0
+last_tx=0
+rate=""
+
+readable() {
+ local bytes=$1
+ local kib=$(( bytes >> 10 ))
+ if [ $kib -lt 0 ]; then
+ echo "? K"
+ elif [ $kib -gt 1024 ]; then
+ local mib_int=$(( kib >> 10 ))
+ local mib_dec=$(( kib % 1024 * 976 / 10000 ))
+ if [ "$mib_dec" -lt 10 ]; then
+ mib_dec="0${mib_dec}"
+ fi
+ echo "${mib_int}.${mib_dec} M"
+ else
+ echo "${kib} K"
+ fi
+}
+
+update_rate() {
+ local time=$(date +%s)
+ local rx=0 tx=0 tmp_rx tmp_tx
+
+ for iface in $ifaces; do
+ read tmp_rx < "/sys/class/net/${iface}/statistics/rx_bytes"
+ read tmp_tx < "/sys/class/net/${iface}/statistics/tx_bytes"
+ rx=$(( rx + tmp_rx ))
+ tx=$(( tx + tmp_tx ))
+ done
+
+ local interval=$(( $time - $last_time ))
+ if [ $interval -gt 0 ]; then
+ rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
+ else
+ rate=""
+ fi
+
+ last_time=$time
+ last_rx=$rx
+ last_tx=$tx
+}
+
+i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
+do
+ read line
+ update_rate
+ echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1
+done)