-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtop
executable file
·381 lines (298 loc) · 9.78 KB
/
strtop
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/sh
# strtop
#
# Shows realtime statistics about a Syncthing relay server.
#
# Requires: jq, ncurses-bin, curl, bc.
#
# This script is in the public domain and was originally written by
# Dennis Murczak <https://github.com/lortordermur/>
#### GLOBALS ####
relayserver="localhost"
relayport="22070"
relaystatus=
viewwidth="78"
# Interval at which the throughput history (and everything else) is updated
historyinterval="10"
# Maximum 10 s throughput history (default 24 = 4 minutes)
maxtphistsize="24"
# Binary units of data
KIBIBYTE="1024"
MEBIBYTE="$(($KIBIBYTE * $KIBIBYTE))"
GIBIBYTE="$(($MEBIBYTE * $KIBIBYTE))"
TEBIBYTE="$(($GIBIBYTE * $KIBIBYTE))"
PEBIBYTE="$(($TEBIBYTE * $KIBIBYTE))"
EXBIBYTE="$(($PEBIBYTE * $KIBIBYTE))"
# ANSI formatting codes
A_BOLD="\e[1m"
A_NOBOLD="\e[21m"
A_UL="\e[4m"
A_NOUL="\e[24m"
A_INV="\e[7m"
A_NOINV="\e[27m"
A_RED="\e[31m"
A_NOCOLOR="\e[39m"
A_NORMAL="\e[0m"
# Combined ANSI formatting codes
A_ERR="$A_RED$A_BOLD"
A_NOERR="$A_NOBOLD$A_NOCOLOR"
#### FUNCTIONS ####
# Portable fixed echo with support for escape codes (like 'echo -e')
echo () {
printf "%b\n" "$*"
}
# Create a string of a single character
charstring () {
local wid="$viewwidth"
local chr=" "
[ "$1" ] && chr="$1"
[ "$2" ] && wid="$2"
printf "%*s" "$wid" | tr " " "$chr"
}
newline () {
printf "\n"
tput cuf $coloffset
}
# Clear to end of line, then print a newline
finline () {
tput el
newline
}
echoline () {
printf "%b" "$*"
finline
}
errorexit () {
echo "$A_ERR$1$A_NOERR"
exit 1
}
# Check whether a command exists in the system, optionally with a custom error
# message
commandexists () {
command -v "$1" > /dev/null || { echo "Required command '$1' not found! $2" && echo && exit 1; }
}
# Seconds since the UNIX epoch
secondsepoch () {
echo $(($(date +%s%N) / 1000000))
}
# Arithmetic division using bc, and printf for rounding
divf () {
local decplaces="1" # Default to one decimal place
[ "$3" ] && decplaces="$3"
LANG=C printf "%.${decplaces}f" $(echo "$1/$2" | bc -l)
}
# Turn a byte amount into something human-readable
formatbytes () {
local bytes="0"
local decplaces
local bytestring
[ "$1" ] && bytes="$1"
[ "$3" ] && decplaces="$3"
if [ "$bytes" -eq "0" ]; then
if [ -z "$2" ]; then
bytestring="0 B"
else
bytestring="$2"
fi
elif [ "$bytes" -le "$KIBIBYTE" ]; then
bytestring="$bytes B"
elif [ "$bytes" -le "$MEBIBYTE" ]; then
bytestring="$(divf "$bytes" "$KIBIBYTE" "$decplaces") KiB"
elif [ "$bytes" -le "$GIBIBYTE" ]; then
bytestring="$(divf "$bytes" "$MEBIBYTE" "$decplaces") MiB"
elif [ "$bytes" -le "$TEBIBYTE" ]; then
bytestring="$(divf "$bytes" "$GIBIBYTE" "$decplaces") GiB"
elif [ "$bytes" -le "$PEBIBYTE" ]; then
bytestring="$(divf "$bytes" "$TEBIBYTE" "$decplaces") TiB"
elif [ "$bytes" -le "$EXBIBYTE" ]; then
bytestring="$(divf "$bytes" "$PEBIBYTE" "$decplaces") PiB"
else
bytestring="$(divf "$bytes" "$EXBIBYTE" "$decplaces") EiB"
fi
echo $bytestring
}
formatbits () {
formatbytes $(($1 / 8)) $2 $3
}
formatkbits () {
formatbytes $(($1 * 128)) $2 $3
}
# Turn seconds into a nice and brief time string
formatseconds () {
local timestring
local interval="$1"
local days=$(($interval / 60 / 60 / 24))
local hours=$(($interval / 60 / 60 % 24))
local minutes=$(($interval / 60 % 60))
local seconds=$(($interval % 60))
if [ "$days" -gt "0" ]; then
timestring="$timestring$days d "
fi
if [ "$days" -lt "10" ] && [ "$hours" -gt "0" ]; then
timestring="$timestring$hours h "
fi
if [ "$days" -eq "0" ] && [ "$minutes" -gt "0" ]; then
timestring="$timestring$minutes m "
fi
if [ "$days" -eq "0" ] && [ "$hours" -eq "0" ] && [ "$minutes" -lt "10" ] && [ "$seconds" -gt "0" ]; then
timestring="$timestring$seconds s"
fi
echo $timestring
}
# Populates and shifts the throughput history
updatetphistory () {
local spd="$1"
[ -z "$spd" ] && $spd="0"
local histsize=$(echo $tphistory | wc -w)
if [ "$histsize" -eq "0" ]; then
tphistory=$spd
elif [ "$histsize" -lt "$maxtphistsize" ]; then
tphistory="$tphistory $spd"
elif [ "$histsize" -eq "$maxtphistsize" ]; then
tphistory=$(echo $tphistory | cut -d ' ' -f2-)
tphistory="$tphistory $spd"
fi
}
usage () {
echo "Usage: $(basename $0) [relayserver] [relayport]"
echo "
This software is a 'top' for Syncthing relays. It continually displays basic
information, as well as the recent throughput history as a bar graph.
Parameters
----------
[relayserver]: IP address or network name of the relay. Defaults to
'localhost'.
[relayport]: Port to connect to. Defaults to 22070.
"
exit 0
}
#### MAIN ####
# Parameter handler
if [ "$1" ]; then
case "$1" in
"--help")
usage
;;
*)
relayserver="$1"
;;
esac
[ "$2" ] && relayport="$2"
fi
commandexists "curl"
commandexists "jq"
commandexists "bc"
commandexists "tput" "Please install the ncurses binaries."
# Generate an inverted horizontal separator bar
separator="$A_INV$(charstring "-")$A_NOINV"
statusurl="$relayserver:$relayport/status"
tphistory=
# Some terminal init before the main loop
tput clear # Clear the screen
tput rmam # Disable line wrap
# Set up traps for window size change and forced exit
trap "tput clear" WINCH
trap "tput reset; exit 0" TERM ABRT KILL HUP
# Main loop start
while true; do
# Get the server status JSON, plus error checking
relaystatus=$(curl -s -m "$historyinterval" "$statusurl")
[ "$?" -ne "0" ] && errorexit "Error contacting relay '$relayserver:$relayport'!"
v=$(echo $relaystatus | jq '.version')
[ "$?" -eq "4" ] && errorexit "No valid response from relay '$relayserver:$relayport'!"
# Switch off cursor (needs to come after error checks but before drawing)
tput civis
# Determine on which terminal column to start
coloffset=$(($(tput cols) / 2 - $viewwidth / 2))
[ "$coloffset" -lt "0" ] && coloffset="0"
# Home the cursor
tput cup 0 $coloffset
# Headline
providedby=$(echo $relaystatus | jq '.options["provided-by"]' | tr -d '"')
headline="Status of $A_BOLD$relayserver:$relayport$A_NOBOLD"
[ "$providedby" ] && headline="$headline -- $A_BOLD$providedby$A_NOBOLD"
echoline $headline
stversion=$(echo $relaystatus | jq '.version' | tr -d '"')
serverinfo=
if [ "$stversion" ]; then
serverinfo="Running strelaysrv $stversion"
else
serverinfo="Running unknown server version"
fi
os=$(echo $relaystatus | jq '.goOS' | tr -d '"')
arch=$(echo $relaystatus | jq '.goArch' | tr -d '"')
serverinfo="$serverinfo on $os/$arch"
echoline $serverinfo
echoline $separator
# Connection information
activesessions=$(echo $relaystatus | jq '.numActiveSessions')
numconnections=$(echo $relaystatus | jq '.numConnections')
echoline "Active sessions: $A_BOLD$activesessions$A_NOBOLD, number of connections: $A_BOLD$numconnections$A_NOBOLD"
globalrate=$(echo $relaystatus | jq '.options["global-rate"]')
persessionrate=$(echo $relaystatus | jq '.options["per-session-rate"]')
echoline "Global rate limit: $A_BOLD$(formatbytes $globalrate "unlimited" "2")/s$A_NOBOLD, per session limit: $A_BOLD$(formatbytes $persessionrate "unlimited" "2")/s$A_NOBOLD"
# Throughput information
bytesproxied=$(echo $relaystatus | jq '.bytesProxied')
echoline "Data proxied: $A_BOLD$(formatbytes $bytesproxied "--" "3")$A_NOBOLD"
curspd=$(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[0]')
kbps10s=$(formatkbits $curspd "--")
kbps1m=$(formatkbits $(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[1]') "--")
kbps5m=$(formatkbits $(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[2]') "--")
kbps15m=$(formatkbits $(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[3]') "--")
kbps30m=$(formatkbits $(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[4]') "--")
kbps60m=$(formatkbits $(echo $relaystatus | jq '.kbps10s1m5m15m30m60m[5]') "--")
echoline "Throughput: $A_BOLD$kbps10s/s$A_NOBOLD (10 s), $A_BOLD$kbps1m/s$A_NOBOLD (1 m), $A_BOLD$kbps5m/s$A_NOBOLD (5 m), "
echoline " $A_BOLD$kbps15m/s$A_NOBOLD (15 m), $A_BOLD$kbps30m/s$A_NOBOLD (30 m), $A_BOLD$kbps60m/s$A_NOBOLD (60 m)"
newline
# Throughput history graph
updatetphistory $curspd
# Calculate bar heights from history "array"
tphistmax="0"
barheights=""
for hist in $tphistory; do
[ "$hist" -gt "$tphistmax" ] && tphistmax=$hist
if [ "$tphistmax" -eq "0" ]; then
tphm="0.001" # Kludge variable to prevent a divide by zero
else
tphm=$tphistmax
fi
barh=$(printf "%.0f" $(echo "$hist/$tphm*5" | bc -l))
barheights="$barheights$barh "
done
# Create filler space if the history is not yet filled up
fillspace=$(($maxtphistsize - $(echo $tphistory | wc -w)))
fillstring=$(charstring " " "$(($fillspace * 2))")
# Output the filler space and bars, line by line
for barline in 5 4 3 2 1; do
blstring=" | $fillstring"
for barh in $barheights; do
if [ "$barh" -ge "$barline" ]; then
blstring="$blstring$A_INV#$A_NOINV "
else
blstring="$blstring "
fi
done
# Maximum history throughput (what the graph is scaled to) at top right
graphlabel=""
if [ "$barline" -eq "5" ]; then
graphlabel="$(formatkbits $tphistmax)/s"
elif [ "$barline" -eq "1" ]; then
graphlabel="$(formatkbits "0")/s"
fi
echoline "$blstring|$graphlabel"
done
newline
# Uptime information
uptimeseconds=$(echo $relaystatus | jq '.uptimeSeconds')
starttime=$(echo $relaystatus | jq '.startTime' | tr -d '"')
echoline "Server up for $A_BOLD$(formatseconds $uptimeseconds)$A_NOBOLD since $(date -d $starttime)"
echoline $separator
echoline "Press CTRL-C to exit"
# Show the cursor while waiting
tput cnorm
sleep $historyinterval
done
# Main loop end
# Fully reset the terminal in order to reliably restore line-wrap
tput reset
exit 0