-
Notifications
You must be signed in to change notification settings - Fork 5
/
system_collector.go
92 lines (78 loc) · 2.48 KB
/
system_collector.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
87
88
89
90
91
92
package edgemax_exporter
import (
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/vaga/edgemax_exporter/edgemax"
)
// A systemCollector is a Prometheus collector for metrics regarding Ubiquiti
// UniFi devices.
type systemCollector struct {
cpuPercent prometheus.Gauge
uptimeSeconds prometheus.Gauge
memoryPercent prometheus.Gauge
}
// Verify that the Exporter implements the prometheus.Collector interface.
var _ prometheus.Collector = &systemCollector{}
// newSystemCollector creates a new systemCollector which collects metrics for
// a specified site.
func newSystemCollector(ch <-chan edgemax.SystemStat) *systemCollector {
const subsystem = "system"
c := &systemCollector{
cpuPercent: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "cpu_percent",
Help: "System CPU usage percentage",
}),
uptimeSeconds: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "uptime_seconds",
Help: "System uptime in seconds",
}),
memoryPercent: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "memory_percent",
Help: "System memory usage percentage",
}),
}
go c.collect(ch)
return c
}
// collect begins a metrics collection task for all metrics related to UniFi
// devices.
func (c *systemCollector) collect(ch <-chan edgemax.SystemStat) {
for s := range ch {
cpu, _ := strconv.Atoi(s.CPU)
c.cpuPercent.Set(float64(cpu))
uptime, _ := strconv.Atoi(s.Uptime)
c.uptimeSeconds.Set(float64(uptime))
mem, _ := strconv.Atoi(s.Mem)
c.memoryPercent.Set(float64(mem))
}
}
// metrics contains a list of metrics which are collected each time
// the exporter is scraped. This list must be kept in sync with the metrics
// in systemCollector.
func (c *systemCollector) metrics() []prometheus.Metric {
return []prometheus.Metric{
c.cpuPercent,
c.uptimeSeconds,
c.memoryPercent,
}
}
// Describe sends the descriptors of each metric over to the provided channel.
// The corresponding metric values are sent separately.
func (c *systemCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range c.metrics() {
ch <- m.Desc()
}
}
// Collect sends the metric values for each metric pertaining to the global
// cluster usage over to the provided prometheus Metric channel.
func (c *systemCollector) Collect(ch chan<- prometheus.Metric) {
for _, m := range c.metrics() {
ch <- m
}
}