forked from MarkSchmitt/tc4400_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
381 lines (342 loc) · 11.6 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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
var (
channelLabelNames = []string{"channel"}
interfaceLabelNames = []string{"interface"}
)
func newChannelMetric(subsystemName, metricName, docString string, extraLabels ...string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, subsystemName, metricName), docString, append(channelLabelNames, extraLabels...), nil)
}
type metrics map[int]*prometheus.Desc
var (
targetUpMetric = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "up"), "Was the last scrape of TC4400 succesful.", nil, nil)
networkMetrics = metrics{
1: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "receive_bytes_total"), "", []string{"interface"}, nil),
2: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "receive_packets_total"), "", []string{"interface"}, nil),
3: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "receive_errs_total"), "", []string{"interface"}, nil),
4: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "receive_drop_total"), "", []string{"interface"}, nil),
5: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "transmit_bytes_total"), "", []string{"interface"}, nil),
6: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "transmit_packets_total"), "", []string{"interface"}, nil),
7: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "transmit_errs_total"), "", []string{"interface"}, nil),
8: prometheus.NewDesc(prometheus.BuildFQName(namespace, "network", "transmit_drop_total"), "", []string{"interface"}, nil),
}
downstreamChannelMetrics = metrics{
2: newChannelMetric("downstream", "locked", "Downstream Lock Status"),
3: newChannelMetric("downstream", "channel_type", "Downstream Channel Type", "type"),
4: newChannelMetric("downstream", "bonded", "Downstream Bonding Status"),
5: newChannelMetric("downstream", "center_frequency_hz", "Downstream Center Frequency"),
6: newChannelMetric("downstream", "width_hz", "Downstream Width"),
7: newChannelMetric("downstream", "snr_threshold_db", "Downstream SNR/MER Threshold Value"),
8: newChannelMetric("downstream", "receive_level_dbmv", "Downstream Receive Level"),
9: newChannelMetric("downstream", "modulation", "Downstream Modulation/Profile ID", "modulation"),
10: newChannelMetric("downstream", "codewords_unerrored_total", "Downstream Unerrored Codewords"),
11: newChannelMetric("downstream", "codewords_corrected_total", "Downstream Corrected Codewords"),
12: newChannelMetric("downstream", "codewords_uncorrectable_total", "Downstream Uncorrectable Codewords"),
13: newChannelMetric("downstream", "modulation_clear", "Downstream Modulation in clear value"),
}
upstreamChannelMetrics = metrics{
2: newChannelMetric("upstream", "locked", "Upstream Lock Status"),
3: newChannelMetric("upstream", "channel_type", "Downstream Channel Type", "type"),
4: newChannelMetric("upstream", "bonded", "Upstream Bonding Status"),
5: newChannelMetric("upstream", "center_frequency_hz", "Upstream Center Frequency"),
6: newChannelMetric("upstream", "width_hz", "Upstream Width"),
7: newChannelMetric("upstream", "transmit_level_dbmv", "Upstream Transmit Level"),
8: newChannelMetric("upstream", "modulation", "Upstream Modulation/Profile ID", "modulation"),
}
)
type Exporter struct {
baseURL string
client *http.Client
mutex sync.RWMutex
totalScrapes prometheus.Counter
parseFailures *prometheus.CounterVec
clientRequestCount *prometheus.CounterVec
clientRequestDuration *prometheus.HistogramVec
}
func NewExporter(uri string, timeout time.Duration) (*Exporter, error) {
client := &http.Client{}
client.Timeout = timeout
clientRequestCount := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_client_requests_total",
Help: "HTTP requests to TC4400",
}, []string{"code", "method"})
clientRequestDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "exporter_client_request_duration_seconds",
Help: "Histogram of TC4400 HTTP request latencies.",
}, []string{"code", "method"})
client.Transport = promhttp.InstrumentRoundTripperCounter(clientRequestCount,
promhttp.InstrumentRoundTripperDuration(clientRequestDuration, http.DefaultTransport))
return &Exporter{
baseURL: uri,
client: client,
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrapes_total",
Help: "Current total TC4400 scrapes.",
}),
parseFailures: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_parse_errors_total",
Help: "Number of errors while parsing HTML tables.",
}, []string{"file"}),
clientRequestCount: clientRequestCount,
clientRequestDuration: clientRequestDuration,
}, nil
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range networkMetrics {
ch <- m
}
for _, m := range downstreamChannelMetrics {
ch <- m
}
for _, m := range upstreamChannelMetrics {
ch <- m
}
ch <- targetUpMetric
ch <- e.totalScrapes.Desc()
e.parseFailures.Describe(ch)
e.clientRequestCount.Describe(ch)
e.clientRequestDuration.Describe(ch)
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
up := e.scrape(ch)
ch <- prometheus.MustNewConstMetric(targetUpMetric, prometheus.GaugeValue, up)
ch <- e.totalScrapes
e.parseFailures.Collect(ch)
e.clientRequestCount.Collect(ch)
e.clientRequestDuration.Collect(ch)
}
func (e *Exporter) fetch(filename string) (io.ReadCloser, error) {
u, err := url.Parse(e.baseURL)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, filename)
resp, err := e.client.Get(u.String())
if err != nil {
return nil, err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
resp.Body.Close()
return nil, fmt.Errorf("Scraping %s failed: HTTP status %d", u.String(), resp.StatusCode)
}
return resp.Body, nil
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
e.totalScrapes.Inc()
// networkMetrics - statsifc.html
body, err := e.fetch("statsifc.html")
if err == nil {
tables, err := parseTables(body)
body.Close()
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("statsifc.html").Inc()
} else {
if len(tables) < 1 || len(tables[0]) < 2 {
log.Errorln("No table found in statsifc.html")
e.parseFailures.WithLabelValues("statsifc.html").Inc()
} else {
for _, row := range tables[0][2:] {
if len(row) != 9 {
continue
}
for i, metric := range networkMetrics {
valueInt, err := strconv.ParseInt(row[i], 10, 64)
value := float64(valueInt)
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("statsifc.html").Inc()
continue
}
ch <- prometheus.MustNewConstMetric(metric, prometheus.CounterValue, value, row[0])
}
}
}
}
}
// upstreamChannelMetrics, downstreamChannelMetrics - cmconnectionstatus.html
body, err = e.fetch("cmconnectionstatus.html")
if err == nil {
tables, err := parseTables(body)
body.Close()
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
} else {
if len(tables) < 3 || len(tables[1]) < 2 || len(tables[2]) < 2 {
log.Errorln("Tables not found in cmconnectionstatus.html")
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
} else {
// downstreamChannelMetrics
for _, row := range tables[1][2:] {
if len(row) != 13 {
continue
}
channel, err := strconv.ParseInt(row[1], 10, 64)
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
continue
}
channelLabel := fmt.Sprintf("%02d", channel)
for i, metric := range downstreamChannelMetrics {
var err error = nil
var value float64
var valueInt int64
var labelValues = []string{channelLabel}
switch i {
case 10, 11, 12:
valueInt, err = strconv.ParseInt(row[i], 10, 64)
value = float64(valueInt)
case 2:
if row[i] == "Locked" {
value = 1
} else {
value = 0
}
case 3, 9:
labelValues = append(labelValues, row[i])
value = 1
case 4:
if row[i] == "Bonded" {
value = 1
} else {
value = 0
}
case 5, 6:
parts := strings.Split(row[i], " ")
if len(parts) != 2 {
continue
}
valueInt, err = strconv.ParseInt(parts[0], 10, 64)
switch parts[1] {
case "Hz":
case "kHz":
valueInt = valueInt * 1000
default:
continue
}
value = float64(valueInt)
case 7:
parts := strings.Split(row[i], " ")
if len(parts) != 2 || parts[1] != "dB" {
continue
}
value, err = strconv.ParseFloat(parts[0], 64)
case 8:
parts := strings.Split(row[i], " ")
if len(parts) != 2 || parts[1] != "dBmV" {
continue
}
value, err = strconv.ParseFloat(parts[0], 64)
case 13:
numval := strings.Replace(row[9], "QAM", "", -1)
ofdmK := false
if (row[3] == "OFDM") {
numval = "0" // random value as OFDM
ofdmK = true
}
convval, _ := strconv.ParseInt(numval, 10, 0)
if (ofdmK) {
convval = convval * 1024
}
value = float64(convval)
default:
continue
}
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
continue
}
ch <- prometheus.MustNewConstMetric(metric, prometheus.CounterValue, value, labelValues...)
}
}
// upstreamChannelMetrics
for _, row := range tables[2][2:] {
if len(row) != 9 {
continue
}
channel, err := strconv.ParseInt(row[1], 10, 64)
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
continue
}
channelLabel := fmt.Sprintf("%02d", channel)
for i, metric := range upstreamChannelMetrics {
var err error = nil
var value float64
var valueInt int64
var labelValues = []string{channelLabel}
switch i {
case 2:
if row[i] == "Locked" {
value = 1
} else {
value = 0
}
case 3, 8:
labelValues = append(labelValues, row[i])
value = 1
case 4:
if row[i] == "Bonded" {
value = 1
} else {
value = 0
}
case 5, 6:
parts := strings.Split(row[i], " ")
if len(parts) != 2 {
continue
}
valueInt, err = strconv.ParseInt(parts[0], 10, 64)
switch parts[1] {
case "Hz":
case "kHz":
valueInt = valueInt * 1000
default:
continue
}
value = float64(valueInt)
case 7:
parts := strings.Split(row[i], " ")
if len(parts) != 2 || parts[1] != "dBmV" {
continue
}
value, err = strconv.ParseFloat(parts[0], 64)
default:
continue
}
if err != nil {
log.Errorln(err)
e.parseFailures.WithLabelValues("cmconnectionstatus.html").Inc()
continue
}
ch <- prometheus.MustNewConstMetric(metric, prometheus.CounterValue, value, labelValues...)
}
}
}
}
}
return 1
}