forked from ohthehugemanatee/netdata-speedtest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeedtest.chart.sh
50 lines (41 loc) · 1.36 KB
/
speedtest.chart.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
#!/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_check() {
require_cmd speedtest || return 1
# flushing temporary file content to something predictable
echo > $speedtest_tmpfile
return 0
}
speedtest_create() {
# create a chart with 2 dimensions
cat <<EOF
CHART system.connectionspeed '' "System Connection Speed" "Mbps" "connection speed" system.connectionspeed line $((speedtest_priority + 1)) $speedtest_update_every
DIMENSION down 'Down' absolute 1 1000000
DIMENSION up 'Up' absolute 1 1000000
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
speedtest --single --csv > $speedtest_tmpfile &
down=0
up=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)
fi
# write the result of the work.
cat <<VALUESEOF
BEGIN system.connectionspeed
SET down = $down
SET up = $up
END
VALUESEOF
return 0
}