Files
mirror-3x-ui/internal/web/service/node_telemetry.go
T
Hamed Sanaei 45236cd492 feat(nodes): add fleet monitoring and host telemetry
Carry sampled host metrics through existing heartbeats and count client identities in SQL. Show the current panel and fleet in a responsive monitoring table based on the approved design.
2026-12-01 14:07:51 +03:30

124 lines
4.3 KiB
Go

package service
import (
"encoding/json"
"math"
"slices"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
const nodeTelemetryFreshness = 30 * time.Second
func validNodeLoad(load float64) bool {
return load >= 0 && !math.IsInf(load, 0)
}
type nodeStatusMetrics struct {
Cpu json.RawMessage `json:"cpu"`
CpuCores json.RawMessage `json:"cpuCores"`
LogicalPro json.RawMessage `json:"logicalPro"`
Mem json.RawMessage `json:"mem"`
TcpCount json.RawMessage `json:"tcpCount"`
UdpCount json.RawMessage `json:"udpCount"`
Loads json.RawMessage `json:"loads"`
Uptime json.RawMessage `json:"uptime"`
NetIO json.RawMessage `json:"netIO"`
ResourceScope string `json:"resourceScope"`
SampleAgeMillis *int64 `json:"sampleAgeMillis"`
UnavailableMetrics []string `json:"unavailableMetrics"`
}
type nodeMetricNumber interface {
int | uint64 | float64
}
func readNodeMetric[T nodeMetricNumber](raw json.RawMessage, unavailable []string, key string) *T {
var v *T
if slices.Contains(unavailable, key) || json.Unmarshal(raw, &v) != nil || v == nil || *v < 0 {
return nil
}
return v
}
func (m nodeStatusMetrics) telemetry(received time.Time) *model.NodeTelemetry {
unavailable := m.UnavailableMetrics
var memory struct {
Current json.RawMessage `json:"current"`
Total json.RawMessage `json:"total"`
Available json.RawMessage `json:"available"`
}
var network struct {
Up json.RawMessage `json:"up"`
Down json.RawMessage `json:"down"`
}
var loads [3]json.RawMessage
_ = json.Unmarshal(m.Mem, &memory)
_ = json.Unmarshal(m.NetIO, &network)
_ = json.Unmarshal(m.Loads, &loads)
age := int64(0)
if m.SampleAgeMillis != nil {
age = min(max(0, *m.SampleAgeMillis), nodeTelemetryFreshness.Milliseconds())
}
sample := &model.NodeTelemetry{
SampledAt: received.UnixMilli() - age,
ResourceScope: m.ResourceScope,
CpuPct: readNodeMetric[float64](m.Cpu, unavailable, "cpu"),
PhysicalCores: readNodeMetric[int](m.CpuCores, unavailable, "cpuCores"),
LogicalCores: readNodeMetric[int](m.LogicalPro, unavailable, "logicalPro"),
MemUsedBytes: readNodeMetric[uint64](memory.Current, unavailable, "mem"),
MemTotalBytes: readNodeMetric[uint64](memory.Total, unavailable, "mem"),
MemAvailableBytes: readNodeMetric[uint64](memory.Available, unavailable, "mem"),
TCPCount: readNodeMetric[int](m.TcpCount, unavailable, "tcpCount"),
UDPCount: readNodeMetric[int](m.UdpCount, unavailable, "udpCount"),
Load1: readNodeMetric[float64](loads[0], unavailable, "loads"),
Load5: readNodeMetric[float64](loads[1], unavailable, "loads"),
Load15: readNodeMetric[float64](loads[2], unavailable, "loads"),
UptimeSecs: readNodeMetric[uint64](m.Uptime, unavailable, "uptime"),
NetUp: readNodeMetric[uint64](network.Up, unavailable, "netUp"),
NetDown: readNodeMetric[uint64](network.Down, unavailable, "netDown"),
}
if sample.CpuPct != nil && *sample.CpuPct > 100 {
sample.CpuPct = nil
}
if sample.ResourceScope != "" || (sample.PhysicalCores != nil && *sample.PhysicalCores == 0) {
sample.PhysicalCores = nil
}
if sample.LogicalCores != nil && *sample.LogicalCores == 0 {
sample.LogicalCores = nil
}
if sample.MemTotalBytes == nil || *sample.MemTotalBytes == 0 ||
sample.MemUsedBytes == nil || *sample.MemUsedBytes > *sample.MemTotalBytes {
sample.MemUsedBytes, sample.MemTotalBytes, sample.MemAvailableBytes = nil, nil, nil
} else if sample.MemAvailableBytes != nil && *sample.MemAvailableBytes > *sample.MemTotalBytes {
sample.MemAvailableBytes = nil
}
return sample
}
func liveNodeTelemetry(node *model.Node, now time.Time) *model.NodeTelemetry {
sample := node.Telemetry
if sample == nil || !node.Enable || node.Status != "online" {
return nil
}
age := now.UnixMilli() - sample.SampledAt
if age < 0 || age >= nodeTelemetryFreshness.Milliseconds() {
return nil
}
view := *sample
view.SampleAgeMillis = &age
return &view
}
// StatusWithSampleAge publishes age relative to the sampler's clock.
func StatusWithSampleAge(status *Status) *Status {
if status == nil {
return nil
}
result := *status
result.SampleAgeMillis = new(max(0, time.Since(status.T).Milliseconds()))
return &result
}