-
Notifications
You must be signed in to change notification settings - Fork 43
/
models.go
277 lines (237 loc) · 6.04 KB
/
models.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package health
import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/v3/host"
"github.com/yaklang/yaklang/common/log"
"net"
"os/exec"
"os/user"
"runtime"
"strings"
)
func GetSystemInfo() (*host.InfoStat, error) {
var (
stat *host.InfoStat
err error
)
stat, err = host.Info()
return stat, err
}
func GetSystemUsers() ([]*host.UserStat, error) {
var users []*host.UserStat
userStats, err := host.Users()
if err != nil {
unameRaw, _ := exec.Command("uname", "-r").CombinedOutput()
if bytes.Contains(unameRaw, []byte("microsoft")) {
return []*host.UserStat{}, nil
}
return nil, errors.Errorf("get user info failed(gopsutil): %v", err)
}
for _, u := range userStats {
users = append(users, &u)
}
usr, err := user.Current()
if err != nil {
return nil, errors.Errorf("fetch current user failed: %s", err)
}
users = append(users, &host.UserStat{
User: usr.Username,
})
if len(users) <= 0 {
return nil, errors.New("no users info")
}
return users, nil
}
type SystemMatrix struct {
NodeId string `json:"node_id"`
GOARCH string `json:"arch"`
GOOS string `json:"os"`
Version string `json:"version"`
Users []*host.UserStat `json:"users"`
ExternalNetwork string `json:"external_network"`
Network *NetworkInfo `json:"network"`
Matrix *host.InfoStat `json:"matrix"`
HealthInfos []*HealthInfo `json:"health_infos"`
NodeAliveDuration uint64 `json:"node_alive_duration"`
}
func (p *SystemMatrix) String() (str string) {
str = ""
jsonByte, err := json.MarshalIndent(*p, "", " ")
if err != nil {
return
}
str = string(jsonByte)
return
}
// 获取主User
func (p *SystemMatrix) GetMainUser() (mainUser string) {
if p.Users == nil {
return
}
if len(p.Users) == 0 {
return
}
mainUser = p.Users[0].User
return
}
// 所有用户tostring
func (p *SystemMatrix) UserToString() (allUser string) {
jsonByte, err := json.MarshalIndent(&p.Users, "", " ")
if err != nil {
return
}
allUser = string(jsonByte)
return
}
// 获取主mac
func (p *SystemMatrix) GetMainMacAndMainNetAddr() (mainMac, mainNetAddr string) {
if p.Network == nil {
return
}
if len(p.Network.Interfaces) == 0 {
return
}
//first not lookback addr
for _, v := range p.Network.Interfaces {
if len(v.Addrs) == 0 {
continue
}
for _, addr := range v.Addrs {
// get first ipv4 addr
if addr.Address != "127.0.0.1" && strings.Contains(addr.Address, ":") == false {
mainMac = v.HardwareAddr
mainNetAddr = addr.Address
return
}
}
}
return
}
func NewSystemMatrixBase() (*SystemMatrix, error) {
matrix, err := GetSystemInfo()
if err != nil {
return nil, err
}
users, err := GetSystemUsers()
if err != nil {
log.Warnf("get system users info failed: %v", err)
}
networkInfo, err := GetNetworkInfo()
externalNetworkInfo, _ := GetExternalIp()
if err != nil {
log.Warnf("get network info failed: %v", err)
}
return &SystemMatrix{
NodeId: "",
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
Version: "dev",
ExternalNetwork: externalNetworkInfo.Addr,
Network: networkInfo,
Users: users,
Matrix: matrix,
}, nil
}
type HealthInfo struct {
Timestamp int64 `json:"timestamp"`
CPUPercent float64 `json:"cpu_percent"`
MemoryPercent float64 `json:"memory_percent"`
// kb/sec
NetworkUpload float64 `json:"network_upload"`
NetworkDownload float64 `json:"network_download"`
// kb/sec
DiskWrite float64 `json:"disk_write"`
DiskRead float64 `json:"disk_read"`
DiskUsage *DiskStat `json:"disk_usage"`
}
type DiskStat struct {
Total uint64 `json:"total,omitempty"`
Used uint64 `json:"used,omitempty"`
}
type NetworkInfo struct {
Interfaces []*NetInterfaceInfo `json:"interfaces"`
}
type ExternalNetworkInfo struct {
Addr string `json:"ex_addr"`
}
func GetExternalIp() (ExternalNetworkInfo, error) {
var ifs = ExternalNetworkInfo{}
ifs.Addr = "error"
return ifs, nil
}
func GetNetworkInfo() (*NetworkInfo, error) {
var ifs []*NetInterfaceInfo
ifaces, err := net.Interfaces()
if err != nil {
return nil, errors.Errorf("fetch interface failed: %v", err)
}
for _, iface := range ifaces {
info := &NetInterfaceInfo{
Index: iface.Index,
MTU: iface.MTU,
Name: iface.Name,
HardwareAddr: iface.HardwareAddr.String(),
Flags: iface.Flags.String(),
}
addrs, err := iface.Addrs()
if err != nil {
log.Warnf("[%v] get addr failed: %v", iface.Name, err)
} else {
for _, addr := range addrs {
ip_value, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
info.Addrs = append(info.Addrs, &NetInterfaceAddr{
Network: addr.Network(),
Address: ip_value.String(),
})
}
}
mcAddrs, err := iface.MulticastAddrs()
if err != nil {
log.Warnf("[%v] get multicase-addrs failed: %v", iface.Name, err)
} else {
for _, addr := range mcAddrs {
ip_value, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
info.MulticastAddrs = append(info.MulticastAddrs, &NetInterfaceAddr{
Network: addr.Network(),
Address: ip_value.String(),
})
}
}
ifs = append(ifs, info)
}
return &NetworkInfo{
Interfaces: ifs,
}, nil
}
type ExternalNetInterfaceInfo struct {
Addrs string `json:"addrs"`
}
type NetInterfaceInfo struct {
Index int `json:"index"`
MTU int `json:"mtu"`
Name string `json:"name"`
HardwareAddr string `json:"hardware_addr"`
Flags string `json:"flags"`
Addrs []*NetInterfaceAddr `json:"addrs"`
MulticastAddrs []*NetInterfaceAddr `json:"multicase_addrs"`
}
func (p *NetInterfaceInfo) String() (str string) {
jsonByte, err := json.Marshal(*p)
if err != nil {
return
}
str = string(jsonByte)
return
}
type NetInterfaceAddr struct {
Network string `json:"network"`
Address string `json:"address"`
}