2018-10-17 20:20:35 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
2018-10-17 20:20:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DiskUsagePath is the path to check usage of the disk.
|
2018-11-02 17:00:39 +00:00
|
|
|
// Must be a filesystem path such as "/", not device file path like "/dev/vda1"
|
2018-10-17 20:20:35 +00:00
|
|
|
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
|
|
|
|
}
|