Skip to content

Instantly share code, notes, and snippets.

@maximilize
Created September 16, 2018 14:56
Show Gist options
  • Save maximilize/e0dbaaea47bb5509e0e75d81e9243a9d to your computer and use it in GitHub Desktop.
Save maximilize/e0dbaaea47bb5509e0e75d81e9243a9d to your computer and use it in GitHub Desktop.
#!/bin/bash
# Authors:
# - Moritz Warning <moritzwarning@web.de> (2016)
# - Zhong Jianxin <azuwis@gmail.com> (2014)
# - maximilize
#
# See file LICENSE at the project root directory for license information.
#
# Fork from https://github.com/i3/i3status/blob/master/contrib/net-speed.sh
# Use with xfce "Generic Monitor" applet.
#
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() {
if ! qvm-ls --no-spinner --raw-data --fields NAME,STATE,FLAGS 2>/dev/null | grep -q '^sys-net|Running|...N....$'; then
echo "Offline"
return
fi
local ifaces=$(qvm-run -p sys-net "ls /sys/class/net" | grep -E '^(eth|wlan|enp|wlp|wls)')
local time=$(date +%s)
local rx=0 tx=0 tmp_rx tmp_tx
local rate=""
for iface in $ifaces; do
read tmp_rx tmp_tx < <(qvm-run -p sys-net "cat /sys/class/net/${iface}/statistics/{rx,tx}_bytes" | tr '\n' ' ')
rx=$(( rx + tmp_rx ))
tx=$(( tx + tmp_tx ))
done
if [ -e /run/qubes/network-status ]; then
local last_time last_rx last_tx
read last_time last_rx last_tx < <(cat /run/qubes/network-status)
local interval=$(( $time - $last_time ))
if [ $interval -gt 0 ]; then
echo "$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
fi
fi
echo "$time $rx $tx" >/run/qubes/network-status
}
update_rate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment