netmaker/logic/metrics/metrics.go

91 lines
2.7 KiB
Go
Raw Normal View History

2022-09-14 03:25:56 +08:00
package metrics
import (
2022-09-21 19:32:32 +08:00
"time"
2022-09-14 03:25:56 +08:00
"github.com/gravitl/netmaker/logger"
2023-01-28 03:51:06 +08:00
proxy_metrics "github.com/gravitl/netmaker/metrics"
2022-09-14 03:25:56 +08:00
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl"
)
// Collect - collects metrics
2023-03-14 11:15:03 +08:00
func Collect(iface, server, network string, peerMap models.PeerMap, proxy bool) (*models.Metrics, error) {
2022-09-14 03:25:56 +08:00
var metrics models.Metrics
metrics.Connectivity = make(map[string]models.Metric)
var wgclient, err = wgctrl.New()
if err != nil {
fillUnconnectedData(&metrics, peerMap)
return &metrics, err
}
defer wgclient.Close()
device, err := wgclient.Device(iface)
if err != nil {
fillUnconnectedData(&metrics, peerMap)
return &metrics, err
}
// TODO handle freebsd??
for i := range device.Peers {
currPeer := device.Peers[i]
2023-01-12 00:27:02 +08:00
if _, ok := peerMap[currPeer.PublicKey.String()]; !ok {
continue
}
2022-09-14 03:25:56 +08:00
id := peerMap[currPeer.PublicKey.String()].ID
address := peerMap[currPeer.PublicKey.String()].Address
if id == "" || address == "" {
logger.Log(0, "attempted to parse metrics for invalid peer from server", id, address)
continue
}
2023-01-12 00:27:02 +08:00
proxyMetrics := proxy_metrics.GetMetric(server, currPeer.PublicKey.String())
2022-09-14 03:25:56 +08:00
var newMetric = models.Metric{
NodeName: peerMap[currPeer.PublicKey.String()].Name,
}
logger.Log(2, "collecting metrics for peer", address)
2023-01-11 13:31:22 +08:00
newMetric.TotalReceived = int64(proxyMetrics.TrafficRecieved)
newMetric.TotalSent = int64(proxyMetrics.TrafficSent)
newMetric.Latency = int64(proxyMetrics.LastRecordedLatency)
newMetric.Connected = proxyMetrics.NodeConnectionStatus[id]
2023-03-14 11:15:03 +08:00
newMetric.CollectedByProxy = proxy
2023-01-12 23:28:29 +08:00
if newMetric.Connected {
newMetric.Uptime = 1
2022-09-14 03:25:56 +08:00
}
// check device peer to see if WG is working if ping failed
if !newMetric.Connected {
if currPeer.ReceiveBytes > 0 &&
currPeer.TransmitBytes > 0 &&
time.Now().Before(currPeer.LastHandshakeTime.Add(time.Minute<<1)) {
2022-09-14 03:25:56 +08:00
newMetric.Connected = true
newMetric.Uptime = 1
2022-09-14 03:25:56 +08:00
}
}
newMetric.TotalTime = 1
metrics.Connectivity[id] = newMetric
2023-01-11 13:31:22 +08:00
if len(proxyMetrics.NodeConnectionStatus) == 1 {
2023-01-12 00:27:02 +08:00
proxy_metrics.ResetMetricsForPeer(server, currPeer.PublicKey.String())
2023-01-11 13:31:22 +08:00
} else {
2023-01-12 00:27:02 +08:00
proxy_metrics.ResetMetricForNode(server, currPeer.PublicKey.String(), id)
2023-01-11 13:31:22 +08:00
}
2022-09-14 03:25:56 +08:00
}
fillUnconnectedData(&metrics, peerMap)
return &metrics, nil
}
// == used to fill zero value data for non connected peers ==
func fillUnconnectedData(metrics *models.Metrics, peerMap models.PeerMap) {
for r := range peerMap {
id := peerMap[r].ID
if !metrics.Connectivity[id].Connected {
newMetric := models.Metric{
NodeName: peerMap[r].Name,
Uptime: 0,
TotalTime: 1,
Connected: false,
Latency: 999,
PercentUp: 0,
}
metrics.Connectivity[id] = newMetric
}
}
}