forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
373 lines (317 loc) · 10.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// SPDX-License-Identifier: MIT
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/digineo/go-ping"
mon "github.com/digineo/go-ping/monitor"
"github.com/czerwonk/ping_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)
const version string = "1.1.0"
var (
showVersion = kingpin.Flag("version", "Print version information").Default().Bool()
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface").Default(":9427").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics").Default("/metrics").String()
serverUseTLS = kingpin.Flag("web.tls.enabled", "Enable TLS for web server, default is false").Default().Bool()
serverTlsCertFile = kingpin.Flag("web.tls.cert-file", "The certificate file for the web server").Default("").String()
serverTlsKeyFile = kingpin.Flag("web.tls.key-file", "The key file for the web server").Default("").String()
serverMutualAuthEnabled = kingpin.Flag("web.tls.mutual-auth-enabled", "Enable TLS client mutual authentication, default is false").Default().Bool()
serverTlsCAFile = kingpin.Flag("web.tls.ca-file", "The certificate authority file for client's certificate verification").Default("").String()
configFile = kingpin.Flag("config.path", "Path to config file").Default("").String()
pingInterval = kingpin.Flag("ping.interval", "Interval for ICMP echo requests").Default("5s").Duration()
pingTimeout = kingpin.Flag("ping.timeout", "Timeout for ICMP echo request").Default("4s").Duration()
pingSize = kingpin.Flag("ping.size", "Payload size for ICMP echo requests").Default("56").Uint16()
historySize = kingpin.Flag("ping.history-size", "Number of results to remember per target").Default("10").Int()
dnsRefresh = kingpin.Flag("dns.refresh", "Interval for refreshing DNS records and updating targets accordingly (0 if disabled)").Default("1m").Duration()
dnsNameServer = kingpin.Flag("dns.nameserver", "DNS server used to resolve hostname of targets").Default("").String()
disableIPv6 = kingpin.Flag("options.disable-ipv6", "Disable DNS from resolving IPv6 AAAA records").Default().Bool()
disableIPv4 = kingpin.Flag("options.disable-ipv4", "Disable DNS from resolving IPv4 A records").Default().Bool()
logLevel = kingpin.Flag("log.level", "Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]").Default("info").String()
targets = kingpin.Arg("targets", "A list of targets to ping").Strings()
tailnet = kingpin.Flag("ts.tailnet", "tailnet name").String()
)
var (
enableDeprecatedMetrics = true // default may change in future
deprecatedMetrics = kingpin.Flag("metrics.deprecated", "Enable or disable deprecated metrics (`ping_rtt_ms{type=best|worst|mean|std_dev}`). Valid choices: [enable, disable]").Default("disable").String()
rttMetricsScale = rttInMills // might change in future
rttMode = kingpin.Flag("metrics.rttunit", "Export ping results as either seconds (default), or milliseconds (deprecated), or both (for migrations). Valid choices: [s, ms, both]").Default("s").String()
)
func main() {
kingpin.Parse()
if len(*tailnet) > 0 {
tsDiscover()
}
if *showVersion {
printVersion()
os.Exit(0)
}
setLogLevel(*logLevel)
log.SetReportCaller(true)
switch *deprecatedMetrics {
case "enable":
enableDeprecatedMetrics = true
case "disable":
enableDeprecatedMetrics = false
default:
kingpin.FatalUsage("metrics.deprecated must be `enable` or `disable`")
}
if rttMetricsScale = rttUnitFromString(*rttMode); rttMetricsScale == rttInvalid {
kingpin.FatalUsage("metrics.rttunit must be `ms` for millis, or `s` for seconds, or `both`")
}
log.Infof("rtt units: %#v", rttMetricsScale)
if mpath := *metricsPath; mpath == "" {
log.Warnln("web.telemetry-path is empty, correcting to `/metrics`")
mpath = "/metrics"
metricsPath = &mpath
} else if mpath[0] != '/' {
mpath = "/" + mpath
metricsPath = &mpath
}
cfg, err := loadConfig()
if err != nil {
kingpin.FatalUsage("could not load config.path: %v", err)
}
if cfg.Ping.History < 1 {
kingpin.FatalUsage("ping.history-size must be greater than 0")
}
if cfg.Ping.Size > 65500 {
kingpin.FatalUsage("ping.size must be between 0 and 65500")
}
if len(cfg.Targets) == 0 {
kingpin.FatalUsage("No targets specified")
}
m, err := startMonitor(cfg)
if err != nil {
log.Errorln(err)
os.Exit(2)
}
startServer(cfg, m)
}
func printVersion() {
fmt.Println("ping-exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): Philip Berndroth, Daniel Czerwonk")
fmt.Println("Metric exporter for go-icmp")
}
func startMonitor(cfg *config.Config) (*mon.Monitor, error) {
resolver := setupResolver(cfg)
var bind4, bind6 string
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
// ipv4 enabled
ln.Close()
bind4 = "0.0.0.0"
}
if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
// ipv6 enabled
ln.Close()
bind6 = "::"
}
pinger, err := ping.New(bind4, bind6)
if err != nil {
return nil, fmt.Errorf("cannot start monitoring: %w", err)
}
if pinger.PayloadSize() != cfg.Ping.Size {
pinger.SetPayloadSize(cfg.Ping.Size)
}
monitor := mon.New(pinger,
cfg.Ping.Interval.Duration(),
cfg.Ping.Timeout.Duration())
monitor.HistorySize = cfg.Ping.History
targets := make([]*target, len(cfg.Targets))
for i, t := range cfg.Targets {
t := &target{
host: t.Addr,
addresses: make([]net.IPAddr, 0),
delay: time.Duration(10*i) * time.Millisecond,
resolver: resolver,
}
targets[i] = t
err := t.addOrUpdateMonitor(monitor, targetOpts{
disableIPv4: cfg.Options.DisableIPv4,
disableIPv6: cfg.Options.DisableIPv6,
})
if err != nil {
log.Errorln(err)
}
}
go startDNSAutoRefresh(cfg.DNS.Refresh.Duration(), targets, monitor, cfg)
return monitor, nil
}
func startDNSAutoRefresh(interval time.Duration, targets []*target, monitor *mon.Monitor, cfg *config.Config) {
if interval <= 0 {
return
}
for range time.NewTicker(interval).C {
refreshDNS(targets, monitor, cfg)
}
}
func refreshDNS(targets []*target, monitor *mon.Monitor, cfg *config.Config) {
log.Infoln("refreshing DNS")
for _, t := range targets {
go func(ta *target) {
err := ta.addOrUpdateMonitor(monitor, targetOpts{
disableIPv4: cfg.Options.DisableIPv4,
disableIPv6: cfg.Options.DisableIPv6,
})
if err != nil {
log.Errorf("could not refresh dns: %v", err)
}
}(t)
}
}
func startServer(cfg *config.Config, monitor *mon.Monitor) {
var err error
log.Infof("Starting ping exporter (Version: %s)", version)
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, indexHTML, *metricsPath)
})
reg := prometheus.NewRegistry()
reg.MustRegister(&pingCollector{
cfg: cfg,
monitor: monitor,
customLabels: newCustomLabelSet(cfg.Targets),
})
l := log.New()
l.Level = log.ErrorLevel
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: l,
ErrorHandling: promhttp.ContinueOnError,
})
http.Handle(*metricsPath, h)
server := http.Server{
Addr: *listenAddress,
}
if *serverUseTLS {
confureTLS(&server)
log.Infof("Listening for %s on %s (HTTPS)", *metricsPath, *listenAddress)
err = server.ListenAndServeTLS("", "")
} else {
log.Infof("Listening for %s on %s (HTTP)", *metricsPath, *listenAddress)
err = server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}
func confureTLS(server *http.Server) {
if *serverTlsCertFile == "" || *serverTlsKeyFile == "" {
log.Error("'web.tls.cert-file' and 'web.tls.key-file' must be defined")
return
}
server.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
var err error
server.TLSConfig.Certificates = make([]tls.Certificate, 1)
server.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(*serverTlsCertFile, *serverTlsKeyFile)
if err != nil {
log.Errorf("Loading certificates error: %v", err)
return
}
if *serverMutualAuthEnabled {
server.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
if *serverTlsCAFile != "" {
var ca []byte
if ca, err = os.ReadFile(*serverTlsCAFile); err != nil {
log.Errorf("Loading CA error: %v", err)
return
} else {
server.TLSConfig.ClientCAs = x509.NewCertPool()
server.TLSConfig.ClientCAs.AppendCertsFromPEM(ca)
}
}
} else {
server.TLSConfig.ClientAuth = tls.NoClientCert
}
}
func loadConfig() (*config.Config, error) {
if *configFile == "" {
cfg := config.Config{}
addFlagToConfig(&cfg)
return &cfg, nil
}
f, err := os.Open(*configFile)
if err != nil {
return nil, fmt.Errorf("cannot load config file: %w", err)
}
defer f.Close()
cfg, err := config.FromYAML(f)
if err == nil {
addFlagToConfig(cfg)
}
return cfg, err
}
func setupResolver(cfg *config.Config) *net.Resolver {
if cfg.DNS.Nameserver == "" {
return net.DefaultResolver
}
if !strings.HasSuffix(cfg.DNS.Nameserver, ":53") {
cfg.DNS.Nameserver += ":53"
}
dialer := func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", cfg.DNS.Nameserver)
}
return &net.Resolver{PreferGo: true, Dial: dialer}
}
// addFlagToConfig updates cfg with command line flag values, unless the
// config has non-zero values.
func addFlagToConfig(cfg *config.Config) {
if len(cfg.Targets) == 0 {
cfg.Targets = make([]config.TargetConfig, len(*targets))
for i, t := range *targets {
cfg.Targets[i] = config.TargetConfig{
Addr: t,
}
}
}
if cfg.Ping.History == 0 {
cfg.Ping.History = *historySize
}
if cfg.Ping.Interval == 0 {
cfg.Ping.Interval.Set(*pingInterval)
}
if cfg.Ping.Timeout == 0 {
cfg.Ping.Timeout.Set(*pingTimeout)
}
if cfg.Ping.Size == 0 {
cfg.Ping.Size = *pingSize
}
if cfg.DNS.Refresh == 0 {
cfg.DNS.Refresh.Set(*dnsRefresh)
}
if cfg.DNS.Nameserver == "" {
cfg.DNS.Nameserver = *dnsNameServer
}
if !cfg.Options.DisableIPv6 {
cfg.Options.DisableIPv6 = *disableIPv6
}
if !cfg.Options.DisableIPv4 {
cfg.Options.DisableIPv4 = *disableIPv4
}
}
const indexHTML = `<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>ping Exporter (Version ` + version + `)</title>
</head>
<body>
<h1>ping Exporter</h1>
<p><a href="%s">Metrics</a></p>
<h2>More information:</h2>
<p><a href="https://github.com/czerwonk/ping_exporter">github.com/czerwonk/ping_exporter</a></p>
</body>
</html>
`