-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
106 lines (83 loc) · 2.3 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/czerwonk/wifi_exporter/configuration"
"github.com/czerwonk/wifi_exporter/unifi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
const version string = "0.4.0"
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9120", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configPath = flag.String("config.path", "config.yml", "Path to config file")
config *configuration.Config
)
func main() {
flag.Parse()
if *showVersion {
printVersion()
os.Exit(0)
}
err := loadConfig()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
startServer()
}
func printVersion() {
fmt.Println("wifi_exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): Daniel Czerwonk")
fmt.Println("Metric exporter for wifi controllers")
}
func loadConfig() error {
var err error
config, err = configuration.Load(*configPath)
if err != nil {
return err
}
return nil
}
func startServer() {
log.Infof("Starting wifi exporter (Version: %s)\n", version)
http.HandleFunc(*metricsPath, errorHandler(handleMetricsRequest))
log.Infof("Listening for %s on %s\n", *metricsPath, *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
log.Errorln(err)
}
}
}
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) error {
reg := prometheus.NewRegistry()
addUnifiCollectors(reg)
addRuckusCollectors(reg)
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError})
h.ServeHTTP(w, r)
return nil
}
func addUnifiCollectors(reg *prometheus.Registry) {
for _, u := range config.Unifi {
c, err := unifi.NewUnifiCollector(u.ApiUrl, u.ApiUser, u.ApiPass)
if err != nil {
log.Errorln(err)
} else {
reg.MustRegister(c)
}
}
}
func addRuckusCollectors(reg *prometheus.Registry) {
}