-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.go
523 lines (463 loc) · 15.1 KB
/
exporter.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// arris_cm_exporter, a Prometheus exporter for Arris Cable Modems
// Copyright 2021 Mark Stenglein
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/tls"
b64 "encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
type DownstreamChannel struct {
ChannelID string // Channel identifier (string)
LockStatus float64 // Whether the channel is locked or not (boolean)
Modulation string // Type of modulation used by channel
Frequency string // Frequency the channel is operating on (Hz)
Power float64 // Power level (dBmV)
SNR float64 // SNR/MER (dB)
CorrectedErrors float64 // Counter, resets to 0 on modem reboot (n)
UncorrectableErrors float64 // Counter, resets to 0 on modem reboot (n)
}
type UpstreamChannel struct {
Channel string // Channel Number (string)
ChannelID string // Channel ID (string)
LockStatus float64 // Whether the channel is locked or not (boolean)
USChannelType string // Upstream channel modulation
Frequency string // Frequency the channel is operating on (Hz)
Width string // Channel width (Hz)
Power float64 // Power level (dBmV)
}
type ArrisModem struct {
Host string // Hostname or network address of SB8200 modem
ConnectivityState float64 // Is the modem connected to upstream provider (boolean)
Uptime float64 // From product info page, Uptime (Seconds)
HardwareVersion string // From product info page
SoftwareVersion string // From product info page
MACAddress string // From product info page
SerialNumber string // From product info page
DownstreamBondedChannels []DownstreamChannel // From status page, array of channels
UpstreamBondedChannels []UpstreamChannel // From status page, array of channels
}
type Exporter struct {
Host string // Hostname or network address of SB8200 modem
AuthToken string // b64 encoded username:password
}
func NewExporter(host string, user string, pass string) *Exporter {
return &Exporter{
Host: host,
AuthToken: b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, pass))),
}
}
// Log into the web interface and return sessionID and csrf token
func (e *Exporter) Login() (sessionID *http.Cookie, csrfToken string, err error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/logout.html", e.Host), nil)
if err != nil {
return
}
logoutResp, err := client.Do(req)
if err != nil {
return
}
defer logoutResp.Body.Close()
url := fmt.Sprintf("https://%s/cmconnectionstatus.html?login_%s", e.Host, e.AuthToken)
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
return
}
csrfToken = string(body)
for _, cookie := range resp.Cookies() {
// The server will set the sessionID to "" whenever it wants to
// force and signal the end of a session.
if cookie.Name == "sessionId" && cookie.Value != "" {
sessionID = cookie
return
}
}
err = errors.New("missing sessionID")
return
}
if resp.StatusCode == http.StatusUnauthorized {
err = errors.New("invalid credentials")
return
}
err = errors.New("unknown error/response code")
return
}
func ScrapeColStr(element *goquery.Selection, child int) string {
selectString := fmt.Sprintf("td:nth-child(%d)", child)
return element.Find(selectString).First().Text()
}
func ScrapeUnitValue(element *goquery.Selection, child int, trim string) (float64, error) {
valStr := strings.TrimRight(ScrapeColStr(element, child), trim)
valFloat, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return 0, err
}
return valFloat, nil
}
func ScrapeDownstreamTableRow(element *goquery.Selection) (downstreamChannel DownstreamChannel, err error) {
// Skip first row (that shows header values)
if ScrapeColStr(element, 1) == "Channel ID" {
err = errors.New("skip parsing second header row")
return
}
lockStatus := 0.
if ScrapeColStr(element, 2) == "Locked" {
lockStatus = 1.
}
power, err := ScrapeUnitValue(element, 5, " dBmV")
if err != nil {
return
}
snr, err := ScrapeUnitValue(element, 6, " dB")
if err != nil {
return
}
correctedErrors, err := ScrapeUnitValue(element, 7, "")
if err != nil {
return
}
uncorrectableErrors, err := ScrapeUnitValue(element, 8, "")
if err != nil {
return
}
downstreamChannel = DownstreamChannel{
ChannelID: ScrapeColStr(element, 1),
LockStatus: lockStatus,
Modulation: ScrapeColStr(element, 3),
Frequency: ScrapeColStr(element, 4),
Power: power,
SNR: snr,
CorrectedErrors: correctedErrors,
UncorrectableErrors: uncorrectableErrors,
}
return
}
func ScrapeDownstreamTable(element *goquery.Selection) (downstreamChannels []DownstreamChannel) {
element.Each(func(index int, element *goquery.Selection) {
parsedRow, err := ScrapeDownstreamTableRow(element)
if err != nil {
log.Debug(err)
return
}
downstreamChannels = append(downstreamChannels, parsedRow)
})
return
}
func ScrapeUpstreamTableRow(element *goquery.Selection) (upstreamChannel UpstreamChannel, err error) {
// Skip first row (that shows header values)
if firstVal := ScrapeColStr(element, 1); firstVal == "Channel" || firstVal == "" {
err = errors.New("skip first two header row")
return
}
lockStatus := 0.
if ScrapeColStr(element, 3) == "Locked" {
lockStatus = 1.
}
power, err := ScrapeUnitValue(element, 7, " dBmV")
if err != nil {
return
}
upstreamChannel = UpstreamChannel{
Channel: ScrapeColStr(element, 1),
ChannelID: ScrapeColStr(element, 2),
LockStatus: lockStatus,
USChannelType: ScrapeColStr(element, 4),
Frequency: ScrapeColStr(element, 5),
Width: ScrapeColStr(element, 6),
Power: power,
}
return
}
func ScrapeUpstreamTable(element *goquery.Selection) (upstreamChannels []UpstreamChannel) {
element.Each(func(index int, element *goquery.Selection) {
parsedRow, err := ScrapeUpstreamTableRow(element)
if err != nil {
log.Debug(err)
return
}
upstreamChannels = append(upstreamChannels, parsedRow)
})
return
}
func GetURL(url string, sessionID *http.Cookie) (document *goquery.Document, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return
}
req.AddCookie(sessionID)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
document, err = goquery.NewDocumentFromReader(resp.Body)
return
}
// Scrape the web page for metric data
func (e *Exporter) Scrape() (modem ArrisModem, err error) {
sessionID, csrfToken, err := e.Login()
if err != nil {
log.Error("Failed to fetch login tokens")
return
}
url := fmt.Sprintf("https://%s/cmconnectionstatus.html?ct_%s", e.Host, csrfToken)
document, err := GetURL(url, sessionID)
if err != nil {
log.Error("Failed to fetch connection status url")
return
}
connectivityStateSelector := ".content > center:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(2)"
connectivityState := 0.
if document.Find(connectivityStateSelector).First().Text() == "OK" {
connectivityState = 1.
}
var downstreamChannels []DownstreamChannel
var upstreamChannels []UpstreamChannel
document.Find("table").Each(func(i int, element *goquery.Selection) {
switch i {
case 1:
downstreamChannels = ScrapeDownstreamTable(element.Find("tr"))
case 2:
upstreamChannels = ScrapeUpstreamTable(element.Find("tr"))
}
})
url = fmt.Sprintf("https://%s/cmswinfo.html?ct_%s", e.Host, csrfToken)
document, err = GetURL(url, sessionID)
if err != nil {
log.Error("Failed to fetch product information page")
return
}
hwVerSelector := "table.simpleTable:nth-child(2) > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2)"
hwVersion := document.Find(hwVerSelector).First().Text()
swVerSelector := "table.simpleTable:nth-child(2) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(2)"
swVersion := document.Find(swVerSelector).First().Text()
macAddrSelector := "table.simpleTable:nth-child(2) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(2)"
macAddress := document.Find(macAddrSelector).First().Text()
serialSelector := "table.simpleTable:nth-child(2) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(2)"
serial := document.Find(serialSelector).First().Text()
uptimeSelector := "table.simpleTable:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2)"
// uptimeStr will look like: 40 days 05h:32m:52s.00
uptimeStr := document.Find(uptimeSelector).First().Text()
// parts will look like ["40" "05" "32" "52" "00"]
uptimeParts := regexp.MustCompile(`\D+`).Split(uptimeStr, -1)
uptime := 0.
for i, nStr := range uptimeParts {
var n float64
n, err = strconv.ParseFloat(nStr, 64)
if err != nil {
return
}
switch i {
case 0: // days
uptime = n
case 1: // hours
uptime = uptime*24 + n
case 2: // minutes
uptime = uptime*60 + n
case 3: // seconds
uptime = uptime*60 + n
} // ignore milliseconds
}
modem = ArrisModem{
Host: e.Host,
ConnectivityState: connectivityState,
Uptime: uptime,
HardwareVersion: hwVersion,
SoftwareVersion: swVersion,
MACAddress: macAddress,
SerialNumber: serial,
DownstreamBondedChannels: downstreamChannels,
UpstreamBondedChannels: upstreamChannels,
}
return
}
const (
namespace = "sb8200"
DOWNSTREAM = "downstream"
UPSTREAM = "upstream"
)
var (
// Metrics
upMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the last data scrape successful?",
[]string{"host"}, nil,
)
connectedMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "connected"),
"Is the modem's connection up (connectivity state)?",
[]string{"host"}, nil,
)
uptimeMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "uptime_seconds"),
"Uptime",
[]string{"host"}, nil,
)
infoMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "info"),
"Metadata about this modem.",
[]string{"host", "hwversion", "swversion", "mac", "serial"},
nil,
)
channelLockMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "lock"),
"Is the downstream channel locked?",
[]string{"host", "channel_id", "type"}, nil,
)
channelPowerMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "power"),
"Power level (dBmV)",
[]string{"host", "channel_id", "type"}, nil,
)
channelSNRMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "snr"),
"SNR/MER rate (dB)",
[]string{"host", "channel_id", "type"}, nil,
)
channelCorrectedMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "corrected_total"),
"Corrected errors, counter resets to 0 on modem reboot",
[]string{"host", "channel_id", "type"}, nil,
)
channelUncorrectableMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "uncorrectable_total"),
"Uncorrectable errors, counter resets to 0 on modem reboot",
[]string{"host", "channel_id", "type"}, nil,
)
channelInfoMetric = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "channel", "info"),
"Channel metadata",
[]string{"host", "channel_id", "modulation", "frequency", "width", "type"}, nil,
)
)
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- upMetric
ch <- connectedMetric
ch <- uptimeMetric
ch <- infoMetric
ch <- channelLockMetric
ch <- channelPowerMetric
ch <- channelSNRMetric
ch <- channelCorrectedMetric
ch <- channelUncorrectableMetric
ch <- channelInfoMetric
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
modem, err := e.Scrape()
if err != nil {
ch <- prometheus.MustNewConstMetric(
upMetric, prometheus.GaugeValue, 0,
)
log.Error(err)
return
}
ch <- prometheus.MustNewConstMetric(
upMetric, prometheus.GaugeValue, 1,
)
// Connected Metric
ch <- prometheus.MustNewConstMetric(
connectedMetric, prometheus.GaugeValue, modem.ConnectivityState,
)
// Uptime Metric
ch <- prometheus.MustNewConstMetric(
uptimeMetric, prometheus.GaugeValue, modem.Uptime,
)
// Modem Meta Metric
ch <- prometheus.MustNewConstMetric(
infoMetric, prometheus.GaugeValue, 1,
e.Host, modem.HardwareVersion, modem.SoftwareVersion,
modem.MACAddress, modem.SerialNumber,
)
// Downstream Channels
for _, channel := range modem.DownstreamBondedChannels {
// Lock Metric
ch <- prometheus.MustNewConstMetric(
channelLockMetric, prometheus.GaugeValue, channel.LockStatus,
channel.ChannelID, DOWNSTREAM,
)
// Power Metric
ch <- prometheus.MustNewConstMetric(
channelPowerMetric, prometheus.GaugeValue, channel.Power,
channel.ChannelID, DOWNSTREAM,
)
// SNR Metric
ch <- prometheus.MustNewConstMetric(
channelSNRMetric, prometheus.GaugeValue, channel.SNR,
channel.ChannelID, DOWNSTREAM,
)
// Corrected Errors Metric
ch <- prometheus.MustNewConstMetric(
channelCorrectedMetric, prometheus.CounterValue, channel.CorrectedErrors,
channel.ChannelID, DOWNSTREAM,
)
// Uncorrectable Errors Metric
ch <- prometheus.MustNewConstMetric(
channelUncorrectableMetric, prometheus.CounterValue, channel.UncorrectableErrors,
channel.ChannelID, DOWNSTREAM,
)
// Meta Metric
ch <- prometheus.MustNewConstMetric(
channelInfoMetric, prometheus.GaugeValue, 1,
channel.ChannelID, channel.Modulation, channel.Frequency,
"", DOWNSTREAM,
)
}
// Upstream Channels
for _, channel := range modem.UpstreamBondedChannels {
// Lock Metric
ch <- prometheus.MustNewConstMetric(
channelLockMetric, prometheus.GaugeValue, channel.LockStatus,
channel.ChannelID, UPSTREAM,
)
// Power Metric
ch <- prometheus.MustNewConstMetric(
channelPowerMetric, prometheus.GaugeValue, channel.Power,
channel.ChannelID, UPSTREAM,
)
// Meta Metric
ch <- prometheus.MustNewConstMetric(
channelInfoMetric, prometheus.GaugeValue, 1,
channel.ChannelID, channel.USChannelType, channel.Frequency,
channel.Width, UPSTREAM,
)
}
}