mirror of
https://github.com/gravitl/netmaker.git
synced 2024-09-20 15:26:04 +08:00
commit
abee5e104a
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Collect - collects metrics
|
// Collect - collects metrics
|
||||||
func Collect(iface, server, network string, peerMap models.PeerMap) (*models.Metrics, error) {
|
func Collect(iface, server, network string, peerMap models.PeerMap, proxy bool) (*models.Metrics, error) {
|
||||||
var metrics models.Metrics
|
var metrics models.Metrics
|
||||||
metrics.Connectivity = make(map[string]models.Metric)
|
metrics.Connectivity = make(map[string]models.Metric)
|
||||||
var wgclient, err = wgctrl.New()
|
var wgclient, err = wgctrl.New()
|
||||||
|
@ -45,6 +45,7 @@ func Collect(iface, server, network string, peerMap models.PeerMap) (*models.Met
|
||||||
newMetric.TotalSent = int64(proxyMetrics.TrafficSent)
|
newMetric.TotalSent = int64(proxyMetrics.TrafficSent)
|
||||||
newMetric.Latency = int64(proxyMetrics.LastRecordedLatency)
|
newMetric.Latency = int64(proxyMetrics.LastRecordedLatency)
|
||||||
newMetric.Connected = proxyMetrics.NodeConnectionStatus[id]
|
newMetric.Connected = proxyMetrics.NodeConnectionStatus[id]
|
||||||
|
newMetric.CollectedByProxy = proxy
|
||||||
if newMetric.Connected {
|
if newMetric.Connected {
|
||||||
newMetric.Uptime = 1
|
newMetric.Uptime = 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,7 @@ func GetProxyUpdateForHost(ctx context.Context, host *models.Host) (models.Proxy
|
||||||
currPeerConf = models.PeerConf{
|
currPeerConf = models.PeerConf{
|
||||||
Proxy: peerHost.ProxyEnabled,
|
Proxy: peerHost.ProxyEnabled,
|
||||||
PublicListenPort: int32(GetPeerListenPort(peerHost)),
|
PublicListenPort: int32(GetPeerListenPort(peerHost)),
|
||||||
|
ProxyListenPort: GetProxyListenPort(peerHost),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,10 +275,11 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
|
||||||
hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
|
hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
|
||||||
peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
|
peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
|
||||||
hostPeerUpdate.HostPeerIDs[peerHost.PublicKey.String()][peer.ID.String()] = models.IDandAddr{
|
hostPeerUpdate.HostPeerIDs[peerHost.PublicKey.String()][peer.ID.String()] = models.IDandAddr{
|
||||||
ID: peer.ID.String(),
|
ID: peer.ID.String(),
|
||||||
Address: peer.PrimaryAddress(),
|
Address: peer.PrimaryAddress(),
|
||||||
Name: peerHost.Name,
|
Name: peerHost.Name,
|
||||||
Network: peer.Network,
|
Network: peer.Network,
|
||||||
|
ProxyListenPort: GetProxyListenPort(peerHost),
|
||||||
}
|
}
|
||||||
nodePeer = peerConfig
|
nodePeer = peerConfig
|
||||||
} else {
|
} else {
|
||||||
|
@ -285,10 +287,11 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
|
||||||
peerAllowedIPs = append(peerAllowedIPs, allowedips...)
|
peerAllowedIPs = append(peerAllowedIPs, allowedips...)
|
||||||
hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
|
hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
|
||||||
hostPeerUpdate.HostPeerIDs[peerHost.PublicKey.String()][peer.ID.String()] = models.IDandAddr{
|
hostPeerUpdate.HostPeerIDs[peerHost.PublicKey.String()][peer.ID.String()] = models.IDandAddr{
|
||||||
ID: peer.ID.String(),
|
ID: peer.ID.String(),
|
||||||
Address: peer.PrimaryAddress(),
|
Address: peer.PrimaryAddress(),
|
||||||
Name: peerHost.Name,
|
Name: peerHost.Name,
|
||||||
Network: peer.Network,
|
Network: peer.Network,
|
||||||
|
ProxyListenPort: GetProxyListenPort(peerHost),
|
||||||
}
|
}
|
||||||
nodePeer = hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]]
|
nodePeer = hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]]
|
||||||
}
|
}
|
||||||
|
@ -402,6 +405,15 @@ func GetPeerListenPort(host *models.Host) int {
|
||||||
return peerPort
|
return peerPort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProxyListenPort - fetches the proxy listen port
|
||||||
|
func GetProxyListenPort(host *models.Host) int {
|
||||||
|
proxyPort := host.ProxyListenPort
|
||||||
|
if host.PublicListenPort != 0 {
|
||||||
|
proxyPort = host.PublicListenPort
|
||||||
|
}
|
||||||
|
return proxyPort
|
||||||
|
}
|
||||||
|
|
||||||
func getExtPeers(node *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, error) {
|
func getExtPeers(node *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, error) {
|
||||||
var peers []wgtypes.PeerConfig
|
var peers []wgtypes.PeerConfig
|
||||||
var idsAndAddr []models.IDandAddr
|
var idsAndAddr []models.IDandAddr
|
||||||
|
|
|
@ -15,15 +15,16 @@ type Metrics struct {
|
||||||
|
|
||||||
// Metric - holds a metric for data between nodes
|
// Metric - holds a metric for data between nodes
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"`
|
NodeName string `json:"node_name" bson:"node_name" yaml:"node_name"`
|
||||||
Uptime int64 `json:"uptime" bson:"uptime" yaml:"uptime"`
|
Uptime int64 `json:"uptime" bson:"uptime" yaml:"uptime"`
|
||||||
TotalTime int64 `json:"totaltime" bson:"totaltime" yaml:"totaltime"`
|
TotalTime int64 `json:"totaltime" bson:"totaltime" yaml:"totaltime"`
|
||||||
Latency int64 `json:"latency" bson:"latency" yaml:"latency"`
|
Latency int64 `json:"latency" bson:"latency" yaml:"latency"`
|
||||||
TotalReceived int64 `json:"totalreceived" bson:"totalreceived" yaml:"totalreceived"`
|
TotalReceived int64 `json:"totalreceived" bson:"totalreceived" yaml:"totalreceived"`
|
||||||
TotalSent int64 `json:"totalsent" bson:"totalsent" yaml:"totalsent"`
|
TotalSent int64 `json:"totalsent" bson:"totalsent" yaml:"totalsent"`
|
||||||
ActualUptime time.Duration `json:"actualuptime" bson:"actualuptime" yaml:"actualuptime"`
|
ActualUptime time.Duration `json:"actualuptime" bson:"actualuptime" yaml:"actualuptime"`
|
||||||
PercentUp float64 `json:"percentup" bson:"percentup" yaml:"percentup"`
|
PercentUp float64 `json:"percentup" bson:"percentup" yaml:"percentup"`
|
||||||
Connected bool `json:"connected" bson:"connected" yaml:"connected"`
|
Connected bool `json:"connected" bson:"connected" yaml:"connected"`
|
||||||
|
CollectedByProxy bool `json:"collected_by_proxy" bson:"collected_by_proxy" yaml:"collected_by_proxy"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IDandAddr - struct to hold ID and primary Address
|
// IDandAddr - struct to hold ID and primary Address
|
||||||
|
|
|
@ -37,6 +37,7 @@ type RelayedConf struct {
|
||||||
type PeerConf struct {
|
type PeerConf struct {
|
||||||
Proxy bool `json:"proxy"`
|
Proxy bool `json:"proxy"`
|
||||||
PublicListenPort int32 `json:"public_listen_port"`
|
PublicListenPort int32 `json:"public_listen_port"`
|
||||||
|
ProxyListenPort int `json:"proxy_listen_port"`
|
||||||
IsExtClient bool `json:"is_ext_client"`
|
IsExtClient bool `json:"is_ext_client"`
|
||||||
Address net.IP `json:"address"`
|
Address net.IP `json:"address"`
|
||||||
ExtInternalIp net.IP `json:"ext_internal_ip"`
|
ExtInternalIp net.IP `json:"ext_internal_ip"`
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
@ -362,6 +363,21 @@ func updateNodeMetrics(currentNode *models.Node, newMetrics *models.Metrics) boo
|
||||||
oldMetric := oldMetrics.Connectivity[k]
|
oldMetric := oldMetrics.Connectivity[k]
|
||||||
currMetric.TotalTime += oldMetric.TotalTime
|
currMetric.TotalTime += oldMetric.TotalTime
|
||||||
currMetric.Uptime += oldMetric.Uptime // get the total uptime for this connection
|
currMetric.Uptime += oldMetric.Uptime // get the total uptime for this connection
|
||||||
|
if currMetric.CollectedByProxy {
|
||||||
|
currMetric.TotalReceived += oldMetric.TotalReceived
|
||||||
|
currMetric.TotalSent += oldMetric.TotalSent
|
||||||
|
} else {
|
||||||
|
if currMetric.TotalReceived < oldMetric.TotalReceived {
|
||||||
|
currMetric.TotalReceived += oldMetric.TotalReceived
|
||||||
|
} else {
|
||||||
|
currMetric.TotalReceived += int64(math.Abs(float64(currMetric.TotalReceived) - float64(oldMetric.TotalReceived)))
|
||||||
|
}
|
||||||
|
if currMetric.TotalSent < oldMetric.TotalSent {
|
||||||
|
currMetric.TotalSent += oldMetric.TotalSent
|
||||||
|
} else {
|
||||||
|
currMetric.TotalSent += int64(math.Abs(float64(currMetric.TotalSent) - float64(oldMetric.TotalSent)))
|
||||||
|
}
|
||||||
|
}
|
||||||
if currMetric.Uptime == 0 || currMetric.TotalTime == 0 {
|
if currMetric.Uptime == 0 || currMetric.TotalTime == 0 {
|
||||||
currMetric.PercentUp = 0
|
currMetric.PercentUp = 0
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue