forked from bajansen/experia-v10-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
266 lines (222 loc) · 6.89 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
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/xml"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
metricPrefix = "experia_v10_"
)
const (
tokenUrl = "http://%s/function_module/login_module/login_page/logintoken_lua.lua"
loginUrl = "http://%s"
logoutUrl = "http://%s"
ethernetPageUrl = "http://%s/getpage.lua?pid=123&nextpage=Localnet_LAN_LocalnetStatus_t.lp&Menu3Location=0&_=1611056303063"
ethernetMetricsUrl = "http://%s/common_page/lanStatus_lua.lua"
dslPageUrl = "http://%s/getpage.lua?pid=123&nextpage=Internet_InternetStatusforRoute_DSL_t.lp&Menu3Location=0"
dslMetricsUrl = "http://%s/common_page/internet_dsl_interface_lua.lua"
)
var (
dslDesc = prometheus.NewDesc(
metricPrefix+"dsl",
"All dsl related metadata.",
[]string{"value"}, nil)
ifInOctets = prometheus.NewDesc(
metricPrefix+"interface_received_bytes_total",
"The total number of bytes received on the interface",
[]string{"id", "alias"}, nil)
ifOutOctets = prometheus.NewDesc(
metricPrefix+"interface_sent_bytes_total",
"The total number of bytes transmitted out of the interface",
[]string{"id", "alias"}, nil)
)
type experiav10Collector struct {
ip net.IP
username string
password string
client *http.Client
upMetric prometheus.Gauge
authErrorsMetric prometheus.Counter
scrapeErrorsMetric prometheus.Counter
}
func newCollector(ip net.IP, username, password string, timeout time.Duration) *experiav10Collector {
cookieJar, _ := cookiejar.New(nil)
return &experiav10Collector{
ip: ip,
username: username,
password: password,
client: &http.Client{
Timeout: timeout,
Jar: cookieJar,
},
upMetric: prometheus.NewGauge(prometheus.GaugeOpts{
Name: metricPrefix + "up",
Help: "Shows if the Experia Box V10 is deemed up by the collector.",
}),
authErrorsMetric: prometheus.NewCounter(prometheus.CounterOpts{
Name: metricPrefix + "auth_errors_total",
Help: "Counts number of authentication errors encountered by the collector.",
}),
scrapeErrorsMetric: prometheus.NewCounter(prometheus.CounterOpts{
Name: metricPrefix + "scrape_errors_total",
Help: "Counts the number of scrape errors by this collector.",
}),
}
}
func (c *experiav10Collector) Describe(ch chan<- *prometheus.Desc) {
c.upMetric.Describe(ch)
c.authErrorsMetric.Describe(ch)
c.scrapeErrorsMetric.Describe(ch)
ch <- ifInOctets
ch <- ifOutOctets
ch <- dslDesc
}
func (c *experiav10Collector) Collect(ch chan<- prometheus.Metric) {
if err := c.login(ch); err != nil {
log.Printf("Error during authentication: %s", err)
c.authErrorsMetric.Inc()
c.upMetric.Set(0)
} else {
defer c.logout(ch)
if err := c.scrape(ch); err != nil {
log.Printf("Error during scrape: %s", err)
c.scrapeErrorsMetric.Inc()
c.upMetric.Set(0)
} else {
c.upMetric.Set(1)
}
}
c.upMetric.Collect(ch)
c.authErrorsMetric.Collect(ch)
c.scrapeErrorsMetric.Collect(ch)
}
func (c *experiav10Collector) login(ch chan<- prometheus.Metric) error {
loginPageRequest, err := c.client.Get(fmt.Sprintf(loginUrl, c.ip.String()))
if err != nil {
return err
}
loginPageRequest.Body.Close()
tokenRequest, err := c.client.Get(fmt.Sprintf(tokenUrl, c.ip.String()))
if err != nil {
return err
}
defer tokenRequest.Body.Close()
var tokenResponse struct {
Token int `xml:",chardata"`
}
if err := xml.NewDecoder(tokenRequest.Body).Decode(&tokenResponse); err != nil {
return fmt.Errorf("failed to parse login token: %w", err)
}
loginParams := url.Values{}
passwordHash := sha256.Sum256([]byte(c.password + strconv.Itoa(tokenResponse.Token)))
loginParams.Set("Username", c.username)
loginParams.Set("Password", hex.EncodeToString(passwordHash[:]))
loginParams.Set("action", "login")
loginRequest, err := c.client.PostForm(fmt.Sprintf(loginUrl, c.ip.String()), loginParams)
if err != nil {
return err
}
defer loginRequest.Body.Close()
body, err := io.ReadAll(loginRequest.Body)
if err != nil {
return err
}
if strings.Contains(string(body), "loginWrapper") {
return errors.New("unable to login")
}
return nil
}
func (c *experiav10Collector) logout(ch chan<- prometheus.Metric) error {
logoutParams := url.Values{}
logoutParams.Set("IF_LogOff", "1")
logoutParams.Set("IF_LanguageSwitch", "")
logoutParams.Set("IF_ModeSwitch", "")
logoutRequest, err := c.client.PostForm(fmt.Sprintf(logoutUrl, c.ip.String()), logoutParams)
if err != nil {
return err
}
logoutRequest.Body.Close()
c.client.Jar, _ = cookiejar.New(nil)
return nil
}
func (c *experiav10Collector) scrape(ch chan<- prometheus.Metric) error {
// For some reason the page containing the actual data will only contain data
// after this page is loaded first
dslPageRequest, err := c.client.Get(fmt.Sprintf(dslPageUrl, c.ip.String()))
if err != nil {
return err
}
dslPageRequest.Body.Close()
dslMetricsRequest, err := c.client.Get(fmt.Sprintf(dslMetricsUrl, c.ip.String()))
if err != nil {
return err
}
defer dslMetricsRequest.Body.Close()
var dslMetrics struct {
Names []string `xml:"OBJ_DSLINTERFACE_ID>Instance>ParaName"`
Values []string `xml:"OBJ_DSLINTERFACE_ID>Instance>ParaValue"`
}
if err := xml.NewDecoder(dslMetricsRequest.Body).Decode(&dslMetrics); err != nil {
return err
}
for i := 0; i < len(dslMetrics.Names); i++ {
value, err := strconv.ParseFloat(dslMetrics.Values[i], 64)
if err != nil {
continue
}
metric, err := prometheus.NewConstMetric(dslDesc, prometheus.CounterValue, value, dslMetrics.Names[i])
if err != nil {
return fmt.Errorf("error creating metric for %s: %s", dslDesc, err)
}
ch <- metric
}
ethernetPageRequest, err := c.client.Get(fmt.Sprintf(ethernetPageUrl, c.ip.String()))
if err != nil {
return err
}
ethernetPageRequest.Body.Close()
ethernetMetricsRequest, err := c.client.Get(fmt.Sprintf(ethernetMetricsUrl, c.ip.String()))
if err != nil {
return err
}
defer ethernetMetricsRequest.Body.Close()
var ethernetMetrics struct {
Names []string `xml:"OBJ_ETH_ID>Instance>ParaName"`
Values []string `xml:"OBJ_ETH_ID>Instance>ParaValue"`
}
if err := xml.NewDecoder(ethernetMetricsRequest.Body).Decode(ðernetMetrics); err != nil {
return err
}
// Each LAN Instance has 6 fields
for i := 0; i < len(ethernetMetrics.Names); i += 6 {
ifName := ethernetMetrics.Values[i]
ifAlias := ethernetMetrics.Values[i+1]
inBytes, err := strconv.ParseFloat(ethernetMetrics.Values[i+2], 64)
if err != nil {
continue
}
outBytes, err := strconv.ParseFloat(ethernetMetrics.Values[i+5], 64)
if err != nil {
continue
}
if inBytes > 0 {
ch <- prometheus.MustNewConstMetric(ifInOctets, prometheus.CounterValue, inBytes, ifName, ifAlias)
}
if outBytes > 0 {
ch <- prometheus.MustNewConstMetric(ifOutOctets, prometheus.CounterValue, outBytes, ifName, ifAlias)
}
}
return nil
}