Commit 20915fbe authored by Glen Mével's avatar Glen Mével

`measure-net-speed`: show the network real-time bit rate

parent 34a8b7ee
#!/bin/sh
# got here: http://code.stapelberg.de/git/i3status/tree/contrib
# Public Domain
# (someone claimed the next lines would be useful for…
# people. So here goes: © 2012 Stefan Breunig
# stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de)
# path to store the old results in
path="/dev/shm/measure-net-speed"
# grabbing data for each adapter and summing them for combined display.
rx=0
tx=0
for intf in /sys/class/net/*/statistics ; do
[ "$intf" = /sys/class/net/lo/statistics ] && continue
read intf_rx < "${intf}/rx_bytes"
read intf_tx < "${intf}/tx_bytes"
rx=$(( $rx + $intf_rx ))
tx=$(( $tx + $intf_tx ))
done
# get the time
time=$(date +%s)
# write current data if file does not exist. Do not exit, this will cause
# problems if this file is sourced instead of executed as another process.
if ! [[ -f "${path}" ]]; then
echo "${time} ${rx} ${tx}" > "${path}"
chmod 0666 "${path}"
fi
# read previous state and update data storage
read old < "${path}"
echo "${time} ${rx} ${tx}" > "${path}"
# parse old data and calc time passed
old=(${old//;/ })
time_diff=$(( $time - ${old[0]} ))
# sanity check: has a positive amount of time passed
if [[ "${time_diff}" -gt 0 ]]; then
# calc bytes transferred, and their rate in byte/s
rx_diff=$(( $rx - ${old[1]} ))
tx_diff=$(( $tx - ${old[2]} ))
rx_rate=$(( $rx_diff / $time_diff ))
tx_rate=$(( $tx_diff / $time_diff ))
# shift by 10 bytes to get KiB/s. If the value is larger than
# 1024^2 = 1048576, then display MiB/s instead (simply cut off
# the last two digits of KiB/s). Since the values only give an
# rough estimate anyway, this improper rounding is negligible.
# incoming
rx_kib=$(( $rx_rate >> 10 ))
if [[ "$rx_rate" -gt 1048576 ]]; then
echo -n "${rx_kib:0:-3}.${rx_kib: -3:-2} M↓"
else
echo -n "${rx_kib} k↓"
fi
echo -n "  "
# outgoing
tx_kib=$(( $tx_rate >> 10 ))
if [[ "$tx_rate" -gt 1048576 ]]; then
echo -n "${tx_kib:0:-3}.${tx_kib: -3:-2} M↑"
else
echo -n "${tx_kib} k↑"
fi
else
echo -n "?"
fi
echo -n " "
#!/bin/sh
# got here: http://code.stapelberg.de/git/i3status/tree/contrib
# Public Domain
# (someone claimed the next lines would be useful for…
# people. So here goes: © 2012 Stefan Breunig
# stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de)
# append i3status output to the measure-net-speed’s one.
# the quote and escape magic is required to get valid
# JSON output, which is expected by i3bar (if you want
# colors, that is. Otherwise plain text would be fine).
# For colors, your i3status.conf should contain:
# general {
# output_format = i3bar
# }
# i3 config looks like this:
# bar {
# status_command measure-net-speed-i3status
# }
i3status | (read line && echo $line && read line && echo $line && while :
do
read line
dat=$(measure-net-speed)
dat="[{ \"full_text\": \"${dat}\" },"
echo "${line/[/$dat}" || exit 1
done)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment