-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
221 lines (199 loc) · 6.04 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
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
package main
import (
"context"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Collector collects metrics from a remote ConnectBox router.
type Collector struct {
timeout time.Duration
targets map[string]ConnectBox
}
// NewCollector creates new collector.
func NewCollector(timeout time.Duration, targets map[string]ConnectBox) *Collector {
return &Collector{timeout: timeout, targets: targets}
}
// ServeHTTP handles requests from Prometheus. It collects all metrics,
// writes them to a temporary registry, and then returns.
func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
client, ok := c.targets[target]
if !ok {
http400(w, "Unknown target")
return
}
if err := client.Login(r.Context()); err != nil {
log.Printf("Failed to login: %v", err)
http500(w, "Collector error")
return
}
defer func() {
// Use a separate context to avoid cancelling logout when
// the request is cancelled
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
if err := client.Logout(ctx); err != nil {
log.Printf("Failed to logout: %v", err)
}
}()
// NOTE: Parallel requests are not possible due to how the auth system
// works - a new token is required for every request
reg := prometheus.NewRegistry()
c.collectCMSSystemInfo(r.Context(), reg, client)
c.collectLANUserTable(r.Context(), reg, client)
c.collectCMState(r.Context(), reg, client)
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func (c *Collector) collectCMSSystemInfo(
ctx context.Context,
reg *prometheus.Registry,
client ConnectBox,
) {
cmDocsisModeGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_docsis_mode",
Help: "DocSis mode.",
}, []string{"mode"})
cmHardwareVersionGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_hardware_version",
Help: "Hardware version.",
}, []string{"version"})
cmMacAddrGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_mac_addr",
Help: "MAC address.",
}, []string{"addr"})
cmSerialNumberGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_serial_number",
Help: "Serial number.",
}, []string{"sn"})
cmSystemUptimeGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_system_uptime",
Help: "System uptime.",
}, []string{})
cmNetworkAccessGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_cm_network_access",
Help: "Network access.",
}, []string{})
reg.MustRegister(cmDocsisModeGauge)
reg.MustRegister(cmHardwareVersionGauge)
reg.MustRegister(cmMacAddrGauge)
reg.MustRegister(cmSerialNumberGauge)
reg.MustRegister(cmSystemUptimeGauge)
reg.MustRegister(cmNetworkAccessGauge)
var data CMSystemInfo
err := client.Get(ctx, FnCMSystemInfo, &data)
if err != nil {
log.Printf("Failed to get CMSSystemInfo: %v", err)
return
}
cmDocsisModeGauge.WithLabelValues(data.DocsisMode).Set(1)
cmHardwareVersionGauge.WithLabelValues(data.HardwareVersion).Set(1)
cmMacAddrGauge.WithLabelValues(data.MacAddr).Set(1)
cmSerialNumberGauge.WithLabelValues(data.SerialNumber).Set(1)
cmSystemUptimeGauge.WithLabelValues().Set(float64(data.SystemUptime))
var val float64
if data.NetworkAccess == NetworkAccessAllowed {
val = 1
}
cmNetworkAccessGauge.WithLabelValues().Set(val)
}
func (c *Collector) collectLANUserTable(
ctx context.Context,
reg *prometheus.Registry,
client ConnectBox,
) {
clientGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_lan_client",
Help: "LAN client.",
}, []string{
"connection",
"interface",
"ipv4",
"hostname",
"mac",
})
reg.MustRegister(clientGauge)
var data LANUserTable
err := client.Get(ctx, FnLANUserTable, &data)
if err != nil {
log.Printf("Failed to get LANUserTable: %v", err)
return
}
for _, c := range data.Ethernet {
clientGauge.WithLabelValues(
"ethernet",
c.Interface,
c.IPv4Addr,
c.Hostname,
c.MACAddr,
).Set(1)
}
for _, c := range data.WIFI {
clientGauge.WithLabelValues(
"wifi",
c.Interface,
c.IPv4Addr,
c.Hostname,
c.MACAddr,
).Set(1)
}
}
func (c *Collector) collectCMState(
ctx context.Context,
reg *prometheus.Registry,
client ConnectBox,
) {
tunnerTemperatureGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_tunner_temperature",
Help: "Tunner temperature.",
}, []string{})
temperatureGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_temperature",
Help: "Temperature.",
}, []string{})
operStateGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_oper_state",
Help: "Operational state.",
}, []string{})
wanIPv4AddrGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_wan_ipv4_addr",
Help: "WAN IPv4 address.",
}, []string{"ip"})
wanIPv6AddrGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_wan_ipv6_addr",
Help: "WAN IPv6 address.",
}, []string{"ip"})
reg.MustRegister(tunnerTemperatureGauge)
reg.MustRegister(temperatureGauge)
reg.MustRegister(operStateGauge)
reg.MustRegister(wanIPv4AddrGauge)
reg.MustRegister(wanIPv6AddrGauge)
var data CMState
err := client.Get(ctx, FnCMState, &data)
if err != nil {
log.Printf("Failed to get CMState: %v", err)
return
}
tunnerTemperatureGauge.WithLabelValues().Set(float64(data.TunnerTemperature))
temperatureGauge.WithLabelValues().Set(float64(data.Temperature))
var val float64
if data.OperState == OperStateOK {
val = 1
}
operStateGauge.WithLabelValues().Set(val)
wanIPv4AddrGauge.WithLabelValues(data.WANIPv4Addr).Set(1)
for _, addr := range data.WANIPv6Addrs {
wanIPv6AddrGauge.WithLabelValues(addr).Set(1)
}
}
func http400(w http.ResponseWriter, resp string) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(resp)) //nolint:errcheck,gosec
}
func http500(w http.ResponseWriter, resp string) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(resp)) //nolint:errcheck,gosec
}