forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_instance.go
More file actions
130 lines (114 loc) · 3.45 KB
/
Copy pathsystem_instance.go
File metadata and controls
130 lines (114 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package service
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
"github.com/bytedance/gopkg/util/gopool"
)
const systemInstanceReportInterval = 30 * time.Second
var systemInstanceReporterOnce sync.Once
type SystemInstanceInfo struct {
SchemaVersion int `json:"schema_version"`
Node common.NodeIdentity `json:"node"`
Role SystemInstanceRoleInfo `json:"role"`
Runtime SystemInstanceRuntimeInfo `json:"runtime"`
Host SystemInstanceHostInfo `json:"host"`
Resources SystemInstanceResources `json:"resources,omitempty"`
Extra map[string]any `json:"extra,omitempty"`
}
type SystemInstanceRoleInfo struct {
IsMaster bool `json:"is_master"`
}
type SystemInstanceRuntimeInfo struct {
Version string `json:"version"`
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
StartedAt int64 `json:"started_at"`
}
type SystemInstanceHostInfo struct {
Hostname string `json:"hostname"`
}
type SystemInstanceResources struct {
CPU SystemInstanceResourceUsage `json:"cpu"`
Memory SystemInstanceResourceUsage `json:"memory"`
Storage SystemInstanceStorageMetrics `json:"storage"`
}
type SystemInstanceResourceUsage struct {
UsagePercent float64 `json:"usage_percent"`
}
type SystemInstanceStorageMetrics struct {
TotalBytes uint64 `json:"total_bytes"`
UsedBytes uint64 `json:"used_bytes"`
FreeBytes uint64 `json:"free_bytes"`
UsedPercent float64 `json:"used_percent"`
}
func StartSystemInstanceReporter() {
systemInstanceReporterOnce.Do(func() {
gopool.Go(func() {
reportSystemInstanceWithLog()
ticker := time.NewTicker(systemInstanceReportInterval)
defer ticker.Stop()
for range ticker.C {
reportSystemInstanceWithLog()
}
})
})
}
func ReportCurrentSystemInstance() error {
identity := common.GetNodeIdentity()
hostname, hostnameErr := os.Hostname()
if strings.TrimSpace(identity.Name) == "" {
if hostnameErr != nil || strings.TrimSpace(hostname) == "" {
return fmt.Errorf("system instance node name is empty")
}
identity.Name = hostname
identity.Source = common.NodeNameSourceHostname
identity.ManuallyConfigured = false
identity.ShouldConfigureManually = true
}
systemStatus := common.GetSystemStatus()
diskInfo := common.GetDiskSpaceInfo()
info := SystemInstanceInfo{
SchemaVersion: 1,
Node: identity,
Role: SystemInstanceRoleInfo{
IsMaster: common.IsMasterNode,
},
Runtime: SystemInstanceRuntimeInfo{
Version: common.Version,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
StartedAt: common.StartTime,
},
Host: SystemInstanceHostInfo{
Hostname: hostname,
},
Resources: SystemInstanceResources{
CPU: SystemInstanceResourceUsage{
UsagePercent: systemStatus.CPUUsage,
},
Memory: SystemInstanceResourceUsage{
UsagePercent: systemStatus.MemoryUsage,
},
Storage: SystemInstanceStorageMetrics{
TotalBytes: diskInfo.Total,
UsedBytes: diskInfo.Used,
FreeBytes: diskInfo.Free,
UsedPercent: diskInfo.UsedPercent,
},
},
}
return model.UpsertSystemInstance(identity.Name, info, common.StartTime, common.GetTimestamp())
}
func reportSystemInstanceWithLog() {
if err := ReportCurrentSystemInstance(); err != nil {
logger.LogWarn(context.Background(), fmt.Sprintf("system instance report failed: %v", err))
}
}