forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 4
/
host.go
59 lines (49 loc) · 1.32 KB
/
host.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
package debug
import (
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)
const (
// DiskUsagePath is the path to check usage of the disk.
// Must be a filesystem path such as "/", not device file path like "/dev/vda1"
DiskUsagePath = "/"
)
// HostInfo includes information about resources on the host as well as
// collection time and
type HostInfo struct {
Memory *mem.VirtualMemoryStat
CPU []cpu.InfoStat
Host *host.InfoStat
Disk *disk.UsageStat
CollectionTime int64
Errors []error
}
// CollectHostInfo queries the host system and returns HostInfo. Any
// errors encountered will be returned in HostInfo.Errors
func CollectHostInfo() *HostInfo {
info := &HostInfo{CollectionTime: time.Now().UTC().UnixNano()}
if h, err := host.Info(); err != nil {
info.Errors = append(info.Errors, err)
} else {
info.Host = h
}
if v, err := mem.VirtualMemory(); err != nil {
info.Errors = append(info.Errors, err)
} else {
info.Memory = v
}
if d, err := disk.Usage(DiskUsagePath); err != nil {
info.Errors = append(info.Errors, err)
} else {
info.Disk = d
}
if c, err := cpu.Info(); err != nil {
info.Errors = append(info.Errors, err)
} else {
info.CPU = c
}
return info
}