-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollector.go
212 lines (173 loc) · 5.04 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
package main
import (
"crypto/sha256"
"encoding/xml"
errors2 "errors"
"fmt"
"io/ioutil"
"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=Internet_InternetStatusforRoute_t.lp&Menu3Location=0&_=1583884785730"
ethernetMetricsUrl = "http://%s/common_page/internet_eth_interface_lua.lua"
)
var (
ethernetDesc = prometheus.NewDesc(
metricPrefix+"ethernet",
"All ethernet (eth) related metadata.",
[]string{"value"}, 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 <- ethernetDesc
}
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 {
_, err := c.client.Get(fmt.Sprintf(loginUrl, c.ip.String()))
if err != nil {
return err
}
tokenRequest, err := c.client.Get(fmt.Sprintf(tokenUrl, c.ip.String()))
if err != nil {
return err
}
type tokenResponseStruct struct {
Token int `xml:",chardata"`
}
tokenData, err := ioutil.ReadAll(tokenRequest.Body)
if err != nil {
return err
}
var tokenResponse tokenResponseStruct
err = xml.Unmarshal(tokenData, &tokenResponse)
if err != nil {
return err
}
loginParams := url.Values{}
loginParams.Set("Username", c.username)
loginParams.Set("Password", fmt.Sprintf("%x", sha256.Sum256([]byte(c.password + strconv.Itoa(tokenResponse.Token)))))
loginParams.Set("action", "login")
loginRequest, err := c.client.PostForm(fmt.Sprintf(loginUrl, c.ip.String()), loginParams)
if err != nil {
return err
}
body, _ := ioutil.ReadAll(loginRequest.Body)
if strings.Contains(string(body), "loginWrapper") {
return errors2.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", "")
_, err := c.client.PostForm(fmt.Sprintf(logoutUrl, c.ip.String()), logoutParams)
if err != nil {
return err
}
c.client.Jar, _ = cookiejar.New(nil)
return nil
}
func (c *experiav10Collector) scrape(ch chan<- prometheus.Metric) error {
_, err := c.client.Get(fmt.Sprintf(ethernetPageUrl, c.ip.String()))
if err != nil {
return err
}
ethernetMetricsRequest, err := c.client.Get(fmt.Sprintf(ethernetMetricsUrl, c.ip.String()))
if err != nil {
return err
}
ethernetMetricsData, err := ioutil.ReadAll(ethernetMetricsRequest.Body)
if err != nil {
return err
}
type ethernetMetricsStruct struct {
Names []string `xml:"OBJ_ETH_ID>Instance>ParaName"`
Values []string `xml:"OBJ_ETH_ID>Instance>ParaValue"`
}
var ethernetMetricsResponse ethernetMetricsStruct
err = xml.Unmarshal(ethernetMetricsData, ðernetMetricsResponse)
if err != nil {
return err
}
for i := 0; i < len(ethernetMetricsResponse.Names); i++ {
value, err := strconv.ParseFloat(ethernetMetricsResponse.Values[i], 0)
if err != nil {
continue
}
metric, err := prometheus.NewConstMetric(ethernetDesc, prometheus.CounterValue, value, ethernetMetricsResponse.Names[i])
if err != nil {
return fmt.Errorf("error creating metric for %s: %s", ethernetDesc, err)
}
ch <- metric
}
return nil
}