-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
77 lines (68 loc) · 2.21 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/alecthomas/kingpin"
"github.com/caarlos0/speedtest-exporter/collector"
"github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// nolint: gochecknoglobals
var (
bind = kingpin.Flag("bind", "addr to bind the server").Short('b').Default(":9876").String()
debug = kingpin.Flag("debug", "show debug logs").Default("false").Bool()
format = kingpin.Flag("logFormat", "log format to use").Default("console").Enum("json", "console")
interval = kingpin.Flag("refresh.interval", "time between refreshes with speedtest").Default("30m").Duration()
server = kingpin.Flag("server", "speedtest server id").Short('s').Default("").String()
serverLabels = kingpin.Flag("showServerLabels", "whether or not to annotate speedtest results with details of the server").Default("false").Bool()
version = "master"
)
func main() {
kingpin.Version("speedtest-exporter version " + version)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *format == "console" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("enabled debug mode")
}
if *server != "" {
log.Info().Msgf("starting speedtest-exporter %s with server %s", version, *server)
} else {
log.Info().Msgf("starting speedtest-exporter %s", version)
}
prometheus.MustRegister(
collector.NewSpeedtestCollectorWithOpts(
cache.New(*interval, *interval),
collector.SpeedtestOpts{
Server: *server,
ShowServerLabels: *serverLabels,
},
),
)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(
w, `
<html>
<head><title>Speedtest Exporter</title></head>
<body>
<h1>Speedtest Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>
`,
)
})
log.Info().Msgf("listening on %s", *bind)
if err := http.ListenAndServe(*bind, nil); err != nil {
log.Fatal().Err(err).Msg("error starting server")
}
}