blob: 8877d285df9ef8555290ed2fbd47d6c02eb28831 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/bash
# Copyright (C) 2012 Stefan Breunig <stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de>
# Copyright (C) 2014 kaueraal
# Copyright (C) 2015 Thiago Perrotta <perrotta dot thiago at poli dot ufrj dot br>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
case "$BLOCK_BUTTON" in
2) $TERMINAL -e nvim "$0" ;;
esac
# Use the provided interface, otherwise the device used for the default route.
if [[ -n $BLOCK_INSTANCE ]]; then
INTERFACE=$BLOCK_INSTANCE
else
INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }')
fi
[ -z "$INTERFACE" ] && echo && exit
# Issue #36 compliant.
if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "$(cat "/sys/class/net/${INTERFACE}/operstate")" = "up" ]
then
# echo "$INTERFACE down"
# echo "$INTERFACE down"
# echo "#FF0000"
echo " "
exit 0
fi
# path to store the old results in
path="/dev/shm/$(basename "$0")-${INTERFACE}"
# grabbing data for each adapter.
read -r rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes"
read -r tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes"
# get 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 -r 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
[[ "${time_diff}" -gt 0 ]] || { echo " "; exit; }
# 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
# incoming
rx_kib=$(( rx_rate >> 10 ))
if [ "$rx_kib" -gt 1 ]; then
# echo -n "📥 "
echo -n "↓ "
if [[ "$rx_rate" -gt 1048576 ]]; then
printf '%sM' "$(echo "scale=1; $rx_kib / 1024" | bc)"
else
echo -n "${rx_kib}K"
fi
fi
# outgoing
tx_kib=$(( tx_rate >> 10 ))
if [ "$tx_kib" -gt 1 ]; then
# echo -n "📤 "
echo -n " ↑ "
if [[ "$tx_rate" -gt 1048576 ]]; then
printf '%sM' "$(echo "scale=1; $tx_kib / 1024" | bc)"
else
echo -n "${tx_kib}K"
fi
fi
# echo
# echo
# echo "$BLOCK_COLOR_LEVEL1"
|