-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
zbx-speedtest.sh
executable file
·172 lines (153 loc) · 3.05 KB
/
zbx-speedtest.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
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
#!/usr/bin/env sh
set -e
DATA_FILE=/tmp/speedtest.json
usage() {
echo "Usage: \"$(basename "$0")\" OPTION"
echo
echo "-u: Display last measured upload speed"
echo "-d: Display last measured download speed"
echo "-j: Display last measured jitter"
echo "-p: Display last measured ping latency"
echo "-t: Display last measurement timestamp"
echo "-s: Display last server used for measurements"
echo "-m X: Fail/don't display data if it is older than X seconds"
echo
echo "-r|--run: Run speedtest"
}
bytes_to_mbit() {
echo "scale=2; $1 / 125000" | bc -l
}
get_data_timestamp() {
jq -r '.timestamp | fromdate' "$DATA_FILE"
}
get_last_ping_time() {
jq -r '.ping.latency' "$DATA_FILE"
}
get_last_jitter_time() {
jq -r '.ping.jitter' "$DATA_FILE"
}
show_last_download_speed() {
bytes_to_mbit "$(jq -r '.download.bandwidth' "$DATA_FILE")"
}
show_last_upload_speed() {
bytes_to_mbit "$(jq -r '.upload.bandwidth' "$DATA_FILE")"
}
show_server_info() {
data="$(jq -r '.server' "$DATA_FILE")"
id="$(echo "$data" | jq -r '.id')"
name="$(echo "$data" | jq -r '.name')"
location="$(echo "$data" | jq -r '.location')"
country="$(echo "$data" | jq -r '.country')"
echo "$id: $name @$location ($country)"
}
data_is_outdated() {
local data_ts
local now
data_ts=$(get_data_timestamp)
now=$(date '+%s')
[[ "$(( now - data_ts ))" -gt "$MAX_AGE" ]]
}
case "$1" in
-f|--data-file)
DATA_FILE="$2"
shift 2
;;
esac
# Default values
ACTION=run
MAX_AGE=3600
while test "$#" -gt 0
do
case "$1" in
-h|--help|help)
usage
exit 0
;;
-m|--max-age)
MAX_AGE="$2"
shift 2
;;
-d|--download)
ACTION=show_dl
shift
;;
-u|--upload)
ACTION=show_ul
shift
;;
-j|--jitter)
ACTION=show_jitter
shift
;;
-p|--ping)
ACTION=show_ping
shift
;;
-s|--server)
ACTION=show_server
shift
;;
-t|--timestamp)
ACTION=show_timestamp
shift
;;
-r|--run)
ACTION=run
shift
;;
--)
# end argument parsing
shift
break
;;
--*=|-*) # unsupported flags
echo "Error: Unsupported flag $1" >&2
usage
exit 1
;;
*)
# preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
if [[ "$ACTION" != "run" ]]
then
if [[ "$MAX_AGE" -gt 0 ]] && data_is_outdated
then
echo "Data is outdated. MAX_AGE is set to ${MAX_AGE} seconds. Please update with --run." >&2
exit 5
fi
fi
case "$ACTION" in
show_dl)
show_last_download_speed
;;
show_ul)
show_last_upload_speed
;;
show_jitter)
get_last_jitter_time
;;
show_ping)
get_last_ping_time
;;
show_server)
show_server_info
;;
show_timestamp)
get_data_timestamp
;;
run)
if speedtest --accept-license --accept-gdpr -f json > "${DATA_FILE}.new"
then
mv "${DATA_FILE}.new" "$DATA_FILE"
fi
;;
*)
usage
exit 2
esac