-
Notifications
You must be signed in to change notification settings - Fork 312
/
node_info.go
82 lines (69 loc) · 1.99 KB
/
node_info.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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package telemetry
import (
"context"
"runtime"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)
// FillNodeInfo fill HardwareInfo and Os info.
func FillNodeInfo(ctx context.Context, info *NodeInfo) (err error) {
info.Hardware, err = GetHardwareInfo(ctx)
if err != nil {
return
}
info.Os, err = GetOSInfo(ctx)
if err != nil {
return
}
return nil
}
// GetHardwareInfo get the HardwareInfo.
func GetHardwareInfo(ctx context.Context) (info HardwareInfo, err error) {
if virt, role, err := host.VirtualizationWithContext(ctx); err == nil && role == "guest" {
info.Virtualization = virt
}
if l, err := load.AvgWithContext(ctx); err == nil {
info.Loadavg15 = float32(l.Load15)
}
// Fill cpu info
info.Cpu.Numcpu = int32(runtime.NumCPU())
if cpus, err := cpu.InfoWithContext(ctx); err == nil && len(cpus) > 0 {
info.Cpu.Sockets = int32(len(cpus))
c := cpus[0]
info.Cpu.Cores = c.Cores
info.Cpu.Model = c.ModelName
info.Cpu.Mhz = float32(c.Mhz)
info.Cpu.Features = c.Flags
}
// Fill mem info
if m, err := mem.VirtualMemory(); err == nil {
info.Mem.Available = m.Available
info.Mem.Total = m.Total
}
return
}
// GetOSInfo get the OSInfo.
func GetOSInfo(ctx context.Context) (info OSInfo, err error) {
platform, family, version, err := host.PlatformInformationWithContext(ctx)
if err != nil {
return
}
info.Platform = platform
info.Family = family
info.Version = version
return
}