github.com/fastest963/consul@v1.4.5/agent/debug/host.go (about)

     1  package debug
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/shirou/gopsutil/cpu"
     7  	"github.com/shirou/gopsutil/disk"
     8  	"github.com/shirou/gopsutil/host"
     9  	"github.com/shirou/gopsutil/mem"
    10  )
    11  
    12  const (
    13  	// DiskUsagePath is the path to check usage of the disk.
    14  	// Must be a filesystem path such as "/", not device file path like "/dev/vda1"
    15  	DiskUsagePath = "/"
    16  )
    17  
    18  // HostInfo includes information about resources on the host as well as
    19  // collection time and
    20  type HostInfo struct {
    21  	Memory         *mem.VirtualMemoryStat
    22  	CPU            []cpu.InfoStat
    23  	Host           *host.InfoStat
    24  	Disk           *disk.UsageStat
    25  	CollectionTime int64
    26  	Errors         []error
    27  }
    28  
    29  // CollectHostInfo queries the host system and returns HostInfo. Any
    30  // errors encountered will be returned in HostInfo.Errors
    31  func CollectHostInfo() *HostInfo {
    32  	info := &HostInfo{CollectionTime: time.Now().UTC().UnixNano()}
    33  
    34  	if h, err := host.Info(); err != nil {
    35  		info.Errors = append(info.Errors, err)
    36  	} else {
    37  		info.Host = h
    38  	}
    39  
    40  	if v, err := mem.VirtualMemory(); err != nil {
    41  		info.Errors = append(info.Errors, err)
    42  	} else {
    43  		info.Memory = v
    44  	}
    45  
    46  	if d, err := disk.Usage(DiskUsagePath); err != nil {
    47  		info.Errors = append(info.Errors, err)
    48  	} else {
    49  		info.Disk = d
    50  	}
    51  
    52  	if c, err := cpu.Info(); err != nil {
    53  		info.Errors = append(info.Errors, err)
    54  	} else {
    55  		info.CPU = c
    56  	}
    57  
    58  	return info
    59  }