-
Notifications
You must be signed in to change notification settings - Fork 6
/
fastd-exporter.go
399 lines (323 loc) · 15.3 KB
/
fastd-exporter.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"regexp"
"strconv"
"time"
"strings"
"github.com/ammario/ipisp/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/simplesurance/go-ip-anonymizer/ipanonymizer"
)
var (
configPathPattern = flag.String("config-path", "/etc/fastd/%s/fastd.conf", "Override fastd config path, %s will be replaced with the fastd instance name.")
webListenAddress = flag.String("web.listen-address", ":9281", "Address on which to expose metrics and web interface.")
webMetricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
ipAsnLookupEnable = flag.Bool("ip-asn-lookup.enable", true, "enable usage of ip->asn lookup")
ipAsnLookupTimeout = flag.Int("ip-asn-lookup.timeout", 300, "milliseconds to wait for ip->asn lookup to finish")
)
// PacketStatistics These are the structs necessary for unmarshalling the data that is being received on fastds unix socket.
type PacketStatistics struct {
Count int `json:"packets"`
Bytes int `json:"bytes"`
}
type Statistics struct {
Rx PacketStatistics `json:"rx"`
RxReordered PacketStatistics `json:"rx_reordered"`
Tx PacketStatistics `json:"tx"`
TxDropped PacketStatistics `json:"tx_dropped"`
TxError PacketStatistics `json:"tx_error"`
}
type Message struct {
Uptime float64 `json:"uptime"`
Interface string `json:"interface"`
Statistics Statistics `json:"statistics"`
Peers map[string]Peer `json:"peers"`
}
type Peer struct {
Name string `json:"name"`
Address string `json:"address"`
Interface string `json:"interface"`
Connection *struct {
Established float64 `json:"established"`
Method string `json:"method"`
Statistics Statistics `json:"statistics"`
} `json:"connection"`
MAC []string `json:"mac_addresses"`
}
type PrometheusExporter struct {
statusSocketPath string
up *prometheus.Desc
uptime *prometheus.Desc
rxPackets *prometheus.Desc
rxBytes *prometheus.Desc
rxReorderedPackets *prometheus.Desc
rxReorderedBytes *prometheus.Desc
txPackets *prometheus.Desc
txBytes *prometheus.Desc
txDroppedPackets *prometheus.Desc
txDroppedBytes *prometheus.Desc
txErrorPackets *prometheus.Desc
txErrorBytes *prometheus.Desc
peersUpTotal *prometheus.Desc
peerUp *prometheus.Desc
peerUptime *prometheus.Desc
peerInfo *prometheus.Desc
peerRxPackets *prometheus.Desc
peerRxBytes *prometheus.Desc
peerRxReorderedPackets *prometheus.Desc
peerRxReorderedBytes *prometheus.Desc
peerTxPackets *prometheus.Desc
peerTxBytes *prometheus.Desc
peerTxDroppedPackets *prometheus.Desc
peerTxDroppedBytes *prometheus.Desc
peerTxErrorPackets *prometheus.Desc
peerTxErrorBytes *prometheus.Desc
}
func prefixWrapper(parts ...string) string {
parts = append([]string{"fastd"}, parts...)
return strings.Join(parts, "_")
}
func NewPrometheusExporter(instance string, sockName string) PrometheusExporter {
staticLabels := prometheus.Labels{
"fastd_instance": instance,
}
dynamicLabels := []string{
"public_key",
"name",
"interface",
}
dynamicPeerInfoLabels := append(dynamicLabels, []string{
"method",
"asn",
"ipaddr_family",
}...)
return PrometheusExporter{
statusSocketPath: sockName,
// global metrics
up: prometheus.NewDesc(prefixWrapper("up"), "whether the fastd process is up", nil, staticLabels),
uptime: prometheus.NewDesc(prefixWrapper("uptime_seconds"), "uptime of the fastd process", nil, staticLabels),
rxPackets: prometheus.NewDesc(prefixWrapper("rx_packets"), "rx packet count", nil, staticLabels),
rxBytes: prometheus.NewDesc(prefixWrapper("rx_bytes"), "rx byte count", nil, staticLabels),
rxReorderedPackets: prometheus.NewDesc(prefixWrapper("rx_reordered_packets"), "rx reordered packets count", nil, staticLabels),
rxReorderedBytes: prometheus.NewDesc(prefixWrapper("rx_reordered_bytes"), "rx reordered bytes count", nil, staticLabels),
txPackets: prometheus.NewDesc(prefixWrapper("tx_packets"), "tx packet count", nil, staticLabels),
txBytes: prometheus.NewDesc(prefixWrapper("tx_bytes"), "tx byte count", nil, staticLabels),
txDroppedPackets: prometheus.NewDesc(prefixWrapper("tx_dropped_packets"), "tx dropped packets count", nil, staticLabels),
txDroppedBytes: prometheus.NewDesc(prefixWrapper("tx_dropped_bytes"), "tx dropped bytes count", nil, staticLabels),
txErrorPackets: prometheus.NewDesc(prefixWrapper("tx_error_packets"), "tx error packets count", nil, staticLabels),
txErrorBytes: prometheus.NewDesc(prefixWrapper("tx_error_bytes"), "tx error bytes count", nil, staticLabels),
peersUpTotal: prometheus.NewDesc(prefixWrapper("peers_up_total"), "number of connected peers", nil, staticLabels),
// per peer metrics
peerUp: prometheus.NewDesc(prefixWrapper("peer_up"), "whether the peer is connected", dynamicLabels, staticLabels),
peerUptime: prometheus.NewDesc(prefixWrapper("peer_uptime_seconds"), "peer session uptime", dynamicLabels, staticLabels),
peerInfo: prometheus.NewDesc(prefixWrapper("peer_info"), "general info about a peer (connection method, ASN, IP Version)", dynamicPeerInfoLabels, staticLabels),
peerRxPackets: prometheus.NewDesc(prefixWrapper("peer_rx_packets"), "peer rx packets count", dynamicLabels, staticLabels),
peerRxBytes: prometheus.NewDesc(prefixWrapper("peer_rx_bytes"), "peer rx bytes count", dynamicLabels, staticLabels),
peerRxReorderedPackets: prometheus.NewDesc(prefixWrapper("peer_rx_reordered_packets"), "peer rx reordered packets count", dynamicLabels, staticLabels),
peerRxReorderedBytes: prometheus.NewDesc(prefixWrapper("peer_rx_reordered_bytes"), "peer rx reordered bytes count", dynamicLabels, staticLabels),
peerTxPackets: prometheus.NewDesc(prefixWrapper("peer_tx_packets"), "peer rx packet count", dynamicLabels, staticLabels),
peerTxBytes: prometheus.NewDesc(prefixWrapper("peer_tx_bytes"), "peer rx bytes count", dynamicLabels, staticLabels),
peerTxDroppedPackets: prometheus.NewDesc(prefixWrapper("peer_tx_dropped_packets"), "peer tx dropped packets count", dynamicLabels, staticLabels),
peerTxDroppedBytes: prometheus.NewDesc(prefixWrapper("peer_tx_dropped_bytes"), "peer tx dropped bytes count", dynamicLabels, staticLabels),
peerTxErrorPackets: prometheus.NewDesc(prefixWrapper("peer_tx_error_packets"), "peer tx error packets count", dynamicLabels, staticLabels),
peerTxErrorBytes: prometheus.NewDesc(prefixWrapper("peer_tx_error_bytes"), "peer tx error bytes count", dynamicLabels, staticLabels),
}
}
func (exporter PrometheusExporter) Describe(channel chan<- *prometheus.Desc) {
channel <- exporter.up
channel <- exporter.uptime
channel <- exporter.rxPackets
channel <- exporter.rxBytes
channel <- exporter.rxReorderedPackets
channel <- exporter.rxReorderedBytes
channel <- exporter.txPackets
channel <- exporter.txBytes
channel <- exporter.txDroppedPackets
channel <- exporter.txDroppedBytes
channel <- exporter.peersUpTotal
channel <- exporter.peerUp
channel <- exporter.peerUptime
channel <- exporter.peerInfo
channel <- exporter.peerRxPackets
channel <- exporter.peerRxBytes
channel <- exporter.peerRxReorderedPackets
channel <- exporter.peerRxReorderedBytes
channel <- exporter.peerTxPackets
channel <- exporter.peerTxBytes
channel <- exporter.peerTxDroppedPackets
channel <- exporter.peerTxDroppedBytes
channel <- exporter.peerTxErrorPackets
channel <- exporter.peerTxErrorBytes
}
func (exporter PrometheusExporter) Collect(channel chan<- prometheus.Metric) {
data, err := readFromStatusSocket(exporter.statusSocketPath)
if err != nil {
log.Print(err)
channel <- prometheus.MustNewConstMetric(exporter.up, prometheus.GaugeValue, 0)
} else {
channel <- prometheus.MustNewConstMetric(exporter.up, prometheus.GaugeValue, 1)
}
channel <- prometheus.MustNewConstMetric(exporter.uptime, prometheus.GaugeValue, data.Uptime/1000)
channel <- prometheus.MustNewConstMetric(exporter.rxPackets, prometheus.CounterValue, float64(data.Statistics.Rx.Count))
channel <- prometheus.MustNewConstMetric(exporter.rxBytes, prometheus.CounterValue, float64(data.Statistics.Rx.Bytes))
channel <- prometheus.MustNewConstMetric(exporter.rxReorderedPackets, prometheus.CounterValue, float64(data.Statistics.RxReordered.Count))
channel <- prometheus.MustNewConstMetric(exporter.rxReorderedBytes, prometheus.CounterValue, float64(data.Statistics.RxReordered.Bytes))
channel <- prometheus.MustNewConstMetric(exporter.txPackets, prometheus.CounterValue, float64(data.Statistics.Tx.Count))
channel <- prometheus.MustNewConstMetric(exporter.txBytes, prometheus.CounterValue, float64(data.Statistics.Tx.Bytes))
channel <- prometheus.MustNewConstMetric(exporter.txDroppedPackets, prometheus.CounterValue, float64(data.Statistics.Tx.Count))
channel <- prometheus.MustNewConstMetric(exporter.txDroppedBytes, prometheus.CounterValue, float64(data.Statistics.TxDropped.Bytes))
peersUpTotal := 0
anonymize := ipanonymizer.NewWithMask(
net.CIDRMask(24, 32),
net.CIDRMask(48, 128),
)
for publicKey, peer := range data.Peers {
peerName := peer.Name
interfaceName := data.Interface
method := ""
ipAddrFamily := "IPv6"
if interfaceName == "" {
interfaceName = peer.Interface
}
if peer.Connection == nil {
channel <- prometheus.MustNewConstMetric(exporter.peerUp, prometheus.GaugeValue, float64(0), publicKey, peerName, interfaceName)
} else {
peersUpTotal += 1
method = peer.Connection.Method
peerIp, _, _ := net.SplitHostPort(peer.Address)
if strings.Contains(peerIp, ".") {
ipAddrFamily = "IPv4"
}
peerAsn := ""
if *ipAsnLookupEnable {
anonIP, err := anonymize.IPString(peerIp)
if err == nil {
peerIp = anonIP
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*ipAsnLookupTimeout)*time.Millisecond)
defer cancel()
asnlookup, err := ipisp.LookupIP(ctx, net.ParseIP(peerIp))
if err != nil {
log.Print(err)
} else {
peerAsn = strconv.Itoa(int(asnlookup.ASN))
}
}
channel <- prometheus.MustNewConstMetric(exporter.peerUp, prometheus.GaugeValue, float64(1), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerUptime, prometheus.GaugeValue, peer.Connection.Established/1000, publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerInfo, prometheus.GaugeValue, float64(1), publicKey, peerName, interfaceName, method, peerAsn, ipAddrFamily)
channel <- prometheus.MustNewConstMetric(exporter.peerRxPackets, prometheus.CounterValue, float64(peer.Connection.Statistics.Rx.Count), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerRxBytes, prometheus.CounterValue, float64(peer.Connection.Statistics.Rx.Bytes), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerRxReorderedPackets, prometheus.CounterValue, float64(peer.Connection.Statistics.RxReordered.Count), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerRxReorderedBytes, prometheus.CounterValue, float64(peer.Connection.Statistics.RxReordered.Bytes), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxPackets, prometheus.CounterValue, float64(peer.Connection.Statistics.Tx.Count), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxBytes, prometheus.CounterValue, float64(peer.Connection.Statistics.Tx.Bytes), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxDroppedPackets, prometheus.CounterValue, float64(peer.Connection.Statistics.TxDropped.Count), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxDroppedBytes, prometheus.CounterValue, float64(peer.Connection.Statistics.TxDropped.Bytes), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxErrorPackets, prometheus.CounterValue, float64(peer.Connection.Statistics.TxError.Count), publicKey, peerName, interfaceName)
channel <- prometheus.MustNewConstMetric(exporter.peerTxErrorBytes, prometheus.CounterValue, float64(peer.Connection.Statistics.TxError.Bytes), publicKey, peerName, interfaceName)
}
}
channel <- prometheus.MustNewConstMetric(exporter.peersUpTotal, prometheus.GaugeValue, float64(peersUpTotal))
}
func readFromStatusSocket(sock string) (Message, error) {
conn, err := net.DialTimeout("unix", sock, 2*time.Second)
if err != nil {
return Message{}, err
}
defer func(conn net.Conn) {
_ = conn.Close()
}(conn)
decoder := json.NewDecoder(conn)
msg := Message{}
err = decoder.Decode(&msg)
if err != nil {
return Message{}, err
}
return msg, nil
}
type fastdConfig struct {
statusSocketPath string
}
func parseConfig(instance string) (fastdConfig, error) {
/*
* Parses a fastd configuration and extracts the status socket, where the exporter
* will pull metrics from.
*
* Returns statusSocketPath, err
* Errors when the configuration could not be read, no status socket is defined or the status socket does not exist
*/
data, err := ioutil.ReadFile(fmt.Sprintf(*configPathPattern, instance))
if err != nil {
return fastdConfig{}, err
}
statusSocketPattern := regexp.MustCompile("status socket \"([^\"]+)\";")
match := statusSocketPattern.FindSubmatch(data)
if len(match) == 0 {
return fastdConfig{}, errors.New(fmt.Sprintf("Instance %s is missing 'status socket' declaration.", instance))
}
statusSocketPath := string(match[1])
return checkSocket(statusSocketPath)
}
func checkSocket(statusSocketPath string) (fastdConfig, error) {
if _, err := os.Stat(statusSocketPath); err == nil {
return fastdConfig{statusSocketPath}, nil
} else {
return fastdConfig{}, errors.New(fmt.Sprintf("Status socket at %s does not exist. Is the fastd instance up?.", statusSocketPath))
}
}
func main() {
flag.Parse()
instances := flag.Args()
if len(instances) == 0 {
log.Fatal("No instances specified, aborting.")
}
instancePattern := regexp.MustCompile(`^([a-zA-Z0-9\._-]+)(=((/[a-zA-Z0-9\._-]+)+))?$`)
for i := 0; i < len(instances); i++ {
instance := instancePattern.FindStringSubmatch(instances[i])
var config fastdConfig
var err error
if instance == nil || len(instance) != 5 {
log.Fatalf("Invalid instance definition: %s", instances[i])
}
// check if there is an provided socket path
if instance[3] != "" {
// use provided socket path
config, err = checkSocket(instance[3])
} else {
// parse config to get socket path
config, err = parseConfig(instance[1])
}
if err != nil {
log.Fatal(err)
}
log.Printf("Reading fastd data for %v from %v", instance[1], config.statusSocketPath)
go prometheus.MustRegister(NewPrometheusExporter(instance[1], config.statusSocketPath))
}
// Expose the registered metrics via HTTP.
http.Handle(*webMetricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>fastd exporter</title></head>
<body>
<h1>fastd exporter</h1>
<p><a href="` + *webMetricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
return
}
})
log.Fatal(http.ListenAndServe(*webListenAddress, nil))
}