diff options
Diffstat (limited to 'collector/wireguard_linux.go')
-rw-r--r-- | collector/wireguard_linux.go | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/collector/wireguard_linux.go b/collector/wireguard_linux.go new file mode 100644 index 0000000..bf4f195 --- /dev/null +++ b/collector/wireguard_linux.go | |||
@@ -0,0 +1,102 @@ | |||
1 | // Copyright 2015 The Prometheus Authors | ||
2 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | // you may not use this file except in compliance with the License. | ||
4 | // You may obtain a copy of the License at | ||
5 | // | ||
6 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | // | ||
8 | // Unless required by applicable law or agreed to in writing, software | ||
9 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | // See the License for the specific language governing permissions and | ||
12 | // limitations under the License. | ||
13 | |||
14 | // +build !nowireguard | ||
15 | // +build linux freebsd openbsd dragonfly darwin | ||
16 | |||
17 | package collector | ||
18 | |||
19 | import ( | ||
20 | "strconv" | ||
21 | "time" | ||
22 | |||
23 | "github.com/go-kit/kit/log" | ||
24 | "github.com/prometheus/client_golang/prometheus" | ||
25 | "golang.zx2c4.com/wireguard/wgctrl" | ||
26 | ) | ||
27 | |||
28 | func init() { | ||
29 | registerCollector("wireguard", defaultDisabled, NewWireguardCollector) | ||
30 | } | ||
31 | |||
32 | func NewWireguardCollector(logger log.Logger) (Collector, error) { | ||
33 | subsystem := "wireguard" | ||
34 | labels := []string{"name", "peer_key", "local_key", "endpoint", "version"} | ||
35 | |||
36 | wg, err := wgctrl.New() | ||
37 | if err != nil { | ||
38 | return nil, err | ||
39 | } | ||
40 | |||
41 | return &wireguardCollector{ | ||
42 | client: wg, | ||
43 | lastHandshakeSeconds: prometheus.NewDesc( | ||
44 | prometheus.BuildFQName(namespace, subsystem, "last_handshake_secs"), | ||
45 | "Wireguard last handshake seconds ago", | ||
46 | labels, nil, | ||
47 | ), | ||
48 | bytesIn: prometheus.NewDesc( | ||
49 | prometheus.BuildFQName(namespace, subsystem, "bytes_in"), | ||
50 | "Wireguard bytes in for connection", | ||
51 | labels, nil, | ||
52 | ), | ||
53 | bytesOut: prometheus.NewDesc( | ||
54 | prometheus.BuildFQName(namespace, subsystem, "bytes_out"), | ||
55 | "Wireguard bytes out for connection", | ||
56 | labels, nil, | ||
57 | ), | ||
58 | }, nil | ||
59 | } | ||
60 | |||
61 | type wireguardCollector struct { | ||
62 | client *wgctrl.Client | ||
63 | lastHandshakeSeconds *prometheus.Desc | ||
64 | bytesIn, bytesOut *prometheus.Desc | ||
65 | } | ||
66 | |||
67 | func (c *wireguardCollector) Update(ch chan<- prometheus.Metric) error { | ||
68 | devs, err := c.client.Devices() | ||
69 | if err != nil { | ||
70 | return err | ||
71 | } | ||
72 | |||
73 | for _, dev := range devs { | ||
74 | for _, peer := range dev.Peers { | ||
75 | labels := []string{ | ||
76 | dev.Name, | ||
77 | peer.PublicKey.String(), | ||
78 | dev.PublicKey.String(), | ||
79 | peer.Endpoint.String(), | ||
80 | strconv.Itoa(peer.ProtocolVersion), | ||
81 | } | ||
82 | |||
83 | ch <- prometheus.MustNewConstMetric( | ||
84 | c.bytesIn, prometheus.CounterValue, | ||
85 | float64(peer.ReceiveBytes), | ||
86 | labels..., | ||
87 | ) | ||
88 | ch <- prometheus.MustNewConstMetric( | ||
89 | c.bytesOut, prometheus.CounterValue, | ||
90 | float64(peer.TransmitBytes), | ||
91 | labels..., | ||
92 | ) | ||
93 | ch <- prometheus.MustNewConstMetric( | ||
94 | c.lastHandshakeSeconds, prometheus.GaugeValue, | ||
95 | float64(time.Now().Sub(peer.LastHandshakeTime).Seconds()), | ||
96 | labels..., | ||
97 | ) | ||
98 | } | ||
99 | } | ||
100 | |||
101 | return nil | ||
102 | } | ||