-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47_turn_watch.sh
More file actions
executable file
·35 lines (31 loc) · 884 Bytes
/
47_turn_watch.sh
File metadata and controls
executable file
·35 lines (31 loc) · 884 Bytes
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
#!/usr/bin/env bash
set -euo pipefail
URL="${1:-http://127.0.0.1:9641/metrics}"
echo "Watching $URL (CTRL-C to stop)"
prev_rx="" ; prev_tx=""
prev_t="$(date +%s)"
grab() {
# first match of the counter; if multiple realms exist, this sums them
curl -s "$URL" \
| awk '
/^turn_total_traffic_rcvb/ { rx+=$2 }
/^turn_total_traffic_sentb/ { tx+=$2 }
END { printf("%.0f %.0f\n", rx, tx) }
'
}
while :; do
now="$(date +%s)"
read -r rx tx < <(grab || echo "0 0")
if [[ -n "$prev_rx" ]]; then
dt=$(( now - prev_t ))
[[ $dt -lt 1 ]] && dt=1
drx=$(( rx - prev_rx ))
dtx=$(( tx - prev_tx ))
rxbps=$(( drx / dt ))
txbps=$(( dtx / dt ))
printf "%s RX: %8d B/s TX: %8d B/s (totals: RX=%d TX=%d)\n" \
"$(date +%H:%M:%S)" "$rxbps" "$txbps" "$rx" "$tx"
fi
prev_rx="$rx"; prev_tx="$tx"; prev_t="$now"
sleep 1
done