-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
121 lines (99 loc) · 3.02 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
package main
import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"github.com/devon-mar/radius-exporter/collector"
"github.com/devon-mar/radius-exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
showVer = flag.Bool("version", false, "Show the version")
configPath = flag.String("config", "config.yml", "Path to the config file.")
listenAddr = flag.String("web.listen-address", ":9881", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
logLevel = flag.String("log.level", "info", "The log level.")
exporterConfig *config.Config
exporterVersion = "DEVELOPMENT"
exporterSha = "abcd"
)
func metricsHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "No target specified", http.StatusBadRequest)
slog.Error("No target specified.")
return
}
moduleName := r.URL.Query().Get("module")
if moduleName == "" {
http.Error(w, "No module specified", http.StatusBadRequest)
slog.Debug("No module specified.")
return
}
module, ok := exporterConfig.Modules[moduleName]
if !ok {
http.Error(w, "Unknown module", http.StatusBadRequest)
slog.Debug("Unknown module", "module", moduleName)
return
}
registry := prometheus.NewRegistry()
registry.MustRegister(collector.NewCollector(&target, &module))
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
handler.ServeHTTP(w, r)
}
func configureLog() {
var slogLevel slog.Level
if err := slogLevel.UnmarshalText([]byte(*logLevel)); err != nil {
slog.Error("error parsing log level", "err", err)
os.Exit(1)
}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slogLevel}))
slog.SetDefault(logger)
}
func main() {
flag.Parse()
if *showVer {
fmt.Printf("Version: %s\n", exporterVersion)
fmt.Printf("SHA: %s\n", exporterSha)
os.Exit(0)
}
slog.Info("Starting RADIUS exporter")
configureLog()
c, err := config.LoadFromFile(*configPath)
if err != nil {
log.Fatal(err)
}
exporterConfig = c
http.Handle(*metricsPath, http.HandlerFunc(metricsHandler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>RADIUS Exporter</title></head>
<body>
<h1>RADIUS Exporter</h1>
<a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
server := http.Server{Addr: *listenAddr}
idleConnsClosed := make(chan struct{})
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
sig := <-sigCh
slog.Warn("received signal", "signal", sig)
if err := server.Shutdown(context.Background()); err != nil {
slog.Error("HTTP server shutdown error", "err", err)
}
close(idleConnsClosed)
}()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
slog.Error("HTTP server ListenAndServe", "err", err)
}
<-idleConnsClosed
}