This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fancollector.go
90 lines (75 loc) · 1.79 KB
/
fancollector.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
package lmsensorsexporter
import (
"github.com/mdlayher/lmsensors"
"github.com/prometheus/client_golang/prometheus"
)
// A FanCollector is a Prometheus collector for lmsensors fan
// sensor metrics.
type FanCollector struct {
RPM *prometheus.Desc
Alarm *prometheus.Desc
devices []*lmsensors.Device
}
var _ prometheus.Collector = &FanCollector{}
// NewFanCollector creates a new FanCollector.
func NewFanCollector(devices []*lmsensors.Device) *FanCollector {
const (
subsystem = "fan"
)
var (
labels = []string{"device", "sensor"}
)
return &FanCollector{
devices: devices,
RPM: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "rpm"),
"Current fan speed detected by sensor in rotations per minute.",
labels,
nil,
),
Alarm: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "alarm"),
"Whether or not a fan 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 *FanCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.RPM,
c.Alarm,
}
for _, d := range ds {
ch <- d
}
}
// Collect sends the metric values for each metric created by the FanCollector
// to the provided prometheus Metric channel.
func (c *FanCollector) Collect(ch chan<- prometheus.Metric) {
for _, d := range c.devices {
for _, s := range d.Sensors {
fs, ok := s.(*lmsensors.FanSensor)
if !ok {
continue
}
labels := []string{
d.Name,
fs.Name,
}
ch <- prometheus.MustNewConstMetric(
c.RPM,
prometheus.GaugeValue,
float64(fs.Input),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.Alarm,
prometheus.GaugeValue,
boolFloat64(fs.Alarm),
labels...,
)
}
}
}