forked from mdlayher/lmsensors_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperaturecollector.go
91 lines (76 loc) · 1.98 KB
/
temperaturecollector.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
package lmsensorsexporter
import (
"github.com/mdlayher/lmsensors"
"github.com/prometheus/client_golang/prometheus"
)
// A TemperatureCollector is a Prometheus collector for lmsensors temperature
// sensor metrics.
type TemperatureCollector struct {
DegreesCelsius *prometheus.Desc
Alarm *prometheus.Desc
devices []*lmsensors.Device
}
var _ prometheus.Collector = &TemperatureCollector{}
// NewTemperatureCollector creates a new TemperatureCollector.
func NewTemperatureCollector(devices []*lmsensors.Device) *TemperatureCollector {
const (
subsystem = "temperature"
)
var (
labels = []string{"device", "sensor", "details"}
)
return &TemperatureCollector{
devices: devices,
DegreesCelsius: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "degrees_celsius"),
"Current temperature detected by sensor in degrees Celsius.",
labels,
nil,
),
Alarm: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "alarm"),
"Whether or not a temperature sensor has triggered an alarm (1 - true, 0 - false).",
labels,
nil,
),
}
}
// Describe sends the descriptors of each metric over to the provided channel.
func (c *TemperatureCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.DegreesCelsius,
c.Alarm,
}
for _, d := range ds {
ch <- d
}
}
// Collect sends the metric values for each metric created by the TemperatureCollector
// to the provided prometheus Metric channel.
func (c *TemperatureCollector) Collect(ch chan<- prometheus.Metric) {
for _, d := range c.devices {
for _, s := range d.Sensors {
ts, ok := s.(*lmsensors.TemperatureSensor)
if !ok {
continue
}
labels := []string{
d.Name,
ts.Name,
ts.Label,
}
ch <- prometheus.MustNewConstMetric(
c.DegreesCelsius,
prometheus.GaugeValue,
ts.Input,
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.Alarm,
prometheus.GaugeValue,
boolFloat64(ts.Alarm),
labels...,
)
}
}
}