2022-09-14 03:25:56 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
2022-10-12 02:13:17 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/wireguard"
|
2022-09-14 03:25:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetMetrics - gets the metrics
|
|
|
|
func GetMetrics(nodeid string) (*models.Metrics, error) {
|
|
|
|
var metrics models.Metrics
|
|
|
|
record, err := database.FetchRecord(database.METRICS_TABLE_NAME, nodeid)
|
|
|
|
if err != nil {
|
|
|
|
if database.IsEmptyRecord(err) {
|
|
|
|
return &metrics, nil
|
|
|
|
}
|
|
|
|
return &metrics, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(record), &metrics)
|
|
|
|
if err != nil {
|
|
|
|
return &metrics, err
|
|
|
|
}
|
|
|
|
return &metrics, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMetrics - updates the metrics of a given client
|
|
|
|
func UpdateMetrics(nodeid string, metrics *models.Metrics) error {
|
|
|
|
data, err := json.Marshal(metrics)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return database.Insert(nodeid, string(data), database.METRICS_TABLE_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMetrics - deletes metrics of a given node
|
|
|
|
func DeleteMetrics(nodeid string) error {
|
|
|
|
return database.DeleteRecord(database.METRICS_TABLE_NAME, nodeid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CollectServerMetrics - collects metrics for given server node
|
|
|
|
func CollectServerMetrics(serverID string, networkNodes []models.Node) *models.Metrics {
|
|
|
|
newServerMetrics := models.Metrics{}
|
|
|
|
newServerMetrics.Connectivity = make(map[string]models.Metric)
|
2022-10-12 02:13:17 +08:00
|
|
|
var serverNode models.Node
|
2022-09-14 03:25:56 +08:00
|
|
|
for i := range networkNodes {
|
|
|
|
currNodeID := networkNodes[i].ID
|
|
|
|
if currNodeID == serverID {
|
2022-10-12 02:13:17 +08:00
|
|
|
serverNode = networkNodes[i]
|
2022-09-14 03:25:56 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if currMetrics, err := GetMetrics(currNodeID); err == nil {
|
|
|
|
if currMetrics.Connectivity != nil && currMetrics.Connectivity[serverID].Connected {
|
|
|
|
metrics := currMetrics.Connectivity[serverID]
|
|
|
|
metrics.NodeName = networkNodes[i].Name
|
|
|
|
metrics.IsServer = "no"
|
|
|
|
newServerMetrics.Connectivity[currNodeID] = metrics
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newServerMetrics.Connectivity[currNodeID] = models.Metric{
|
|
|
|
Connected: false,
|
|
|
|
Latency: 999,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 02:13:17 +08:00
|
|
|
|
|
|
|
if serverNode.IsIngressGateway == "yes" {
|
|
|
|
clients, err := GetExtClientsByID(serverID, serverNode.Network)
|
|
|
|
if err == nil {
|
|
|
|
peers, err := wireguard.GetDevicePeers(serverNode.Interface)
|
|
|
|
if err == nil {
|
|
|
|
for i := range clients {
|
|
|
|
for j := range peers {
|
|
|
|
if clients[i].PublicKey == peers[j].PublicKey.String() {
|
2022-10-12 03:30:55 +08:00
|
|
|
if peers[j].ReceiveBytes > 0 &&
|
2022-10-12 02:13:17 +08:00
|
|
|
peers[j].TransmitBytes > 0 {
|
|
|
|
newServerMetrics.Connectivity[clients[i].ClientID] = models.Metric{
|
|
|
|
NodeName: clients[i].ClientID,
|
2022-10-13 01:08:39 +08:00
|
|
|
TotalTime: 5,
|
|
|
|
Uptime: 5,
|
2022-10-12 02:13:17 +08:00
|
|
|
IsServer: "no",
|
|
|
|
TotalReceived: peers[j].ReceiveBytes,
|
|
|
|
TotalSent: peers[j].TransmitBytes,
|
|
|
|
Connected: true,
|
2022-10-15 00:15:01 +08:00
|
|
|
Latency: -1, // can not determine latency on server currently
|
2022-10-12 02:13:17 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newServerMetrics.Connectivity[clients[i].ClientID] = models.Metric{
|
|
|
|
NodeName: clients[i].ClientID,
|
2022-10-13 01:08:39 +08:00
|
|
|
TotalTime: 5,
|
2022-10-12 02:13:17 +08:00
|
|
|
Uptime: 0,
|
|
|
|
IsServer: "no",
|
|
|
|
Connected: false,
|
|
|
|
Latency: 999,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 03:25:56 +08:00
|
|
|
return &newServerMetrics
|
|
|
|
}
|