forked from robinmanuelthiel/speedtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtest.sh
executable file
·47 lines (43 loc) · 1.4 KB
/
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
#!/bin/sh
# These values can be overwritten with env variables
LOOP="${LOOP:-false}"
LOOP_DELAY="${LOOP_DELAY:-60}"
DB_SAVE="${DB_SAVE:-false}"
DB_HOST="${DB_HOST:-http://localhost:8086}"
DB_NAME="${DB_NAME:-speedtest}"
DB_USERNAME="${DB_USERNAME:-admin}"
DB_PASSWORD="${DB_PASSWORD:-password}"
run_speedtest()
{
DATE=$(date +%s)
HOSTNAME=$(hostname)
# Start speed test
echo "Running a Speed Test..."
JSON=$(speedtest --accept-license --accept-gdpr -f json)
DOWNLOAD="$(echo $JSON | jq -r '.download.bandwidth')"
UPLOAD="$(echo $JSON | jq -r '.upload.bandwidth')"
echo "Your download speed is $(($DOWNLOAD / 125000 )) Mbps ($DOWNLOAD Bytes/s)."
echo "Your upload speed is $(($UPLOAD / 125000 )) Mbps ($UPLOAD Bytes/s)."
# Save results in the database
if $DB_SAVE;
then
echo "Saving values to database..."
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "download,host=$HOSTNAME value=$DOWNLOAD $DATE"
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "upload,host=$HOSTNAME value=$UPLOAD $DATE"
echo "Values saved."
fi
}
if $LOOP;
then
while :
do
run_speedtest
echo "Running nest test in ${LOOP_DELAY}s..."
echo ""
sleep $LOOP_DELAY
done
else
run_speedtest
fi