Browse Source

support node level metrics on host

Abhishek Kondur 2 years ago
parent
commit
009845a024
3 changed files with 74 additions and 42 deletions
  1. 56 1
      go.mod
  2. 12 32
      logic/metrics/metrics.go
  3. 6 9
      models/metrics.go

+ 56 - 1
go.mod

@@ -43,7 +43,7 @@ require (
 )
 
 require (
-	github.com/gravitl/netclient v0.0.0-20230110162232-fcba83b2f589
+	github.com/gravitl/netclient v0.0.0-20230111052236-026a8d6b2a76
 	github.com/guumaster/tablewriter v0.0.10
 	github.com/matryer/is v1.4.0
 	github.com/olekukonko/tablewriter v0.0.5
@@ -52,10 +52,65 @@ require (
 
 require (
 	cloud.google.com/go/compute/metadata v0.2.1 // indirect
+	github.com/Microsoft/go-winio v0.5.2 // indirect
+	github.com/bep/debounce v1.2.1 // indirect
+	github.com/cloverstd/tcping v0.1.1 // indirect
+	github.com/devilcove/httpclient v0.6.0 // indirect
+	github.com/docker/distribution v2.8.1+incompatible // indirect
+	github.com/docker/docker v20.10.17+incompatible // indirect
+	github.com/docker/go-connections v0.4.0 // indirect
+	github.com/docker/go-units v0.4.0 // indirect
+	github.com/fsnotify/fsnotify v1.5.4 // indirect
 	github.com/go-jose/go-jose/v3 v3.0.0 // indirect
+	github.com/go-ole/go-ole v1.2.6 // indirect
+	github.com/gogo/protobuf v1.3.2 // indirect
+	github.com/google/gopacket v1.1.19 // indirect
+	github.com/guumaster/hostctl v1.1.3 // indirect
+	github.com/hashicorp/hcl v1.0.0 // indirect
+	github.com/imdario/mergo v0.3.12 // indirect
 	github.com/inconshreveable/mousetrap v1.0.1 // indirect
+	github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
+	github.com/kr/pretty v0.3.1 // indirect
+	github.com/kr/text v0.2.0 // indirect
+	github.com/labstack/echo/v4 v4.9.0 // indirect
+	github.com/labstack/gommon v0.3.1 // indirect
+	github.com/leaanthony/go-ansi-parser v1.0.1 // indirect
+	github.com/leaanthony/gosod v1.0.3 // indirect
+	github.com/leaanthony/slicer v1.5.0 // indirect
+	github.com/magiconair/properties v1.8.6 // indirect
+	github.com/mattn/go-colorable v0.1.12 // indirect
+	github.com/mattn/go-isatty v0.0.14 // indirect
+	github.com/mitchellh/mapstructure v1.5.0 // indirect
+	github.com/opencontainers/go-digest v1.0.0 // indirect
+	github.com/opencontainers/image-spec v1.0.2 // indirect
+	github.com/pelletier/go-toml v1.9.5 // indirect
+	github.com/pelletier/go-toml/v2 v2.0.5 // indirect
+	github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 // indirect
 	github.com/rivo/uniseg v0.2.0 // indirect
+	github.com/rogpeppe/go-internal v1.9.0 // indirect
+	github.com/samber/lo v1.27.1 // indirect
+	github.com/spf13/afero v1.9.2 // indirect
+	github.com/spf13/cast v1.5.0 // indirect
+	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
+	github.com/spf13/viper v1.13.0 // indirect
+	github.com/subosito/gotenv v1.4.1 // indirect
+	github.com/tkrajina/go-reflector v0.5.5 // indirect
+	github.com/valyala/bytebufferpool v1.0.0 // indirect
+	github.com/valyala/fasttemplate v1.2.1 // indirect
+	github.com/vishvananda/netlink v1.1.0 // indirect
+	github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
+	github.com/wailsapp/mimetype v1.4.1 // indirect
+	github.com/wailsapp/wails/v2 v2.2.0 // indirect
+	golang.design/x/clipboard v0.6.3 // indirect
+	golang.org/x/exp/shiny v0.0.0-20221208044002-44028be4359e // indirect
+	golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
+	golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
+	golang.org/x/term v0.3.0 // indirect
+	golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
+	golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
+	gopkg.in/ini.v1 v1.67.0 // indirect
+	gopkg.in/yaml.v2 v2.4.0 // indirect
 )
 
 require (

+ 12 - 32
logic/metrics/metrics.go

@@ -3,9 +3,7 @@ package metrics
 import (
 	"time"
 
-	"github.com/go-ping/ping"
 	proxy_metrics "github.com/gravitl/netclient/nmproxy/metrics"
-	proxy_models "github.com/gravitl/netclient/nmproxy/models"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/models"
@@ -27,8 +25,6 @@ func Collect(iface, network string, proxy bool, peerMap models.PeerMap) (*models
 		fillUnconnectedData(&metrics, peerMap)
 		return &metrics, err
 	}
-	metrics.ProxyMetrics = make(map[string]proxy_models.Metric)
-
 	// TODO handle freebsd??
 	for i := range device.Peers {
 		currPeer := device.Peers[i]
@@ -38,38 +34,20 @@ func Collect(iface, network string, proxy bool, peerMap models.PeerMap) (*models
 			logger.Log(0, "attempted to parse metrics for invalid peer from server", id, address)
 			continue
 		}
+		proxyMetrics := proxy_metrics.GetMetric(currPeer.PublicKey.String())
 		var newMetric = models.Metric{
 			NodeName: peerMap[currPeer.PublicKey.String()].Name,
 			IsServer: peerMap[currPeer.PublicKey.String()].IsServer,
 		}
 		logger.Log(2, "collecting metrics for peer", address)
-		newMetric.TotalReceived = currPeer.ReceiveBytes
-		newMetric.TotalSent = currPeer.TransmitBytes
-
-		// get latency
-		pinger, err := ping.NewPinger(address)
-		if err != nil {
-			logger.Log(0, "could not initiliaze ping for metrics on peer address", address, err.Error())
-			newMetric.Connected = false
+		newMetric.TotalReceived = int64(proxyMetrics.TrafficRecieved)
+		newMetric.TotalSent = int64(proxyMetrics.TrafficSent)
+		newMetric.Latency = int64(proxyMetrics.LastRecordedLatency)
+		newMetric.Connected = proxyMetrics.NodeConnectionStatus[id]
+		if !newMetric.Connected {
 			newMetric.Latency = 999
-		} else {
-			pinger.Count = 1
-			pinger.Timeout = time.Second * 2
-			err = pinger.Run()
-			if err != nil {
-				logger.Log(0, "failed ping for metrics on peer address", address, err.Error())
-				newMetric.Connected = false
-				newMetric.Latency = 999
-			} else {
-				pingStats := pinger.Statistics()
-				if pingStats.PacketsRecv > 0 {
-					newMetric.Uptime = 1
-					newMetric.Connected = true
-					newMetric.Latency = pingStats.AvgRtt.Milliseconds()
-				}
-			}
 		}
-
+		newMetric.Uptime = 1
 		// check device peer to see if WG is working if ping failed
 		if !newMetric.Connected {
 			if currPeer.ReceiveBytes > 0 &&
@@ -79,11 +57,13 @@ func Collect(iface, network string, proxy bool, peerMap models.PeerMap) (*models
 				newMetric.Uptime = 1
 			}
 		}
-
 		newMetric.TotalTime = 1
 		metrics.Connectivity[id] = newMetric
-		metrics.ProxyMetrics[id] = proxy_metrics.GetMetric(network, currPeer.PublicKey.String())
-		proxy_metrics.ResetMetricsForPeer(network, currPeer.PublicKey.String())
+		if len(proxyMetrics.NodeConnectionStatus) == 1 {
+			proxy_metrics.ResetMetricsForPeer(currPeer.PublicKey.String())
+		} else {
+			proxy_metrics.ResetMetricForNode(currPeer.PublicKey.String(), id)
+		}
 	}
 
 	fillUnconnectedData(&metrics, peerMap)

+ 6 - 9
models/metrics.go

@@ -2,19 +2,16 @@ package models
 
 import (
 	"time"
-
-	proxy_models "github.com/gravitl/netclient/nmproxy/models"
 )
 
 // Metrics - metrics struct
 type Metrics struct {
-	Network       string                         `json:"network" bson:"network" yaml:"network"`
-	NodeID        string                         `json:"node_id" bson:"node_id" yaml:"node_id"`
-	NodeName      string                         `json:"node_name" bson:"node_name" yaml:"node_name"`
-	IsServer      string                         `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"`
-	Connectivity  map[string]Metric              `json:"connectivity" bson:"connectivity" yaml:"connectivity"`
-	FailoverPeers map[string]string              `json:"needsfailover" bson:"needsfailover" yaml:"needsfailover"`
-	ProxyMetrics  map[string]proxy_models.Metric `json:"proxy_metrics" bson:"proxy_metrics" yaml:"proxy_metrics"`
+	Network       string            `json:"network" bson:"network" yaml:"network"`
+	NodeID        string            `json:"node_id" bson:"node_id" yaml:"node_id"`
+	NodeName      string            `json:"node_name" bson:"node_name" yaml:"node_name"`
+	IsServer      string            `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"`
+	Connectivity  map[string]Metric `json:"connectivity" bson:"connectivity" yaml:"connectivity"`
+	FailoverPeers map[string]string `json:"needsfailover" bson:"needsfailover" yaml:"needsfailover"`
 }
 
 // Metric - holds a metric for data between nodes