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
/
Copy pathcurrentcollector.go
91 lines (76 loc) · 1.87 KB
/
currentcollector.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 CurrentCollector is a Prometheus collector for lmsensors current
// sensor metrics.
type CurrentCollector struct {
Amperes *prometheus.Desc
Alarm *prometheus.Desc
devices []*lmsensors.Device
}
var _ prometheus.Collector = &CurrentCollector{}
// NewCurrentCollector creates a new CurrentCollector.
func NewCurrentCollector(devices []*lmsensors.Device) *CurrentCollector {
const (
subsystem = "current"
)
var (
labels = []string{"device", "sensor", "details"}
)
return &CurrentCollector{
devices: devices,
Amperes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "amperes"),
"Current current detected by sensor in Amperes.",
labels,
nil,
),
Alarm: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "alarm"),
"Whether or not a current 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 *CurrentCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.Amperes,
c.Alarm,
}
for _, d := range ds {
ch <- d
}
}
// Collect sends the metric values for each metric created by the CurrentCollector
// to the provided prometheus Metric channel.
func (c *CurrentCollector) Collect(ch chan<- prometheus.Metric) {
for _, d := range c.devices {
for _, s := range d.Sensors {
cs, ok := s.(*lmsensors.CurrentSensor)
if !ok {
continue
}
labels := []string{
d.Name,
cs.Name,
cs.Label,
}
ch <- prometheus.MustNewConstMetric(
c.Amperes,
prometheus.GaugeValue,
cs.Input,
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.Alarm,
prometheus.GaugeValue,
boolFloat64(cs.Alarm),
labels...,
)
}
}
}