This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
speedtest_exporter.go
146 lines (124 loc) · 4.54 KB
/
speedtest_exporter.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2016, 2017 Nicolas Lamirault <[email protected]>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/dchest/uniuri"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
prom_version "github.com/prometheus/common/version"
"github.com/nlamirault/speedtest_exporter/speedtest"
"github.com/nlamirault/speedtest_exporter/version"
)
const (
namespace = "speedtest"
)
var (
ping = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "ping"),
"Latency (ms)",
nil, nil,
)
download = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "download"),
"Download bandwidth (Mbps).",
nil, nil,
)
upload = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "upload"),
"Upload bandwidth (Mbps).",
nil, nil,
)
)
// Exporter collects Speedtest stats from the given server and exports them using
// the prometheus metrics package.
type Exporter struct {
Client *speedtest.Client
}
// NewExporter returns an initialized Exporter.
func NewExporter(config string, server string, interval time.Duration) (*Exporter, error) {
log.Infof("Setup Speedtest client with interval %s", interval)
client, err := speedtest.NewClient(config, server)
if err != nil {
return nil, fmt.Errorf("Can't create the Speedtest client: %s", err)
}
log.Debugln("Init exporter")
return &Exporter{
Client: client,
}, nil
}
// Describe describes all the metrics ever exported by the Speedtest exporter.
// It implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- ping
ch <- download
ch <- upload
}
// Collect fetches the stats from configured Speedtest location and delivers them
// as Prometheus metrics.
// It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
log.Infof("Speedtest exporter starting")
if e.Client == nil {
log.Errorf("Speedtest client not configured.")
return
}
metrics := e.Client.NetworkMetrics()
ch <- prometheus.MustNewConstMetric(ping, prometheus.GaugeValue, metrics["ping"])
ch <- prometheus.MustNewConstMetric(download, prometheus.GaugeValue, metrics["download"])
ch <- prometheus.MustNewConstMetric(upload, prometheus.GaugeValue, metrics["upload"])
log.Infof("Speedtest exporter finished")
}
func init() {
prometheus.MustRegister(prom_version.NewCollector("speedtest_exporter"))
}
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9112", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configURL = flag.String("speedtest.config-url", "http://c.speedtest.net/speedtest-config.php?x="+uniuri.New(), "Speedtest configuration URL")
serverURL = flag.String("speedtest.server-url", "http://c.speedtest.net/speedtest-servers-static.php?x="+uniuri.New(), "Speedtest server URL")
//interval = flag.Int("interval", 60*time.Second, "Interval for metrics.")
)
flag.Parse()
if *showVersion {
fmt.Printf("Speedtest Prometheus exporter. v%s\n", version.Version)
os.Exit(0)
}
log.Infoln("Starting speedtest exporter", prom_version.Info())
log.Infoln("Build context", prom_version.BuildContext())
interval := 60 * time.Second
exporter, err := NewExporter(*configURL, *serverURL, interval)
if err != nil {
log.Errorf("Can't create exporter : %s", err)
os.Exit(1)
}
log.Infoln("Register exporter")
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Speedtest Exporter</title></head>
<body>
<h1>Speedtest Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}