-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
hostmetrics.go
86 lines (75 loc) · 2.76 KB
/
hostmetrics.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package downwardmetrics
import (
"os"
"time"
"github.com/c9s/goprocinfo/linux"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/downwardmetrics/vhostmd/api"
metricspkg "kubevirt.io/kubevirt/pkg/downwardmetrics/vhostmd/metrics"
)
type hostMetricsCollector struct {
procCPUInfo string
procStat string
procMemInfo string
procVMStat string
pageSize int
}
func (h *hostMetricsCollector) hostCPUMetrics() (metrics []api.Metric) {
if cpuinfo, err := linux.ReadCPUInfo(h.procCPUInfo); err == nil {
metrics = append(metrics,
metricspkg.MustToUnitlessHostMetric(cpuinfo.NumPhysicalCPU(), "NumberOfPhysicalCPUs"),
)
} else {
log.Log.Reason(err).Info("failed to collect cpuinfo on the node")
}
if stat, err := linux.ReadStat(h.procStat); err == nil {
// CLK_TCK is a constant on Linux, see e.g.
// https://github.com/containerd/cgroups/pull/12
var clk_tck float64 = 100
cpuTime := float64(stat.CPUStatAll.User+stat.CPUStatAll.Nice+stat.CPUStatAll.System) / clk_tck
metrics = append(metrics,
metricspkg.MustToHostMetric(cpuTime, "TotalCPUTime", "s"),
)
} else {
log.Log.Reason(err).Info("failed to collect cputime on the node")
}
return
}
func (h *hostMetricsCollector) hostMemoryMetrics() (metrics []api.Metric) {
if memInfo, err := linux.ReadMemInfo(h.procMemInfo); err == nil {
metrics = append(metrics,
metricspkg.MustToHostMetric(memInfo.MemFree, "FreePhysicalMemory", "KiB"),
metricspkg.MustToHostMetric(memInfo.MemFree+memInfo.SwapFree, "FreeVirtualMemory", "KiB"),
metricspkg.MustToHostMetric(memInfo.MemTotal-memInfo.MemFree-memInfo.Buffers-memInfo.Cached, "AllocatedToVirtualServers", "KiB"),
metricspkg.MustToHostMetric(memInfo.MemTotal+memInfo.SwapTotal-memInfo.MemFree-memInfo.Cached-memInfo.Buffers-memInfo.SwapCached, "UsedVirtualMemory", "KiB"),
)
} else {
log.Log.Reason(err).Info("failed to collect meminfo on the node")
}
if vmstat, err := linux.ReadVMStat(h.procVMStat); err == nil {
metrics = append(metrics,
metricspkg.MustToHostMetric(vmstat.PageSwapin*uint64(h.pageSize)/1024, "PagedInMemory", "KiB"),
metricspkg.MustToHostMetric(vmstat.PageSwapout*uint64(h.pageSize)/1024, "PagedOutMemory", "KiB"),
)
} else {
log.Log.Reason(err).Info("failed to collect vmstat on the node")
}
return
}
func (h *hostMetricsCollector) Collect() (metrics []api.Metric) {
metrics = append(metrics, h.hostCPUMetrics()...)
metrics = append(metrics, h.hostMemoryMetrics()...)
metrics = append(metrics,
metricspkg.MustToHostMetric(time.Now().Unix(), "Time", "s"),
)
return
}
func defaultHostMetricsCollector() *hostMetricsCollector {
return &hostMetricsCollector{
procCPUInfo: "/proc/cpuinfo",
procStat: "/proc/stat",
procMemInfo: "/proc/meminfo",
procVMStat: "/proc/vmstat",
pageSize: os.Getpagesize(),
}
}