forked from mdlayher/lmsensors_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrusioncollector.go
74 lines (61 loc) · 1.56 KB
/
intrusioncollector.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
package lmsensorsexporter
import (
"github.com/mdlayher/lmsensors"
"github.com/prometheus/client_golang/prometheus"
)
// A IntrusionCollector is a Prometheus collector for lmsensors intrusion
// sensor metrics.
type IntrusionCollector struct {
Alarm *prometheus.Desc
devices []*lmsensors.Device
}
var _ prometheus.Collector = &IntrusionCollector{}
// NewIntrusionCollector creates a new IntrusionCollector.
func NewIntrusionCollector(devices []*lmsensors.Device) *IntrusionCollector {
const (
subsystem = "intrusion"
)
var (
labels = []string{"device", "sensor"}
)
return &IntrusionCollector{
devices: devices,
Alarm: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "alarm"),
"Whether or not an intrusion 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 *IntrusionCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.Alarm,
}
for _, d := range ds {
ch <- d
}
}
// Collect sends the metric values for each metric created by the IntrusionCollector
// to the provided prometheus Metric channel.
func (c *IntrusionCollector) Collect(ch chan<- prometheus.Metric) {
for _, d := range c.devices {
for _, s := range d.Sensors {
is, ok := s.(*lmsensors.IntrusionSensor)
if !ok {
continue
}
labels := []string{
d.Name,
is.Name,
}
ch <- prometheus.MustNewConstMetric(
c.Alarm,
prometheus.GaugeValue,
boolFloat64(is.Alarm),
labels...,
)
}
}
}