forked from mdlayher/unifi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarmcollector.go
126 lines (105 loc) · 2.93 KB
/
alarmcollector.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package unifiexporter
import (
"log"
"github.com/mdlayher/unifi"
"github.com/prometheus/client_golang/prometheus"
)
// AlarmCollector is a Prometheus collector for Unifi alarms
type AlarmCollector struct {
AlarmsTotal *prometheus.Desc
Alarms *prometheus.Desc
c *unifi.Client
sites []*unifi.Site
}
// Verify that the Exporter implements the collector interface.
var _ collector = &AlarmCollector{}
// NewAlarmCollector creates a new AlarmCollector
func NewAlarmCollector(c *unifi.Client, sites []*unifi.Site) *AlarmCollector {
const (
subsystem = "alarms"
)
var (
labelsSiteOnly = []string{"site"}
labelsAlarms = []string{"site", "id", "name", "mac", "key", "message", "subsytem"}
)
return &AlarmCollector{
AlarmsTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "total"),
"Total number of active alarms",
labelsSiteOnly,
nil,
),
Alarms: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", subsystem),
"Number of active alarms",
labelsAlarms,
nil,
),
c: c,
sites: sites,
}
}
// Describe sends the descriptors of each metric over to the provided channel.
// The corresponding metric values are sent separately.
func (c *AlarmCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.AlarmsTotal,
c.Alarms,
}
for _, d := range ds {
ch <- d
}
}
// collect begins a metrics collection task for all alarms
func (c *AlarmCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
for _, s := range c.sites {
alarms, err := c.c.Alarms(s.Name)
if err != nil {
return c.Alarms, err
}
ch <- prometheus.MustNewConstMetric(
c.AlarmsTotal,
prometheus.GaugeValue,
float64(len(alarms)),
s.Description,
)
c.collectAlarms(ch, s.Description, alarms)
}
return nil, nil
}
// collectAlarms collects the details for each alarm
func (c *AlarmCollector) collectAlarms(ch chan<- prometheus.Metric, siteLabel string, alarms []*unifi.Alarm) {
for _, alarm := range alarms {
labels := []string{
siteLabel,
alarm.ID,
alarm.APName,
alarm.APMAC.String(),
alarm.Key,
alarm.Message,
alarm.Subsystem,
}
ch <- prometheus.MustNewConstMetric(
c.Alarms,
prometheus.GaugeValue,
float64(len(alarms)),
labels...,
)
}
}
// Collect is the same as CollectError, but ignores any errors which occur.
// Collect exists to satisfy the prometheus.Collector interface.
func (c *AlarmCollector) Collect(ch chan<- prometheus.Metric) {
_ = c.CollectError(ch)
}
// CollectError sends the metric values for each metric pertaining to the global
// cluster usage over to the provided prometheus Metric channel, returning any
// errors which occur.
func (c *AlarmCollector) CollectError(ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil {
ch <- prometheus.NewInvalidMetric(desc, err)
log.Printf("[ERROR] failed collecting device metric %v: %v", desc, err)
return err
}
return nil
}