-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
157 lines (136 loc) · 4.38 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/AthennaMind/opnsense-exporter/internal/collector"
"github.com/AthennaMind/opnsense-exporter/internal/options"
"github.com/AthennaMind/opnsense-exporter/opnsense"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
)
var version = ""
func main() {
options.Init()
logger, err := options.Logger()
if err != nil {
fmt.Fprintf(os.Stderr, "error creating logger: %v\n", err)
os.Exit(1)
}
level.Info(logger).
Log("msg", "starting opnsense-exporter", "version", version)
runtime.GOMAXPROCS(*options.MaxProcs)
level.Debug(logger).
Log("msg", "settings Go MAXPROCS", "procs", runtime.GOMAXPROCS(0))
opnsConfig, err := options.OPNSense()
if err != nil {
level.Error(logger).
Log("msg", "failed to assemble OPNsense configuration", "err", err)
os.Exit(1)
}
opnsenseClient, err := opnsense.NewClient(
*opnsConfig,
version,
logger,
)
if err != nil {
level.Error(logger).
Log("msg", "opnsense client build failed", "err", err)
os.Exit(1)
}
level.Debug(logger).Log(
"msg", fmt.Sprintf("OPNsense registered endpoints %s", opnsenseClient.Endpoints()),
)
registry := prometheus.NewRegistry()
if !*options.DisableExporterMetrics {
registry.MustRegister(
promcollectors.NewProcessCollector(promcollectors.ProcessCollectorOpts{}),
)
registry.MustRegister(promcollectors.NewGoCollector())
}
collectorsSwitches := options.CollectorsSwitches()
collectorOptionFuncs := []collector.Option{}
if !collectorsSwitches.Unbound {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutUnboundCollector())
level.Info(logger).Log("msg", "unbound collector disabled")
}
if !collectorsSwitches.Wireguard {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutWireguardCollector())
level.Info(logger).Log("msg", "wireguard collector disabled")
}
if !collectorsSwitches.Cron {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutCronCollector())
level.Info(logger).Log("msg", "cron collector disabled")
}
if !collectorsSwitches.ARP {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutArpTableCollector())
level.Info(logger).Log("msg", "arp collector disabled")
}
if !collectorsSwitches.Firewall {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutFirewallCollector())
level.Info(logger).Log("msg", "firewall collector disabled")
}
if !collectorsSwitches.Firmware {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutFirmwareCollector())
level.Info(logger).Log("msg", "firmware collector disabled")
}
if !collectorsSwitches.OpenVPN {
collectorOptionFuncs = append(collectorOptionFuncs, collector.WithoutOpenVPNCollector())
level.Info(logger).Log("msg", "openvpn collector disabled")
}
collectorInstance, err := collector.New(&opnsenseClient, logger, *options.InstanceLabel, collectorOptionFuncs...)
if err != nil {
level.Error(logger).
Log("msg", "failed to construct the collecotr", "err", err)
os.Exit(1)
}
registry.MustRegister(collectorInstance)
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
http.Handle(*options.MetricsPath, handler)
if *options.MetricsPath != "/" && *options.MetricsPath != "" {
landingConfig := web.LandingConfig{
Name: "OPNsense Exporter",
Description: "Prometheus OPNsense Firewall Exporter",
Version: version,
Links: []web.LandingLinks{
{
Address: *options.MetricsPath,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
}
term := make(chan os.Signal, 1)
srvClose := make(chan struct{})
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
srv := &http.Server{}
go func() {
if err := web.ListenAndServe(srv, options.WebConfig, logger); err != nil {
level.Error(logger).
Log("msg", "Error received from the HTTP server", "err", err)
close(srvClose)
}
}()
for {
select {
case <-term:
level.Info(logger).
Log("msg", "Received SIGTERM, exiting gracefully...")
os.Exit(0)
case <-srvClose:
os.Exit(1)
}
}
}