master
go 152 lines 3.55 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package wireguard
4
5 import (
6 "fmt"
7 "strings"
8
9 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
10 )
11
12 const (
13 prioDeviceNetworkIO = collectorapi.Priority + iota
14 prioDevicePeers
15 prioPeerNetworkIO
16 prioPeerLatestHandShake
17 )
18
19 var (
20 deviceChartsTmpl = collectorapi.Charts{
21 deviceNetworkIOChartTmpl.Copy(),
22 devicePeersChartTmpl.Copy(),
23 }
24
25 deviceNetworkIOChartTmpl = collectorapi.Chart{
26 ID: "device_%s_network_io",
27 Title: "Device traffic",
28 Units: "B/s",
29 Fam: "device traffic",
30 Ctx: "wireguard.device_network_io",
31 Type: collectorapi.Area,
32 Priority: prioDeviceNetworkIO,
33 Dims: collectorapi.Dims{
34 {ID: "device_%s_receive", Name: "receive", Algo: collectorapi.Incremental},
35 {ID: "device_%s_transmit", Name: "transmit", Algo: collectorapi.Incremental, Mul: -1},
36 },
37 }
38 devicePeersChartTmpl = collectorapi.Chart{
39 ID: "device_%s_peers",
40 Title: "Device peers",
41 Units: "peers",
42 Fam: "device peers",
43 Ctx: "wireguard.device_peers",
44 Priority: prioDevicePeers,
45 Dims: collectorapi.Dims{
46 {ID: "device_%s_peers", Name: "peers"},
47 },
48 }
49 )
50
51 var (
52 peerChartsTmpl = collectorapi.Charts{
53 peerNetworkIOChartTmpl.Copy(),
54 peerLatestHandShakeChartTmpl.Copy(),
55 }
56
57 peerNetworkIOChartTmpl = collectorapi.Chart{
58 ID: "peer_%s_network_io",
59 Title: "Peer traffic",
60 Units: "B/s",
61 Fam: "peer traffic",
62 Ctx: "wireguard.peer_network_io",
63 Type: collectorapi.Area,
64 Priority: prioPeerNetworkIO,
65 Dims: collectorapi.Dims{
66 {ID: "peer_%s_receive", Name: "receive", Algo: collectorapi.Incremental},
67 {ID: "peer_%s_transmit", Name: "transmit", Algo: collectorapi.Incremental, Mul: -1},
68 },
69 }
70 peerLatestHandShakeChartTmpl = collectorapi.Chart{
71 ID: "peer_%s_latest_handshake_ago",
72 Title: "Peer time elapsed since the latest handshake",
73 Units: "seconds",
74 Fam: "peer latest handshake",
75 Ctx: "wireguard.peer_latest_handshake_ago",
76 Priority: prioPeerLatestHandShake,
77 Dims: collectorapi.Dims{
78 {ID: "peer_%s_latest_handshake_ago", Name: "time"},
79 },
80 }
81 )
82
83 func newDeviceCharts(device string) *collectorapi.Charts {
84 charts := deviceChartsTmpl.Copy()
85
86 for _, c := range *charts {
87 c.ID = fmt.Sprintf(c.ID, device)
88 c.Labels = []collectorapi.Label{
89 {Key: "device", Value: device},
90 }
91 for _, d := range c.Dims {
92 d.ID = fmt.Sprintf(d.ID, device)
93 }
94 }
95
96 return charts
97 }
98
99 func (c *Collector) addNewDeviceCharts(device string) {
100 charts := newDeviceCharts(device)
101
102 if err := c.Charts().Add(*charts...); err != nil {
103 c.Warning(err)
104 }
105 }
106
107 func (c *Collector) removeDeviceCharts(device string) {
108 prefix := fmt.Sprintf("device_%s", device)
109
110 for _, c := range *c.Charts() {
111 if strings.HasPrefix(c.ID, prefix) {
112 c.MarkRemove()
113 c.MarkNotCreated()
114 }
115 }
116 }
117
118 func newPeerCharts(id, device, pubKey string) *collectorapi.Charts {
119 charts := peerChartsTmpl.Copy()
120
121 for _, c := range *charts {
122 c.ID = fmt.Sprintf(c.ID, id)
123 c.Labels = []collectorapi.Label{
124 {Key: "device", Value: device},
125 {Key: "public_key", Value: pubKey},
126 }
127 for _, d := range c.Dims {
128 d.ID = fmt.Sprintf(d.ID, id)
129 }
130 }
131
132 return charts
133 }
134
135 func (c *Collector) addNewPeerCharts(id, device, pubKey string) {
136 charts := newPeerCharts(id, device, pubKey)
137
138 if err := c.Charts().Add(*charts...); err != nil {
139 c.Warning(err)
140 }
141 }
142
143 func (c *Collector) removePeerCharts(id string) {
144 prefix := fmt.Sprintf("peer_%s", id)
145
146 for _, c := range *c.Charts() {
147 if strings.HasPrefix(c.ID, prefix) {
148 c.MarkRemove()
149 c.MarkNotCreated()
150 }
151 }
152 }