forked from mdlayher/lmsensors_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmsensorsexporter.go
84 lines (71 loc) · 2.06 KB
/
lmsensorsexporter.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
// Package lmsensorsexporter provides the Exporter type used in the
// lmsensors_exporter Prometheus exporter.
package lmsensorsexporter
import (
"log"
"github.com/mdlayher/lmsensors"
"github.com/prometheus/client_golang/prometheus"
)
const (
// namespace is the top-level namespace for this lmsensors exporter.
namespace = "lmsensors"
)
// An Exporter is a Prometheus exporter for lmsensors metrics.
// It wraps all lmsensors metrics collectors and provides a single global
// exporter which can serve metrics.
//
// It implements the prometheus.Collector interface in order to register
// with Prometheus.
type Exporter struct {
s *lmsensors.Scanner
}
var _ prometheus.Collector = &Exporter{}
// New creates a new Exporter which collects metrics using the input Scanner.
func New(s *lmsensors.Scanner) *Exporter {
return &Exporter{
s: s,
}
}
// Describe sends all the descriptors of the collectors included to
// the provided channel.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.withCollectors(func(cs []prometheus.Collector) {
for _, c := range cs {
c.Describe(ch)
}
})
}
// Collect sends the collected metrics from each of the collectors to
// prometheus.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.withCollectors(func(cs []prometheus.Collector) {
for _, c := range cs {
c.Collect(ch)
}
})
}
// withCollectors starts an lmsensors device scan and creates a set of prometheus
// collectors, invoking the input closure with the collectors to collect metrics.
func (e *Exporter) withCollectors(fn func(cs []prometheus.Collector)) {
devices, err := e.s.Scan()
if err != nil {
log.Printf("[ERROR] error scanning lmsensors devices: %v", err)
return
}
cs := []prometheus.Collector{
NewCurrentCollector(devices),
NewFanCollector(devices),
NewIntrusionCollector(devices),
NewTemperatureCollector(devices),
NewVoltageCollector(devices),
}
fn(cs)
}
// boolFloat64 converts a boolean into a float64 with a value of 1.0 if true, and
// 0.0 if false.
func boolFloat64(b bool) float64 {
if !b {
return 0.0
}
return 1.0
}