-
Notifications
You must be signed in to change notification settings - Fork 13
/
rateswatch.sh
executable file
·65 lines (54 loc) · 1.93 KB
/
rateswatch.sh
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
#!/bin/bash
devs=${@:?Which devices?}
function get_rx_bytes() {
ethtool -S $1 | grep -E 'rx_bytes_phy|vport_rx_bytes' | awk {'print $2'}
}
function get_tx_bytes() {
ethtool -S $1 | grep -E 'tx_bytes_phy|vport_tx_bytes' | awk {'print $2'}
}
function get_rx_pkts() {
ethtool -S $1 | grep -E 'rx_packets_phy|vport_rx_packets' | awk {'print $2'}
}
function get_tx_pkts() {
ethtool -S $1 | grep -E 'tx_packets_phy|vport_tx_packets' | awk {'print $2'}
}
function humanrate() {
local rate=$1
(( rate > 1000*1000*1000 )) && echo "$(bc <<< "scale=3; $rate/(1000*1000*1000)") G" && return
(( rate > 1000*1000 )) && echo "$(bc <<< "scale=3; $rate/(1000*1000)") M" && return
(( rate > 1000 )) && echo "$(bc <<< "scale=3; $rate/1000") K" && return
echo "$(bc <<< "scale=3; $rate/1000") K"
}
function rateswatch() {
local devs=$@
while true; do
for d in $devs; do
declare ${d}_b_tx=`get_tx_bytes $d`
declare ${d}_b_rx=`get_rx_bytes $d`
declare ${d}_b_ptx=`get_tx_pkts $d`
declare ${d}_b_prx=`get_rx_pkts $d`
done
sleep 1
for d in $devs; do
a_tx=`get_tx_bytes $d`
a_rx=`get_rx_bytes $d`
a_ptx=`get_tx_pkts $d`
a_prx=`get_rx_pkts $d`
b_tx=${d}_b_tx
b_rx=${d}_b_rx
b_ptx=${d}_b_ptx
b_prx=${d}_b_prx
echo -en " \r";
echo -en " dev: $d "
echo -en "\ttx: `humanrate $((($a_tx - ${!b_tx})*8))`ibs "
echo -en "\t(`humanrate $(($a_ptx - ${!b_ptx}))`pps)"
echo -en "\trx: `humanrate $((($a_rx - ${!b_rx})*8))`ibs "
echo -en "\t(`humanrate $(($a_prx - ${!b_prx}))`pps)"
echo ""
done
for d in $devs; do
echo -en "\033[1A\r";
done
done
}
rateswatch $devs