-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhardware_info.go
144 lines (134 loc) · 3.9 KB
/
hardware_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
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
// Copyright 2019 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sysutil
import (
"fmt"
"runtime"
"strings"
pb "github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
)
var getMemoryCapacity func() (uint64, error)
func RegisterGetMemoryCapacity(f func() (uint64, error)) {
getMemoryCapacity = f
}
func GetMemoryCapacity() (uint64, error) {
if getMemoryCapacity == nil {
memoryTotal, err := mem.VirtualMemory()
if err != nil {
return 0, err
}
return memoryTotal.Total, err
}
data, err := getMemoryCapacity()
if err != nil {
return 0, err
}
return data, err
}
func getHardwareInfo() []*pb.ServerInfoItem {
var results []*pb.ServerInfoItem
// cpu
infos, err := cpu.Info()
if err == nil && len(infos) > 0 {
physicalCores, err := cpu.Counts(false)
if err != nil {
physicalCores = int(infos[0].Cores)
}
results = append(results, &pb.ServerInfoItem{
Tp: "cpu",
Name: "cpu",
Pairs: []*pb.ServerInfoPair{
{Key: "cpu-arch", Value: fmt.Sprintf("%s", runtime.GOARCH)},
{Key: "cpu-logical-cores", Value: fmt.Sprintf("%d", runtime.NumCPU())},
{Key: "cpu-physical-cores", Value: fmt.Sprintf("%d", physicalCores)},
{Key: "cpu-frequency", Value: fmt.Sprintf("%.2fMHz", infos[0].Mhz)},
{Key: "cache", Value: fmt.Sprintf("%d", infos[0].CacheSize)},
},
})
}
// memory
total, err := GetMemoryCapacity()
if err == nil {
results = append(results, &pb.ServerInfoItem{
Tp: "memory",
Name: "memory",
Pairs: []*pb.ServerInfoPair{
{Key: "capacity", Value: fmt.Sprintf("%d", total)},
},
})
}
// disk
parts, err := disk.Partitions(true)
if err == nil && len(parts) > 0 {
for _, p := range parts {
if !strings.HasPrefix(p.Device, "/dev/") {
continue
}
usage, err := disk.Usage(p.Mountpoint)
if err != nil {
continue
}
results = append(results, &pb.ServerInfoItem{
Tp: "disk",
Name: p.Device[5:],
Pairs: []*pb.ServerInfoPair{
{Key: "fstype", Value: p.Fstype},
{Key: "opts", Value: strings.Join(p.Opts, ",")},
{Key: "path", Value: p.Mountpoint},
{Key: "total", Value: fmt.Sprintf("%d", usage.Total)},
{Key: "free", Value: fmt.Sprintf("%d", usage.Free)},
{Key: "used", Value: fmt.Sprintf("%d", usage.Used)},
{Key: "free-percent", Value: fmt.Sprintf("%.2f", (100-usage.UsedPercent)/100.00)},
{Key: "used-percent", Value: fmt.Sprintf("%.2f", usage.UsedPercent/100.00)},
},
})
}
}
// network
nics, err := net.Interfaces()
if err == nil && len(nics) > 0 {
for _, nic := range nics {
flag := func(f string) string {
for _, s := range nic.Flags {
if s == f {
return "true"
}
}
return "false"
}
var addrs []string
for _, addr := range nic.Addrs {
addrs = append(addrs, addr.Addr)
}
results = append(results, &pb.ServerInfoItem{
Tp: "net",
Name: nic.Name,
Pairs: []*pb.ServerInfoPair{
{Key: "mac", Value: nic.HardwareAddr},
{Key: "is-up", Value: flag("up")},
{Key: "is-broadcast", Value: flag("broadcast")},
{Key: "is-multicast", Value: flag("multicast")},
{Key: "is-loopback", Value: flag("loopback")},
{Key: "is-point-to-point", Value: flag("pointtopoint")},
{Key: "addresses", Value: strings.Join(addrs, ",")},
},
})
}
}
return results
}