-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
179 lines (145 loc) · 5.93 KB
/
collector.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
package exporter
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type Collector struct {
client *Client
apGroup string // WiFi AP Group name
ctx context.Context // HTTP request context
}
type PortalCollector struct {
client *Client
portal string // Guest access portal name
ctx context.Context // HTTP request context
}
var (
_ prometheus.Collector = (*Collector)(nil)
_ prometheus.Collector = (*PortalCollector)(nil)
)
const namespace = "cambium_maestro"
var (
ctrlUp = prometheus.NewDesc(namespace+"up", "indicator whether cloud controller is reachable", nil, nil)
groupLabels = []string{"name"} // used in groupDesc
groupDevicesCount = groupDesc("devices_count", "number of adopted devices")
groupDevicesOffline = groupDesc("devices_offline_count", "number of offline devices")
groupDevicesOutOfSync = groupDesc("devices_out_of_sync_count", "number of devices with old configuration")
groupClientCount = groupDesc("client_count", "number of currently connected clients")
groupClientCount24H = groupDesc("client_count_24h", "number of clients seen in the past 24 hours")
apLabels = []string{"apgroup", "mac"} // used in apDesc
apUp = apDesc("up", "details for AP", "model", "hostname", "serial", "site", "firmware")
apUptime = apDesc("uptime", "number of uptime seconds")
apDowntime = apDesc("downtime", "number of downtime seconds")
apReboot = apDesc("reboot", "number of seconds since last reboot", "reason")
radioLabels = []string{"apgroup", "ap", "band"} // used in radioDesc
radioChannel = radioDesc("channel", "WiFi channel number")
radioChannelWidth = radioDesc("channel_width", "WiFi channel width in MHz")
radioPower = radioDesc("power", "RF transmit power")
radioQuality = radioDesc("quality", "RF quality measurement in percentage points")
radioXfer = radioDesc("transfer_rate", "current traffic rate in bps", "direction")
portalSessions = prometheus.NewDesc(namespace+"_sessions_count", "number of active sessions", []string{"name"}, nil)
portalAPSessions = prometheus.NewDesc(namespace+"_ap_sessions_count", "number of active sessions", []string{"portal", "mac"}, nil)
)
func (*Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- ctrlUp
ch <- groupDevicesCount
ch <- groupDevicesOffline
ch <- groupDevicesOutOfSync
ch <- groupClientCount
ch <- groupClientCount24H
ch <- apUp
ch <- apUptime
ch <- apDowntime
ch <- apReboot
ch <- radioChannel
ch <- radioChannelWidth
ch <- radioPower
ch <- radioQuality
ch <- radioXfer
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.client.log.Debugf("collecting metrics for %s", c.apGroup)
metric := func(desc *prometheus.Desc, v float64, labels ...string) {
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, labels...)
}
intMetric := func(desc *prometheus.Desc, v int, labels ...string) {
metric(desc, float64(v), labels...)
}
now := time.Now()
timeMetric := func(desc *prometheus.Desc, v *time.Time, labels ...string) {
if v != nil {
metric(desc, now.Sub(*v).Seconds(), labels...)
}
}
group, err := c.client.fetchAPGroupData(c.ctx, c.apGroup)
if err != nil {
c.client.log.Errorf("fetching AP group data for %s failed with %v", c.apGroup, err)
metric(ctrlUp, 0)
return
}
devices, err := c.client.fetchDevices(c.ctx, c.apGroup)
if err != nil {
c.client.log.Errorf("fetching device data for %s failed with %v", c.apGroup, err)
metric(ctrlUp, 0)
return
}
metric(ctrlUp, 1)
name := group.Name
intMetric(groupDevicesCount, group.DevicesCount, name)
intMetric(groupDevicesOffline, group.DevicesOffline, name)
intMetric(groupDevicesOutOfSync, group.DevicesOutOfSync, name)
intMetric(groupClientCount, group.ClientCount, name)
intMetric(groupClientCount24H, group.ClientCount24H, name)
for _, dev := range devices {
mac := dev.MAC
metric(apUp, 1, name, mac, dev.Model, dev.Hostname, dev.Serial, dev.SiteName, dev.FirmwareVersion)
timeMetric(apUptime, dev.Uptime, name, mac)
timeMetric(apDowntime, dev.Downtime, name, mac)
timeMetric(apReboot, dev.LastRebootAt, name, mac, dev.RebootReason)
for _, r := range dev.Radios {
band := string(r.Band)
intMetric(radioChannel, r.Channel, name, mac, band)
intMetric(radioChannelWidth, r.ChannelWidth, name, mac, band)
intMetric(radioPower, r.Power, name, mac, band)
intMetric(radioQuality, r.Quality, name, mac, band)
// controller reports kBit/s, we export Bit/s
intMetric(radioXfer, r.Tx*1000, name, mac, band, "out")
intMetric(radioXfer, r.Rx*1000, name, mac, band, "in")
}
}
}
func (p *PortalCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- ctrlUp
ch <- portalSessions
ch <- portalAPSessions
}
func (c *PortalCollector) Collect(ch chan<- prometheus.Metric) {
c.client.log.Debugf("collecting metrics for portal %s", c.portal)
metric := func(desc *prometheus.Desc, v float64, labels ...string) {
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, labels...)
}
sessions, total, err := c.client.fetchPortalSessions(c.ctx, c.portal)
if err != nil {
c.client.log.Errorf("fetching portal data for %s failed with %v", c.portal, err)
metric(ctrlUp, 0)
return
}
metric(ctrlUp, 1)
metric(portalSessions, float64(total), c.portal)
for _, s := range sessions {
metric(portalAPSessions, float64(s.Sessions), c.portal, s.DeviceMAC)
}
}
func groupDesc(name, help string) *prometheus.Desc {
fqdn := prometheus.BuildFQName(namespace, "ap_group", name)
return prometheus.NewDesc(fqdn, help, groupLabels, nil)
}
func apDesc(name, help string, extraLabel ...string) *prometheus.Desc {
fqdn := prometheus.BuildFQName(namespace, "ap", name)
return prometheus.NewDesc(fqdn, help, append(apLabels, extraLabel...), nil)
}
func radioDesc(name, help string, extraLabel ...string) *prometheus.Desc {
fqdn := prometheus.BuildFQName(namespace, "ap_radio", name)
return prometheus.NewDesc(fqdn, help, append(radioLabels, extraLabel...), nil)
}