forked from gremwell/netdata-speedtest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtest.chart.sh
More file actions
65 lines (56 loc) · 2.12 KB
/
speedtest.chart.sh
File metadata and controls
65 lines (56 loc) · 2.12 KB
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
# Netdata charts.d collector for fast.com internet speed test.
# Requires installed speedtest.com cli: `pip install speedtest-cli`
speedtest_update_every=60
speedtest_priority=100
speedtest_tmpfile="/tmp/speedtest_out.tmp"
speedtest_server=
speedtest_check() {
require_cmd speedtest || return 1
require_cmd bc || return 1
require_cmd xargs || return 1
# Find the server from the list
server=$(speedtest --list | grep -m 1 "$speedtest_server" | cut -d ')' -f 1 | xargs)
# Set the server id
speedtest_server=$server
# flushing temporary file content to something predictable
echo > $speedtest_tmpfile
return 0
}
speedtest_create() {
# create a chart with 2 dimensions and chart for latency
cat <<EOF
CHART system.connectionspeed '' "System Connection Speed" "Mbps" "connection speed" system.connectionspeed area $((speedtest_priority + 1)) $speedtest_update_every
DIMENSION down 'Down' absolute 1 1000000
DIMENSION up 'Up' absolute 1 1000000
CHART system.connectionlatency '' "Connection Latency" "ms" "connection speed" system.connectionspeed line $((speedtest_priority + 1)) $speedtest_update_every
DIMENSION latency 'Latency' absolute 1 1000
EOF
return 0
}
speedtest_update() {
# get the up and down speed from the previously executed . Parse them into separate values, and drop the Mbps.
speedtest_output=$(cat $speedtest_tmpfile)
# collect speed test results in background
server=${speedtest_server:+" --server $speedtest_server"}
(speedtest --single --csv $server 2> /dev/null || speedtest --single --csv) | echo $(cat -) > $speedtest_tmpfile &
down=0
up=0
latency=0
if [ -n "$speedtest_output" ]; then
down=$(echo "$speedtest_output" | cut -d ',' -f 7 | cut -d '.' -f 1)
up=$(echo "$speedtest_output" | cut -d ',' -f 8 | cut -d '.' -f 1)
latency=$(echo "$speedtest_output" | cut -d ',' -f 6 | echo "$(cat -) * 1000" | bc)
fi
# write the result of the work.
cat <<VALUESEOF
BEGIN system.connectionspeed
SET down = $down
SET up = -$up
END
BEGIN system.connectionlatency
SET latency = $latency
END
VALUESEOF
return 0
}