-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
299 lines (251 loc) · 9.54 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
291
292
293
294
295
296
297
298
299
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type ipv4Stats struct {
ReceivePackets int
TransmitPackets int
ReceiveBytes int
TransmitBytes int
ReceiveUnicast int
TransmitUnicast int
ReceiveMulticast int
TransmitMulticast int
ReceiveDrops int
TransmitDrops int
ReceiveErrors int
TransmitErrors int
Collisions int
}
func recordMetrics(router_addr *string) {
go func() {
var ipv4Stats, last_ipv4Stats ipv4Stats
for {
probeCounter.Inc()
response, err := http.Get(fmt.Sprintf("http://%s/cgi-bin/broadbandstatistics.ha", *router_addr))
if err != nil {
probeSuccessGauge.Set(0)
log.Printf("request failed: %s\n", err)
response.Body.Close()
continue
} else {
probeSuccessGauge.Set(1)
}
probeStatusCodeGauge.Set(float64(response.StatusCode))
if response.StatusCode != 200 {
probeSuccessGauge.Set(0)
log.Printf("status code error: %d %s\n", response.StatusCode, response.Status)
response.Body.Close()
continue
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Printf("failed to query doc: %s\n", err)
response.Body.Close()
continue
}
fmt.Println("ipv4")
// Find the review items
doc.Find("#content-sub table[summary=\"Ethernet IPv4 Statistics Table\"] tbody tr").Each(func(i int, s *goquery.Selection) {
// For each item found, get the field name and the data
label := s.Find("th").Text()
value := s.Find("td").Text()
fmt.Printf("found %d: %s = %s\n", i, label, value)
switch label {
case "Receive Packets":
ipv4Stats.ReceivePackets, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceivePackets - last_ipv4Stats.ReceivePackets
attBroadbandIpv4RxPackets.Add(float64(change))
case "Transmit Packets":
ipv4Stats.TransmitPackets, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitPackets - last_ipv4Stats.TransmitPackets
attBroadbandIpv4TxPackets.Add(float64(change))
case "Receive Bytes":
ipv4Stats.ReceiveBytes, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceiveBytes - last_ipv4Stats.ReceiveBytes
attBroadbandIpv4RxBytes.Add(float64(change))
case "Transmit Bytes":
ipv4Stats.TransmitBytes, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitBytes - last_ipv4Stats.TransmitBytes
attBroadbandIpv4TxBytes.Add(float64(change))
case "Receive Unicast":
ipv4Stats.ReceiveUnicast, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceiveUnicast - last_ipv4Stats.ReceiveUnicast
attBroadbandIpv4RxUnicast.Add(float64(change))
case "Transmit Unicast":
ipv4Stats.TransmitUnicast, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitUnicast - last_ipv4Stats.TransmitUnicast
attBroadbandIpv4TxUnicast.Add(float64(change))
case "Receive Multicast":
ipv4Stats.ReceiveMulticast, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceiveMulticast - last_ipv4Stats.ReceiveMulticast
attBroadbandIpv4RxMulticast.Add(float64(change))
case "Transmit Multicast":
ipv4Stats.TransmitMulticast, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitMulticast - last_ipv4Stats.TransmitMulticast
attBroadbandIpv4TxMulticast.Add(float64(change))
case "Receive Drops":
ipv4Stats.ReceiveDrops, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceiveDrops - last_ipv4Stats.ReceiveDrops
attBroadbandIpv4RxDrops.Add(float64(change))
case "Transmit Drops":
ipv4Stats.TransmitDrops, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitDrops - last_ipv4Stats.TransmitDrops
attBroadbandIpv4TxDrops.Add(float64(change))
case "Receive Errors":
ipv4Stats.ReceiveErrors, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.ReceiveErrors - last_ipv4Stats.ReceiveErrors
attBroadbandIpv4RxErrors.Add(float64(change))
case "Transmit Errors":
ipv4Stats.TransmitErrors, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.TransmitErrors - last_ipv4Stats.TransmitErrors
attBroadbandIpv4TxErrors.Add(float64(change))
case "Collisions":
ipv4Stats.Collisions, err = strconv.Atoi(value)
if err != nil {
log.Printf("failed to convert value to integer: %s\n", err)
}
change := ipv4Stats.Collisions - last_ipv4Stats.Collisions
attBroadbandIpv4Collisions.Add(float64(change))
}
})
last_ipv4Stats = ipv4Stats
time.Sleep(5 * time.Second)
}
}()
}
var (
listen_addr = flag.String("listen-address", "0.0.0.0:9085", "The address to listen on for HTTP requests.")
router_addr = flag.String("router-address", "192.168.1.254", "The address of the router to scrape")
deviceName = "Arris BGW210-700"
metricsPath = "/metrics"
probeCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "probe_total",
Help: "The total number of probe requests",
})
probeSuccessGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "probe_success",
Help: "Displays whether or not the probe was a success",
})
probeStatusCodeGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "probe_status_code",
Help: "HTTP response status code, where 200 is normal success",
})
attBroadbandIpv4RxPackets = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_packets",
Help: "The total number of packets received on the wan interface",
})
attBroadbandIpv4TxPackets = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_packets",
Help: "The total number of packets transmitted on the wan interface",
})
attBroadbandIpv4RxBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_bytes",
Help: "The total number of bytes received on the wan interface",
})
attBroadbandIpv4TxBytes = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_bytes",
Help: "The total number of bytes transmitted on the wan interface",
})
attBroadbandIpv4RxUnicast = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_unicast",
Help: "The total number of unicast packets received on the wan interface",
})
attBroadbandIpv4TxUnicast = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_unicast",
Help: "The total number of unicast packets transmitted on the wan interface",
})
attBroadbandIpv4RxMulticast = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_multicast",
Help: "The total number of multicast packets received on the wan interface",
})
attBroadbandIpv4TxMulticast = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_multicast",
Help: "The total number of multicast packets transmitted on the wan interface",
})
attBroadbandIpv4RxDrops = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_drops",
Help: "The total number of dropped packets received on the wan interface",
})
attBroadbandIpv4TxDrops = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_drops",
Help: "The total number of dropped packets transmitted on the wan interface",
})
attBroadbandIpv4RxErrors = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_receive_errors",
Help: "The total number of errors received on the wan interface",
})
attBroadbandIpv4TxErrors = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_transmit_errors",
Help: "The total number of errors transmitted on the wan interface",
})
attBroadbandIpv4Collisions = promauto.NewCounter(prometheus.CounterOpts{
Name: "att_broadband_ipv4_collisions",
Help: "The total number of collisions on the wan interface",
})
)
func main() {
flag.Parse()
//prometheus.MustRegister(probeSuccessGauge)
//prometheus.MustRegister(probeStatusCodeGauge)
// prometheus.MustRegister(attBroadbandIpv4RxPackets)
recordMetrics(router_addr)
http.HandleFunc("/", defaultPageHandler)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(*listen_addr, nil)
}
func defaultPageHandler(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`<html>
<head><title>` + deviceName + ` Exporter</title></head>
<body>
<h1>` + deviceName + ` Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
}