-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprom.go
400 lines (386 loc) · 13.9 KB
/
prom.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Prometheus Registry and Metrics
var (
reg = prometheus.NewRegistry()
metrics = promauto.With(reg)
//
// Exporter Metrics
expUpdateTime = metrics.NewHistogramVec(prometheus.HistogramOpts{
Name: "airos_exporter_update_time",
Help: "Time spent updating AirOS Statistics",
Buckets: prometheus.DefBuckets,
}, []string{"device"})
//
// AirOS Device Metrics
airosUptime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_device_uptime_s",
Help: "AirOS Device Uptime in Seconds",
}, []string{"device", "id", "model", "version", "mode", "net_role"})
airosPowerTime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_device_powertime_s",
Help: "Airos PowerTime in Seconds",
}, []string{"device"})
airosLoadAvg = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_device_load_avg",
Help: "Airos Load Average",
}, []string{"device"})
airosTemp = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_device_temp",
Help: "Airos Device Temperature",
}, []string{"device"})
airosRamUsage = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_device_ram_usage_percent",
Help: "Percent of RAM utilized",
}, []string{"device"})
//
// AirOS Interface Metrics
airosIfMTU = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_mtu",
Help: "Interface Maximum Transmission Unit",
}, []string{
"device",
"if_name",
"if_mac",
"if_ip",
"status",
"duplex",
})
airosIfBytes = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_bytes",
Help: "Interface Bytes Gauge",
}, []string{"device", "if_name", "direction"})
airosIfPkts = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_packets",
Help: "Interface Packets Gauge",
}, []string{"device", "if_name", "direction"})
airosIfErrors = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_errors",
Help: "Interface Errors Gauge",
}, []string{"device", "if_name", "direction"})
airosIfDropped = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_dropped",
Help: "Interface Packets Dropped Gauge",
}, []string{"device", "if_name", "direction"})
airosIfCblLen = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_cable_length_ft",
Help: "Interface Cable Length (ft)",
}, []string{"device", "if_name"})
airosIfCblSNR = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_if_cable_snr",
Help: "Interface Cable Signal-to-Noise Ratio",
}, []string{"device", "if_name"})
//
// AirOS Wireless Metrics
airosWLInfo = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_info",
Help: "Info table for AirOS Wireless Device",
}, []string{
"device",
"essid",
"ieeemode",
"band",
"ap_mac",
"dfs",
"security",
"ap_repeater",
})
airosWLNoiseFloor = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_noise_floor",
Help: "Noise Floor (dBm)",
}, []string{"device"})
airosWLAntennaGain = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_antenna_gain",
Help: "Antenna Gain (dBm)",
}, []string{"device"})
airosWLFreq = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_frequency",
Help: "Wireless Frequency (mHz)",
}, []string{"device"})
airosWLDistance = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_distance_ft",
Help: "Wireless Distance (ft)",
}, []string{"device"})
airosWLTXPower = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_tx_power_dbm",
Help: "Wireless Transmit Power (dBm)",
}, []string{"device"})
airosWLTput = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_throughput_kbps",
Help: "Wireless Transmit Throughput (kbps)",
}, []string{"device", "direction"})
airosWLSvcTime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_service_time_s",
Help: "Wireless Service Time (s)",
}, []string{"device"})
airosWLLinkTime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_link_time_s",
Help: "Wireless Link Time (s)",
}, []string{"device"})
airosWLServiceUptime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_service_uptime_perc",
Help: "Percent of time link is available",
}, []string{"device"})
airosWLCBCapacity = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_capacity_cb_kbit",
Help: "CB Capacity (mbit)",
}, []string{"device"})
airosWLDLCapacity = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_capacity_dl_kbit",
Help: "DL Capacity (mbit)",
}, []string{"device"})
airosWLULCapacity = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_capacity_ul_kbit",
Help: "UL Capacity (mbit)",
}, []string{"device"})
airosWLSTACount = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_wireless_station_count",
Help: "Count of Association Stations",
}, []string{"device"})
//
// AirOS Station Metrics
airosSTALabels = []string{
"device",
"remote_device",
}
airosSTAInfoLabels = []string{
"device",
"remote_device",
"remote_id",
"remote_model",
"remote_version",
"remote_mode",
"remote_net_role",
"remote_mac",
"remote_ip",
}
airosSTAInfo = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_info",
Help: "Info table for AirOS Remote Station",
}, airosSTAInfoLabels)
airosSTASignal = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_signal",
Help: "Station Signal",
}, airosSTALabels)
airosSTARSSI = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_rssi",
Help: "Station Received Signal Strength Indicator",
}, airosSTALabels)
airosSTANoiseFloor = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_noise_floor",
Help: "Station Noise Floor",
}, airosSTALabels)
airosSTATxLatency = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_tx_latency_ms",
Help: "Station Transmit Latency (ms)",
}, airosSTALabels)
airosSTAUptime = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_uptime_s",
Help: "Station Uptime (s)",
}, airosSTALabels)
airosSTABytes = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_bytes",
Help: "Station Bytes",
}, append(airosSTALabels, "direction"))
airosSTAPackets = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_packets",
Help: "Station Packets",
}, append(airosSTALabels, "direction"))
airosSTAPPS = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_pps",
Help: "Station Packets Per Second",
}, append(airosSTALabels, "direction"))
airosSTAPriority = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_priority",
Help: "Station Actual Priority",
}, airosSTALabels)
airosSTACBCap = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_cb_capacity",
Help: "Station CB Capacity",
}, airosSTALabels)
airosSTADLCap = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_dl_capacity",
Help: "Station DL Capacity",
}, airosSTALabels)
airosSTAULCap = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_ul_capacity",
Help: "Station UL Capacity",
}, airosSTALabels)
airosSTAUsage = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_chain_usage",
Help: "Station Chain Usage",
}, append(airosSTALabels, "direction"))
airosSTACINR = metrics.NewGaugeVec(prometheus.GaugeOpts{
Name: "airos_sta_cinr",
Help: "Station CINR (SNR)",
}, append(airosSTALabels, "direction"))
)
// Updates all Prometheus Metrics
func updatePromMetrics(s *airosStatus) {
device := s.Host.HostName
deviceLabels := prometheus.Labels{
"device": device,
"id": s.Host.DeviceID,
"model": s.Host.Model,
"version": s.Host.FWVersion,
"mode": s.Wireless.Mode,
"net_role": s.Host.NetRole,
}
var isRepater string
if s.Wireless.APRepeater {
isRepater = "true"
} else {
isRepater = "false"
}
wirelessLabels := prometheus.Labels{
"device": device,
"essid": s.Wireless.ESSID,
"ieeemode": s.Wireless.IEEEMode,
"band": fmt.Sprint(s.Wireless.Band),
"ap_mac": s.Wireless.APMac,
"dfs": fmt.Sprint(s.Wireless.DFS),
"security": s.Wireless.Security,
"ap_repeater": isRepater,
}
// Device Metrics
airosUptime.With(deviceLabels).Set(float64(s.Host.Uptime))
airosPowerTime.WithLabelValues(device).Set(float64(s.Host.PowerTime))
airosLoadAvg.WithLabelValues(device).Set(float64(s.Host.LoadAvg))
airosTemp.WithLabelValues(device).Set(float64(s.Host.Temperature))
// Calculate Percent RAM Used
ramUsagePerc := 1.0 - float64(s.Host.FreeRam)/float64(s.Host.TotalRam)
airosRamUsage.WithLabelValues(device).Set(ramUsagePerc)
// Interface Metrics
for _, i := range s.Interfaces {
// Check interface status
var status, duplex string
if i.Enabled {
status = "enabled"
} else {
status = "disabled"
}
if i.Status.Duplex {
duplex = "true"
} else {
duplex = "false"
}
// Set labels
ifLabels := prometheus.Labels{
"device": s.Host.HostName,
"if_name": i.IFName,
"if_mac": i.HWAddr,
"if_ip": i.Status.IPAddr,
"status": status,
"duplex": duplex,
}
ifLabelsTX := prometheus.Labels{
"device": s.Host.HostName,
"if_name": i.IFName,
"direction": "tx",
}
ifLabelsRX := prometheus.Labels{
"device": s.Host.HostName,
"if_name": i.IFName,
"direction": "rx",
}
airosIfMTU.With(ifLabels).Set(float64(i.MTU))
airosIfBytes.With(ifLabelsTX).Set(float64(i.Status.TXBytes))
airosIfBytes.With(ifLabelsRX).Set(float64(i.Status.RXBytes))
airosIfPkts.With(ifLabelsTX).Set(float64(i.Status.TXPackets))
airosIfPkts.With(ifLabelsRX).Set(float64(i.Status.RXPackets))
airosIfErrors.With(ifLabelsTX).Set(float64(i.Status.TXErrors))
airosIfErrors.With(ifLabelsRX).Set(float64(i.Status.RXErrors))
airosIfDropped.With(ifLabelsTX).Set(float64(i.Status.TXDropped))
airosIfDropped.With(ifLabelsRX).Set(float64(i.Status.RXDropped))
airosIfCblLen.WithLabelValues(s.Host.HostName, i.IFName).Set(float64(i.Status.CableLength))
// Average Cable SNR Values
if len(i.Status.SNR) > 0 {
var snrSum int
var snr float64
for _, v := range i.Status.SNR {
snrSum += v
}
snr = float64(snrSum) / float64(len(i.Status.SNR))
airosIfCblSNR.WithLabelValues(s.Host.HostName, i.IFName).Set(snr)
}
}
// Wireless Metrics
airosWLInfo.With(wirelessLabels).Set(1)
airosWLNoiseFloor.WithLabelValues(device).Set(float64(s.Wireless.NoiseFloor))
airosWLAntennaGain.WithLabelValues(device).Set(float64(s.Wireless.AntennaGain))
airosWLFreq.WithLabelValues(device).Set(float64(s.Wireless.Frequency))
airosWLDistance.WithLabelValues(device).Set(float64(s.Wireless.Distance))
airosWLTXPower.WithLabelValues(device).Set(float64(s.Wireless.TXPower))
airosWLTput.WithLabelValues(device, "tx").Set(float64(s.Wireless.Throughput.TX))
airosWLTput.WithLabelValues(device, "rx").Set(float64(s.Wireless.Throughput.RX))
airosWLSvcTime.WithLabelValues(device).Set(float64(s.Wireless.Service.Time))
airosWLLinkTime.WithLabelValues(device).Set(float64(s.Wireless.Service.Link))
// Calculate service availability ratio
svcUptime := 1.0 - ((float64(s.Wireless.Service.Time) - float64(s.Wireless.Service.Link)) / float64(s.Wireless.Service.Time))
airosWLServiceUptime.WithLabelValues(device).Set(svcUptime)
airosWLCBCapacity.WithLabelValues(device).Set(float64(s.Wireless.Polling.CBCapacity))
airosWLDLCapacity.WithLabelValues(device).Set(float64(s.Wireless.Polling.DLCapacity))
airosWLULCapacity.WithLabelValues(device).Set(float64(s.Wireless.Polling.ULCapacity))
airosWLSTACount.WithLabelValues(device).Set(float64(s.Wireless.STACount))
// Associated Station Metrics
for _, r := range s.Wireless.Stations {
// Common labels
staLabels := prometheus.Labels{
"device": s.Host.HostName,
"remote_device": r.Remote.HostName,
}
staLabelsTX := prometheus.Labels{
"device": s.Host.HostName,
"remote_device": r.Remote.HostName,
"direction": "tx",
}
staLabelsRX := prometheus.Labels{
"device": s.Host.HostName,
"remote_device": r.Remote.HostName,
"direction": "rx",
}
airosSTAInfo.With(prometheus.Labels{
"device": s.Host.HostName,
"remote_device": r.Remote.HostName,
"remote_id": r.Remote.DeviceID,
"remote_model": r.Remote.Platform,
"remote_version": r.Remote.Version,
"remote_mode": r.Remote.Mode,
"remote_net_role": r.Remote.NetRole,
"remote_mac": r.MAC,
"remote_ip": r.LastIP,
}).Set(1)
airosSTASignal.With(staLabels).Set(float64(r.Signal))
airosSTARSSI.With(staLabels).Set(float64(r.RSSI))
airosSTANoiseFloor.With(staLabels).Set(float64(r.NoiseFloor))
airosSTATxLatency.With(staLabels).Set(float64(r.TXLatency))
airosSTAUptime.With(staLabels).Set(float64(r.Uptime))
airosSTABytes.With(staLabelsTX).Set(float64(r.Stats.TXBytes))
airosSTABytes.With(staLabelsRX).Set(float64(r.Stats.RXBytes))
airosSTAPackets.With(staLabelsTX).Set(float64(r.Stats.TXPackets))
airosSTAPackets.With(staLabelsRX).Set(float64(r.Stats.RXPackets))
airosSTAPPS.With(staLabelsTX).Set(float64(r.Stats.TXPPS))
airosSTAPPS.With(staLabelsRX).Set(float64(r.Stats.RXPPS))
airosSTAPriority.With(staLabels).Set(float64(r.AirMax.ActualPriority))
airosSTACBCap.With(staLabels).Set(float64(r.AirMax.CBCapacity))
airosSTADLCap.With(staLabels).Set(float64(r.AirMax.DLCapacity))
airosSTAULCap.With(staLabels).Set(float64(r.AirMax.ULCapacity))
airosSTAUsage.With(staLabelsTX).Set(float64(r.AirMax.TX.Usage))
airosSTAUsage.With(staLabelsRX).Set(float64(r.AirMax.RX.Usage))
airosSTACINR.With(staLabelsTX).Set(float64(r.AirMax.TX.CINR))
airosSTACINR.With(staLabelsRX).Set(float64(r.AirMax.RX.CINR))
}
}
// Starts listener at listenAddr and services at /metrics
// Only serves custom registry
func servePromMetrics() {
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
log.WithField("ListenAddress", listenAddr).Info("Serving Prometheus at /metrics")
http.ListenAndServe(listenAddr, nil)
}