forked from mdlayher/unifi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstationcollector.go
257 lines (218 loc) · 6.13 KB
/
stationcollector.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package unifiexporter
import (
"log"
"github.com/mdlayher/unifi"
"github.com/prometheus/client_golang/prometheus"
)
// A StationCollector is a Prometheus collector for metrics regarding Ubiquiti
// UniFi stations (clients).
type StationCollector struct {
Stations *prometheus.Desc
ReceivedBytesTotal *prometheus.Desc
TransmittedBytesTotal *prometheus.Desc
ReceivedPacketsTotal *prometheus.Desc
TransmittedPacketsTotal *prometheus.Desc
RSSIDBM *prometheus.Desc
NoiseDBM *prometheus.Desc
c *unifi.Client
sites []*unifi.Site
}
// Verify that the Exporter implements the prometheus.Collector interface.
var _ collector = &StationCollector{}
// NewStationCollector creates a new StationCollector which collects metrics for
// a specified site.
func NewStationCollector(c *unifi.Client, sites []*unifi.Site) *StationCollector {
const (
subsystem = "stations"
)
var (
labelsSiteOnly = []string{"site"}
labelsStation = []string{
"site",
"id",
"ap_mac",
"station_mac",
"hostname",
"connection",
}
)
return &StationCollector{
Stations: prometheus.NewDesc(
// Subsystem is used as name so we get "unifi_stations"
prometheus.BuildFQName(namespace, "", subsystem),
"Total number of stations (clients)",
labelsSiteOnly,
nil,
),
ReceivedBytesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "received_bytes_total"),
"Number of bytes received by the AP for stations (client upload)",
labelsStation,
nil,
),
TransmittedBytesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "transmitted_bytes_total"),
"Number of bytes transmitted by the AP to stations (client download)",
labelsStation,
nil,
),
ReceivedPacketsTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "received_packets_total"),
"Number of packets received by the AP for stations (client upload)",
labelsStation,
nil,
),
TransmittedPacketsTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "transmitted_packets_total"),
"Number of packets transmitted by the AP for stations (client download)",
labelsStation,
nil,
),
RSSIDBM: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "rssi_dbm"),
"Current signal strength of stations",
labelsStation,
nil,
),
NoiseDBM: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "noise_dbm"),
"Current noise floor of stations",
labelsStation,
nil,
),
c: c,
sites: sites,
}
}
// collect begins a metrics collection task for all metrics related to UniFi
// stations.
func (c *StationCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
for _, s := range c.sites {
stations, err := c.c.Stations(s.Name)
if err != nil {
return c.Stations, err
}
ch <- prometheus.MustNewConstMetric(
c.Stations,
prometheus.GaugeValue,
float64(len(stations)),
s.Description,
)
c.collectStationBytes(ch, s.Description, stations)
c.collectStationSignal(ch, s.Description, stations)
}
return nil, nil
}
// hostName picks the more desirable of the two names available. It uses the Unifi-set name if provided,
// otherwise uses the host-provided name.
func hostName(s *unifi.Station) string {
if s.Name != "" {
return s.Name
}
return s.Hostname
}
// connType returns a string indicating if a station is connected using a wired
// or wireless connection.
func connType(s *unifi.Station) string {
if s.IsWired {
return "wired"
}
return "wireless"
}
// collectStationBytes collects receive and transmit byte counts for UniFi stations.
func (c *StationCollector) collectStationBytes(ch chan<- prometheus.Metric, siteLabel string, stations []*unifi.Station) {
for _, s := range stations {
labels := []string{
siteLabel,
s.ID,
s.APMAC.String(),
s.MAC.String(),
hostName(s),
connType(s),
}
ch <- prometheus.MustNewConstMetric(
c.ReceivedBytesTotal,
prometheus.CounterValue,
float64(s.Stats.ReceiveBytes),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.TransmittedBytesTotal,
prometheus.CounterValue,
float64(s.Stats.TransmitBytes),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.ReceivedPacketsTotal,
prometheus.CounterValue,
float64(s.Stats.ReceivePackets),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.TransmittedPacketsTotal,
prometheus.CounterValue,
float64(s.Stats.TransmitPackets),
labels...,
)
}
}
// collectStationSignal collects wireless signal strength for UniFi stations.
func (c *StationCollector) collectStationSignal(ch chan<- prometheus.Metric, siteLabel string, stations []*unifi.Station) {
for _, s := range stations {
if s.IsWired {
continue
}
labels := []string{
siteLabel,
s.ID,
s.APMAC.String(),
s.MAC.String(),
hostName(s),
connType(s),
}
ch <- prometheus.MustNewConstMetric(
c.RSSIDBM,
prometheus.GaugeValue,
float64(s.RSSI),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.NoiseDBM,
prometheus.GaugeValue,
float64(s.Noise),
labels...,
)
}
}
// Describe sends the descriptors of each metric over to the provided channel.
// The corresponding metric values are sent separately.
func (c *StationCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.Stations,
c.ReceivedBytesTotal,
c.TransmittedBytesTotal,
c.ReceivedPacketsTotal,
c.TransmittedPacketsTotal,
c.RSSIDBM,
c.NoiseDBM,
}
for _, d := range ds {
ch <- d
}
}
// Collect is the same as Collect, but ignores any errors which occur.
// Collect exists to satisfy the prometheus.Collector interface.
func (c *StationCollector) 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 *StationCollector) CollectError(ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil {
log.Printf("[ERROR] failed collecting station metric %v: %v", desc, err)
ch <- prometheus.NewInvalidMetric(desc, err)
return err
}
return nil
}