forked from pingcap/sysutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware_info.go
108 lines (102 loc) · 2.83 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
package sysutil
import (
"fmt"
"runtime"
"strings"
pb "github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
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-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
ms, err := mem.VirtualMemory()
if err == nil {
results = append(results, &pb.ServerInfoItem{
Tp: "memory",
Name: "memory",
Pairs: []*pb.ServerInfoPair{
{Key: "capacity", Value: fmt.Sprintf("%d", ms.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: 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
}