This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
290 lines (257 loc) · 8.3 KB
/
main.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
package main
import (
"encoding/base64"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
"github.com/gocolly/colly"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const namespace = "netgear_cm"
var (
version string
revision string
branch string
buildUser string
buildDate string
)
// Exporter represents an instance of the Netgear cable modem exporter.
type Exporter struct {
url, authHeaderValue string
mu sync.Mutex
// Exporter metrics.
totalScrapes prometheus.Counter
scrapeErrors prometheus.Counter
// Downstream metrics.
dsChannelSNR *prometheus.Desc
dsChannelPower *prometheus.Desc
dsChannelCorrectableErrs *prometheus.Desc
dsChannelUncorrectableErrs *prometheus.Desc
// Upstream metrics.
usChannelPower *prometheus.Desc
usChannelSymbolRate *prometheus.Desc
}
// basicAuth returns the base64 encoding of the username and password
// separated by a colon. Borrowed the net/http package.
func basicAuth(username, password string) string {
auth := fmt.Sprintf("%s:%s", username, password)
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// NewExporter returns an instance of Exporter configured with the modem's
// address, admin username and password.
func NewExporter(addr, username, password string) *Exporter {
var (
dsLabelNames = []string{"channel", "lock_status", "modulation", "channel_id", "frequency"}
usLabelNames = []string{"channel", "lock_status", "channel_type", "channel_id", "frequency"}
)
return &Exporter{
// Modem access details.
url: "http://" + addr + "/DocsisStatus.asp",
authHeaderValue: "Basic " + basicAuth(username, password),
// Collection metrics.
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "status_scrapes_total",
Help: "Total number of scrapes of the modem status page.",
}),
scrapeErrors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "status_scrape_errors_total",
Help: "Total number of failed scrapes of the modem status page.",
}),
// Downstream metrics.
dsChannelSNR: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "downstream_channel", "snr_db"),
"Downstream channel signal to noise ratio in dB.",
dsLabelNames, nil,
),
dsChannelPower: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "downstream_channel", "power_dbmv"),
"Downstream channel power in dBmV.",
dsLabelNames, nil,
),
dsChannelCorrectableErrs: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "downstream_channel", "correctable_errors_total"),
"Downstream channel correctable errors.",
dsLabelNames, nil,
),
dsChannelUncorrectableErrs: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "downstream_channel", "uncorrectable_errors_total"),
"Downstream channel uncorrectable errors.",
dsLabelNames, nil,
),
// Upstream metrics.
usChannelPower: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "upstream_channel", "power_dbmv"),
"Upstream channel power in dBmV.",
usLabelNames, nil,
),
usChannelSymbolRate: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "upstream_channel", "symbol_rate"),
"Upstream channel symbol rate per second",
usLabelNames, nil,
),
}
}
// Describe returns Prometheus metric descriptions for the exporter metrics.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// Exporter metrics.
ch <- e.totalScrapes.Desc()
ch <- e.scrapeErrors.Desc()
// Downstream metrics.
ch <- e.dsChannelSNR
ch <- e.dsChannelPower
ch <- e.dsChannelCorrectableErrs
ch <- e.dsChannelUncorrectableErrs
// Upstream metrics.
ch <- e.usChannelPower
ch <- e.usChannelSymbolRate
}
// Collect runs our scrape loop returning each Prometheus metric.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
c := colly.NewCollector()
// OnRequest callback adds basic auth header.
c.OnRequest(func(r *colly.Request) {
r.Headers.Add("Authorization", e.authHeaderValue)
})
// OnError callback counts any errors that occur during scraping.
c.OnError(func(r *colly.Response, err error) {
log.Printf("scrape failed: %d %s", r.StatusCode, http.StatusText(r.StatusCode))
e.scrapeErrors.Inc()
})
// Callback to parse the tbody block of table with id=dsTable, the downstream table info.
c.OnHTML(`#dsTable tbody`, func(elem *colly.HTMLElement) {
elem.DOM.Find("tr").Each(func(i int, row *goquery.Selection) {
if i == 0 {
return // no rows were returned
}
var (
channel string
lockStatus string
modulation string
channelID string
freqMHz string
snr float64
power float64
corrErrs float64
unCorrErrs float64
)
row.Find("td").Each(func(j int, col *goquery.Selection) {
text := strings.TrimSpace(col.Text())
switch j {
case 0:
channel = text
case 1:
lockStatus = text
case 2:
modulation = text
case 3:
channelID = text
case 4:
{
var freqHZ float64
fmt.Sscanf(text, "%f Hz", &freqHZ)
freqMHz = fmt.Sprintf("%0.2f MHz", freqHZ/1e6)
}
case 5:
fmt.Sscanf(text, "%f dBmV", &power)
case 6:
fmt.Sscanf(text, "%f dB", &snr)
case 7:
fmt.Sscanf(text, "%f", &corrErrs)
case 8:
fmt.Sscanf(text, "%f", &unCorrErrs)
}
})
labels := []string{channel, lockStatus, modulation, channelID, freqMHz}
ch <- prometheus.MustNewConstMetric(e.dsChannelSNR, prometheus.GaugeValue, snr, labels...)
ch <- prometheus.MustNewConstMetric(e.dsChannelPower, prometheus.GaugeValue, power, labels...)
ch <- prometheus.MustNewConstMetric(e.dsChannelCorrectableErrs, prometheus.CounterValue, corrErrs, labels...)
ch <- prometheus.MustNewConstMetric(e.dsChannelUncorrectableErrs, prometheus.CounterValue, unCorrErrs, labels...)
})
})
// Callback to parse the tbody block of table with id=usTable, the upstream channel info.
c.OnHTML(`#usTable tbody`, func(elem *colly.HTMLElement) {
elem.DOM.Find("tr").Each(func(i int, row *goquery.Selection) {
if i == 0 {
return // no rows were returned
}
var (
channel string
lockStatus string
channelType string
channelID string
symbolRate float64
freqMHz string
power float64
)
row.Find("td").Each(func(j int, col *goquery.Selection) {
text := strings.TrimSpace(col.Text())
switch j {
case 0:
channel = text
case 1:
lockStatus = text
case 2:
channelType = text
case 3:
channelID = text
case 4:
{
fmt.Sscanf(text, "%f Ksym/sec", &symbolRate)
symbolRate = symbolRate * 1000 // convert to sym/sec
}
case 5:
{
var freqHZ float64
fmt.Sscanf(text, "%f Hz", &freqHZ)
freqMHz = fmt.Sprintf("%0.2f MHz", freqHZ/1e6)
}
case 6:
fmt.Sscanf(text, "%f dBmV", &power)
}
})
labels := []string{channel, lockStatus, channelType, channelID, freqMHz}
ch <- prometheus.MustNewConstMetric(e.usChannelPower, prometheus.GaugeValue, power, labels...)
ch <- prometheus.MustNewConstMetric(e.usChannelSymbolRate, prometheus.GaugeValue, symbolRate, labels...)
})
})
e.mu.Lock()
c.Visit(e.url)
e.totalScrapes.Collect(ch)
e.scrapeErrors.Collect(ch)
e.mu.Unlock()
}
func main() {
var (
configFile = flag.String("config.file", "netgear_cm_exporter.yml", "Path to configuration file.")
showVersion = flag.Bool("version", false, "Print version information.")
)
flag.Parse()
if *showVersion {
fmt.Printf("netgear_cm_exporter version=%s revision=%s branch=%s buildUser=%s buildDate=%s\n",
version, revision, branch, buildUser, buildDate)
os.Exit(0)
}
config, err := NewConfigFromFile(*configFile)
if err != nil {
log.Fatal(err)
}
exporter := NewExporter(config.Modem.Address, config.Modem.Username, config.Modem.Password)
prometheus.MustRegister(exporter)
http.Handle(config.Telemetry.MetricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, config.Telemetry.MetricsPath, http.StatusMovedPermanently)
})
log.Printf("exporter listening on %s", config.Telemetry.ListenAddress)
if err := http.ListenAndServe(config.Telemetry.ListenAddress, nil); err != nil {
log.Fatalf("failed to start netgear exporter: %s", err)
}
}