-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (103 loc) · 3.47 KB
/
main.go
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
package main
import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go"
"github.com/johnsto/speedtest"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
)
func main() {
location := getEnvVariable("SPEEDFLUX_LOCATION")
userName := getEnvVariable("SPEEDFLUX_USER")
password := getEnvVariable("SPEEDFLUX_PASS")
protocol := getEnvVariable("SPEEDFLUX_PROTOCOL")
host := getEnvVariable("SPEEDFLUX_HOST")
port := getEnvVariable("SPEEDFLUX_PORT")
db := getEnvVariable("SPEEDFLUX_DB")
interval, err := strconv.Atoi(getEnvVariable("SPEEDFLUX_INTERVAL"))
if err != nil {
log.Fatalf("Interval: %d could not successfully be parsed: %v", interval, err)
}
log.Printf("starting measurements for location %s every %d minute(s)", location, interval)
for true {
go measure(location, protocol, host, port, userName, password, db)
log.Printf("starting measurement and next iteration in %d minutes", interval)
time.Sleep(time.Duration(interval) * time.Minute)
}
}
func measure(location string, protocol string, host string, port string, userName string, password string, db string) {
down, up := testSpeed()
sendToInflux(down, up, location, fmt.Sprintf("%s://%s:%s", protocol, host, port), db, fmt.Sprintf("%s:%s", userName, password))
}
func sendToInflux(down, up int, location, serverUrl, db, authToken string) {
log.Printf("sending data to influxdb server %s", serverUrl)
client := influxdb2.NewClient(serverUrl, authToken)
writeAPI := client.WriteAPIBlocking("", db)
measurement := influxdb2.NewPoint("speed",
map[string]string{
"location": location,
},
map[string]interface{}{
"down": down,
"up": up,
},
time.Now())
err := writeAPI.WritePoint(context.Background(), measurement)
if err != nil {
log.Printf("Write error: %s\n", err.Error())
}
log.Print("Data sent to influxdb successfully")
}
func testSpeed() (int, int) {
const timeout = 10 * time.Second
log.Print("starting speedtest exporter")
log.Print("fetching server list")
settings, err := speedtest.FetchSettings()
if err != nil {
log.Printf("error: %v", err)
return 0, 0
}
log.Printf("%v found.\n", len(settings.Servers))
log.Printf("Fetching config...\n")
config, err := speedtest.FetchConfig()
if err != nil {
log.Printf("Couldn't read config: %v", err)
return 0, 0
}
settings.UpdateDistances(config.Client.Lat, config.Client.Lon)
log.Printf(" ISP: %v\n", config.Client.IspName)
log.Printf(" Location: %v, %v\n\n", config.Client.Lat, config.Client.Lon)
var server speedtest.Server
settings.Servers.SortByDistance()
server = settings.Servers[0]
log.Printf("Using server %d. %v, %v, %v (%dkm)\n",
server.ID, server.Sponsor, server.Name, server.Country, int(server.Distance))
client := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
},
},
}
benchmarkDown := speedtest.NewDownloadBenchmark(client, server)
log.Print("Testing download speed... ")
rateDown := speedtest.RunBenchmark(benchmarkDown, 4, 16, timeout)
log.Println(speedtest.NiceRate(rateDown))
benchmarkUp := speedtest.NewUploadBenchmark(client, server)
log.Print("Testing upload speed... ")
rateUp := speedtest.RunBenchmark(benchmarkUp, 4, 16, timeout)
log.Println(speedtest.NiceRate(rateUp))
return rateDown, rateUp
}
func getEnvVariable(v string) string {
value, found := os.LookupEnv(v)
if !found {
log.Fatalf("ENV variable %s is not set. Quitting", v)
}
return value
}