diff options
author | Moritz Warning <moritzwarning@web.de> | 2017-01-02 16:06:06 +0100 |
---|---|---|
committer | Michael Stapelberg <stapelberg@users.noreply.github.com> | 2017-01-02 16:06:06 +0100 |
commit | 3f6d71feb9b620f28396e7c980323f92081dc2fb (patch) | |
tree | 0ef43e8ec7ca9497c251e2af8f46a47c197e311d | |
parent | 9832a0d93a65eba36d5aaa99ec5d7b94a29bb3ba (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
-rw-r--r-- | contrib/measure-net-speed-i3status.bash | 27 | ||||
-rwxr-xr-x | contrib/measure-net-speed.bash | 75 | ||||
-rwxr-xr-x | contrib/net-speed | 100 | ||||
-rwxr-xr-x | contrib/net-speed.sh | 79 |
4 files changed, 79 insertions, 202 deletions
diff --git a/contrib/measure-net-speed-i3status.bash b/contrib/measure-net-speed-i3status.bash deleted file mode 100644 index 4eec33b..0000000 --- a/contrib/measure-net-speed-i3status.bash +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -# 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.bash -# } - -i3status | (read line && echo $line && read line && echo $line && while : -do - read line - dat=$(measure-net-speed.bash) - dat="[{ \"full_text\": \"${dat}\" }," - echo "${line/[/$dat}" || exit 1 -done)
\ No newline at end of file diff --git a/contrib/measure-net-speed.bash b/contrib/measure-net-speed.bash deleted file mode 100755 index cef4004..0000000 --- a/contrib/measure-net-speed.bash +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# 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. -# You can find the paths to your adapters using -# find /sys/devices -name statistics -# If you have more (or less) than two adapters, simply adjust the script here -# and in the next block. -eth0="/sys/devices/pci0000:00/0000:00:19.0/net/eth0/statistics" -wlan0="/sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlan0/statistics" -read eth0_rx < "${eth0}/rx_bytes" -read eth0_tx < "${eth0}/tx_bytes" -read wlan0_rx < "${wlan0}/rx_bytes" -read wlan0_tx < "${wlan0}/tx_bytes" - -# get time and sum of rx/tx for combined display -time=$(date +%s) -rx=$(( $eth0_rx + $wlan0_rx )) -tx=$(( $eth0_tx + $wlan0_tx )) - -# 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
\ No newline at end of file diff --git a/contrib/net-speed b/contrib/net-speed deleted file mode 100755 index 7f332b0..0000000 --- a/contrib/net-speed +++ /dev/null @@ -1,100 +0,0 @@ -#!/bin/sh - -# Copyright © 2014 Zhong Jianxin <azuwis@gmail.com> -# -# See file LICENSE at the project root directory for license information. -# -# Thank Stefan Breunig for the original implementation, see -# contrib/measure-net-speed.bash. - -# i3status.conf should contain: -# general { -# output_format = i3bar -# } - -# i3 config looks like this: -# bar { -# status_command exec /usr/share/doc/i3status/contrib/net-speed -# } - -# Single interface -#ifaces="eth0" - -# Multiple interfaces -#ifaces="eth0 wlan0" - -# Auto detect -ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan)') - -# Interval must be the same as in i3status.conf -#interval=5 - -if [ -z "$XDG_CONFIG_HOME" ]; then - XDG_CONFIG_HOME="${HOME}/.config" -fi - -# Auto detect -if [ -f "${XDG_CONFIG_HOME}/i3status/config" ]; then - i3status_conf="${XDG_CONFIG_HOME}/i3status/config" -elif [ -f ~/.i3status.conf ]; then - i3status_conf=~/.i3status.conf -else - i3status_conf="/etc/i3status.conf" -fi - -if [ -f "$i3status_conf" ]; then - interval=$(grep -o '^[[:space:]]*interval[[:space:]]*=[[:space:]]*[[:digit:]]\+' $i3status_conf | grep -o '[[:digit:]]\+') -fi - -if [ x"$interval" = x ]; then - interval=5 -fi - -last_rx=0 -last_tx=0 -rate="" - -readable() { - local byte=$1 - local kib=$(( byte >> 10 )) - if [ "$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 rx=0 - local tx=0 - for iface in $ifaces; do - local tmp_rx - local tmp_tx - local stat="/sys/class/net/${iface}/statistics" - read tmp_rx < "${stat}/rx_bytes" - read tmp_tx < "${stat}/tx_bytes" - rx=$(( rx + tmp_rx )) - tx=$(( tx + tmp_tx )) - done - rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑" - last_rx=$rx - last_tx=$tx -} - -# while :; do -# update_rate -# echo "$rate" -# sleep "$interval" -# done - -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) 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) |