forked from datarhei/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
61 lines (53 loc) · 1.86 KB
/
cpu.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
package prometheus
import (
"github.com/datarhei/core/v16/monitor/metric"
"github.com/prometheus/client_golang/prometheus"
)
type cpuCollector struct {
core string
collector metric.Reader
cpuSystemTimeDesc *prometheus.Desc
cpuUserTimeDesc *prometheus.Desc
cpuIdleTimeDesc *prometheus.Desc
cpuOtherTimeDesc *prometheus.Desc
}
func NewCPUCollector(core string, c metric.Reader) prometheus.Collector {
return &cpuCollector{
core: core,
collector: c,
cpuSystemTimeDesc: prometheus.NewDesc(
"cpu_system_time_percent",
"CPU system time in percent",
[]string{"core"}, nil),
cpuUserTimeDesc: prometheus.NewDesc(
"cpu_user_time_percent",
"CPU user time in percent",
[]string{"core"}, nil),
cpuIdleTimeDesc: prometheus.NewDesc(
"cpu_idle_time_percent",
"CPU idle time in percent",
[]string{"core"}, nil),
cpuOtherTimeDesc: prometheus.NewDesc(
"cpu_other_time_percent",
"CPU other time in percent",
[]string{"core"}, nil),
}
}
func (c *cpuCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.cpuSystemTimeDesc
ch <- c.cpuUserTimeDesc
ch <- c.cpuIdleTimeDesc
ch <- c.cpuOtherTimeDesc
}
func (c *cpuCollector) Collect(ch chan<- prometheus.Metric) {
metrics := c.collector.Collect([]metric.Pattern{
metric.NewPattern("cpu_system"),
metric.NewPattern("cpu_user"),
metric.NewPattern("cpu_idle"),
metric.NewPattern("cpu_other"),
})
ch <- prometheus.MustNewConstMetric(c.cpuSystemTimeDesc, prometheus.GaugeValue, metrics.Value("cpu_system").Val(), c.core)
ch <- prometheus.MustNewConstMetric(c.cpuUserTimeDesc, prometheus.GaugeValue, metrics.Value("cpu_user").Val(), c.core)
ch <- prometheus.MustNewConstMetric(c.cpuIdleTimeDesc, prometheus.GaugeValue, metrics.Value("cpu_idle").Val(), c.core)
ch <- prometheus.MustNewConstMetric(c.cpuOtherTimeDesc, prometheus.GaugeValue, metrics.Value("cpu_other").Val(), c.core)
}