github.com/netdata/go.d.plugin@v0.58.1/modules/wireguard/charts.go (about)

     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/go.d.plugin/agent/module"
    10  )
    11  
    12  const (
    13  	prioDeviceNetworkIO = module.Priority + iota
    14  	prioDevicePeers
    15  	prioPeerNetworkIO
    16  	prioPeerLatestHandShake
    17  )
    18  
    19  var (
    20  	deviceChartsTmpl = module.Charts{
    21  		deviceNetworkIOChartTmpl.Copy(),
    22  		devicePeersChartTmpl.Copy(),
    23  	}
    24  
    25  	deviceNetworkIOChartTmpl = module.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:     module.Area,
    32  		Priority: prioDeviceNetworkIO,
    33  		Dims: module.Dims{
    34  			{ID: "device_%s_receive", Name: "receive", Algo: module.Incremental},
    35  			{ID: "device_%s_transmit", Name: "transmit", Algo: module.Incremental, Mul: -1},
    36  		},
    37  	}
    38  	devicePeersChartTmpl = module.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: module.Dims{
    46  			{ID: "device_%s_peers", Name: "peers"},
    47  		},
    48  	}
    49  )
    50  
    51  var (
    52  	peerChartsTmpl = module.Charts{
    53  		peerNetworkIOChartTmpl.Copy(),
    54  		peerLatestHandShakeChartTmpl.Copy(),
    55  	}
    56  
    57  	peerNetworkIOChartTmpl = module.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:     module.Area,
    64  		Priority: prioPeerNetworkIO,
    65  		Dims: module.Dims{
    66  			{ID: "peer_%s_receive", Name: "receive", Algo: module.Incremental},
    67  			{ID: "peer_%s_transmit", Name: "transmit", Algo: module.Incremental, Mul: -1},
    68  		},
    69  	}
    70  	peerLatestHandShakeChartTmpl = module.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: module.Dims{
    78  			{ID: "peer_%s_latest_handshake_ago", Name: "time"},
    79  		},
    80  	}
    81  )
    82  
    83  func newDeviceCharts(device string) *module.Charts {
    84  	charts := deviceChartsTmpl.Copy()
    85  
    86  	for _, c := range *charts {
    87  		c.ID = fmt.Sprintf(c.ID, device)
    88  		c.Labels = []module.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 (w *WireGuard) addNewDeviceCharts(device string) {
   100  	charts := newDeviceCharts(device)
   101  
   102  	if err := w.Charts().Add(*charts...); err != nil {
   103  		w.Warning(err)
   104  	}
   105  }
   106  
   107  func (w *WireGuard) removeDeviceCharts(device string) {
   108  	prefix := fmt.Sprintf("device_%s", device)
   109  
   110  	for _, c := range *w.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) *module.Charts {
   119  	charts := peerChartsTmpl.Copy()
   120  
   121  	for _, c := range *charts {
   122  		c.ID = fmt.Sprintf(c.ID, id)
   123  		c.Labels = []module.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 (w *WireGuard) addNewPeerCharts(id, device, pubKey string) {
   136  	charts := newPeerCharts(id, device, pubKey)
   137  
   138  	if err := w.Charts().Add(*charts...); err != nil {
   139  		w.Warning(err)
   140  	}
   141  }
   142  
   143  func (w *WireGuard) removePeerCharts(id string) {
   144  	prefix := fmt.Sprintf("peer_%s", id)
   145  
   146  	for _, c := range *w.Charts() {
   147  		if strings.HasPrefix(c.ID, prefix) {
   148  			c.MarkRemove()
   149  			c.MarkNotCreated()
   150  		}
   151  	}
   152  }