host_resources.go 5.61 KB
Newer Older
Lei Li's avatar
Lei Li committed
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package resources

import (
	"linkfog.com/public/lib/l"
	"linkfog.com/public/lib/unit"
)

var (
	preHostDiskIOCountersStat *HostDiskIOCountersStatWrap
	preHostCPUTimesStat       *HostCPUTimesStatWrap
	preHostNetIOCountersStat  *HostNetIOCountersStatWrap
	cpuUsage                  float64
	memUsage                  float64
	diskUsage                 float64
	inodeUsage                float64
)

func getAndPrintHostInfo() {
	getAndPrintHostCPUInfo()
	getAndPrintHostCPUStates()

	getAndPrintHostMemoryUsage()

	// getAndPrintDockerRootDiskSpaceUsage()
	getAndPrintHostDiskSpaceUsage()
	getAndPrintHostDiskIOUsage()

	getAndPrintHostNetIOUsage()

	printHostSummary()
}

func getAndPrintHostCPUInfo() {
	cnt, err := GetHostCPUCount()
	if err != nil {
		l.Errorf("get host cpu count error: %v", err)
		return
	}

	usage, err := GetHostCPUUsage()
	if err != nil {
		l.Errorf("get host cpu usage error: %v", err)
		return
	}
	cpuUsage = usage

	avg, err := GetHostCPULoad()
	if err != nil {
		l.Errorf("get host cpu load error: %v", err)
		return
	}

	l.Debugf("host cpu info, count:%d, usage:%.1f%%, load1:%.2f, load5:%.2f, load15:%.2f",
		cnt, usage, avg.Load1, avg.Load5, avg.Load15)
}

func getAndPrintHostCPUStates() {
	var states []HostCPUStatesPct
	var err error
	states, preHostCPUTimesStat, err = GetHostCPUStatesWithPre(preHostCPUTimesStat)
	if err != nil {
		l.Errorf("get host cpu states error: %v", err)
		return
	}

	for _, state := range states {
		l.Debugf("host cpu states, cpu:%s, us:%.1f%%, sy:%.1f%%, ni:%.1f%%, id:%.1f%%, wa:%.1f%%, "+
			"hi:%.1f%%, si:%.1f%%, st:%.1f%%, gu:%.1f%%, gn:%.1f%%", state.CPU,
			state.User, state.System, state.Nice, state.Idle, state.Iowait,
			state.Irq, state.Softirq, state.Steal, state.Guest, state.GuestNice)
	}
}

func getAndPrintHostMemoryUsage() {
	vmStat, err := GetHostVirtualMemory()
	if err != nil {
		l.Errorf("get host virtual memory error: %v", err)
		return
	}
	memUsage = vmStat.UsedPercent

	vmExStat, err := GetHostVirtualMemoryEx()
	if err != nil {
		l.Errorf("get host virtual memoryEx error: %v", err)
		return
	}

	l.Debugf("host mem usage, total:%s, used:%s, avail:%s, act_file:%s, inact_file:%s, "+
		"act_anon:%s, inact_anon:%s, buff:%s, cache:%s, shr:%s, slab:%s", unit.ByteSize(vmStat.Total),
		unit.ByteSize(vmStat.Used), unit.ByteSize(vmStat.Available),
		unit.ByteSize(vmExStat.ActiveFile), unit.ByteSize(vmExStat.InactiveFile),
		unit.ByteSize(vmExStat.ActiveAnon), unit.ByteSize(vmExStat.InactiveAnon),
		unit.ByteSize(vmStat.Buffers), unit.ByteSize(vmStat.Cached),
		unit.ByteSize(vmStat.Shared), unit.ByteSize(vmStat.Slab))

	smStat, err := GetHostSwapMemory()
	if err != nil {
		l.Errorf("get host swap memory error: %v", err)
		return
	}

	l.Debugf("host swap mem usage, total:%s, used:%s, avail:%s",
		unit.ByteSize(smStat.Total), unit.ByteSize(smStat.Used),
		unit.ByteSize(smStat.Free))
}

//func getAndPrintDockerRootDiskSpaceUsage() {
//	usage, err := GetDockerRootDiskSpaceUsage()
//	if err != nil {
//		l.Errorf("get docker root disk space usage error: %v", err)
//		return
//	}
//	diskUsage = usage.UsedPercent
//	inodeUsage = usage.InodesUsedPercent
//
//	l.Debugf("host docker root disk space usage, path:%s, size:%s, used:%s, avail:%s, usage:%.2f%%, "+
//		"isize:%s, iused:%s, iavail:%s, iusage:%.2f%%", usage.Path,
//		unit.ByteSize(usage.Total), unit.ByteSize(usage.Used),
//		unit.ByteSize(usage.Free), usage.UsedPercent,
//		unit.ByteSize(usage.InodesTotal), unit.ByteSize(usage.InodesUsed),
//		unit.ByteSize(usage.InodesFree), usage.InodesUsedPercent)
//}

func getAndPrintHostDiskSpaceUsage() {
	usages, err := GetHostDiskSpaceUsage()
	if err != nil {
		l.Errorf("get host disk space usage error: %v", err)
		return
	}

	filter := make(map[string]struct{})
	for _, usage := range usages {
		if _, ok := filter[usage.PartitionStat.Device]; ok {
			continue
		}
		filter[usage.PartitionStat.Device] = struct{}{}
		l.Debugf("host disk space usage, dev:%s, size:%s, used:%s, avail:%s, usage:%.2f%%, "+
			"isize:%d, iused:%d, iavail:%d, iusage:%.2f%%",
			usage.PartitionStat.Device,
			unit.ByteSize(usage.UsageStat.Total),
			unit.ByteSize(usage.UsageStat.Used),
			unit.ByteSize(usage.UsageStat.Free),
			usage.UsageStat.UsedPercent,
			usage.UsageStat.InodesTotal,
			usage.UsageStat.InodesUsed,
			usage.UsageStat.InodesFree,
			usage.UsageStat.InodesUsedPercent)
	}
}

func getAndPrintHostDiskIOUsage() {
	var usages []HostDiskIOUsage
	var err error
	usages, preHostDiskIOCountersStat, err = GetHostDiskIOUsageWithPre(preHostDiskIOCountersStat)
	if err != nil {
		l.Errorf("get host disk io usage error: %v", err)
		return
	}

	for _, usage := range usages {
		l.Debugf("host disk io stats, dev:%s, rps:%s, wps:%s, r:%s, w:%s", usage.Device,
			unit.ByteSize(usage.ReadUsage), unit.ByteSize(usage.WriteUsage),
			unit.ByteSize(usage.ReadBytes), unit.ByteSize(usage.WriteBytes))
	}
}

func getAndPrintHostNetIOUsage() {
	var usages []HostNetIOUsage
	var err error
	usages, preHostNetIOCountersStat, err = GetHostNetIOUsageWithPre(preHostNetIOCountersStat)
	if err != nil {
		l.Errorf("get host net io usage error: %v", err)
		return
	}

	for _, usage := range usages {
		l.Infof("host net io stats, if:%s, "+
			"rx/s:%s, tx/s:%s, rx_pkg/s:%d, tx_pkg/s:%d, rx_err/s:%d, tx_err/s:%d, "+
			"rx_drop/s:%d, tx_drop/s:%d, rx_fifo/s:%d, tx_fifo/s:%d", usage.Name,
			unit.ByteSize(usage.BytesRecv), unit.ByteSize(usage.BytesSent),
			usage.PacketsRecv, usage.PacketsSent,
			usage.Errin, usage.Errout,
			usage.Dropin, usage.Dropout,
			usage.Fifoin, usage.Fifoout)
	}
}

func printHostSummary() {
	l.Infof("host stats, cpu_usage:%.2f%%, mem_usage:%.2f%%, disk_usage:%.2f%%, inode_usage:%.2f%%",
		cpuUsage, memUsage, diskUsage, inodeUsage)
}