/
host_monitor.go
71 lines (62 loc) · 1.58 KB
/
host_monitor.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
package task
import (
"time"
"github.com/ZongweiBai/golang-in-action/config"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
// cpu info
func GetCpuInfo() {
cpuInfos, err := cpu.Info()
if err != nil {
config.LOG.Errorf("get cpu info failed", err)
}
for _, ci := range cpuInfos {
config.LOG.Info(ci)
}
// CPU使用率
percent, _ := cpu.Percent(time.Second, false)
config.LOG.Infof("cpu percent:%v", percent)
}
// 获取CPU负载信息
func GetCpuLoad() {
info, _ := load.Avg()
config.LOG.Infof("CPU load:%v", info)
}
// mem info
func GetMemInfo() {
memInfo, _ := mem.VirtualMemory()
config.LOG.Infof("mem info:%v", memInfo)
}
// host info
func GetHostInfo() {
hInfo, _ := host.Info()
config.LOG.Infof("host info:%v uptime:%v boottime:%v", hInfo, hInfo.Uptime, hInfo.BootTime)
}
// disk info
func GetDiskInfo() {
parts, err := disk.Partitions(true)
if err != nil {
config.LOG.Errorf("get Partitions failed, err:%v", err)
return
}
for _, part := range parts {
config.LOG.Infof("part:%v\n", part.String())
// diskInfo, _ := disk.Usage(part.Mountpoint)
// config.LOG.Infof("disk info:used:%v free:%v", diskInfo.UsedPercent, diskInfo.Free)
}
ioStat, _ := disk.IOCounters()
for k, v := range ioStat {
config.LOG.Infof("iostat:%v:%v", k, v)
}
}
func GetNetInfo() {
info, _ := net.IOCounters(true)
for index, v := range info {
config.LOG.Infof("%v:%v send:%v recv:%v", index, v, v.BytesSent, v.BytesRecv)
}
}