forked from saphoooo/freebox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (263 loc) · 10.1 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
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"bufio"
"flag"
"log"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/iancoleman/strcase"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
mafreebox string
listen string
debug bool
fiber bool
)
func init() {
flag.StringVar(&mafreebox, "endpoint", "http://mafreebox.freebox.fr/", "Endpoint for freebox API")
flag.StringVar(&listen, "listen", ":10001", "Prometheus metrics port")
flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.BoolVar(&fiber, "fiber", false, "Turn on if you're using a fiber Freebox")
}
func main() {
flag.Parse()
if !strings.HasSuffix(mafreebox, "/") {
mafreebox = mafreebox + "/"
}
endpoint := mafreebox + "api/v4/login/"
myAuthInfo := &authInfo{
myAPI: api{
login: endpoint,
authz: endpoint + "authorize/",
loginSession: endpoint + "session/",
},
myStore: store{location: os.Getenv("HOME") + "/.freebox_token"},
myApp: app{
AppID: "fr.freebox.exporter",
AppName: "prometheus-exporter",
AppVersion: "0.4",
DeviceName: "local",
},
myReader: bufio.NewReader(os.Stdin),
}
myPostRequest := newPostRequest()
myConnectionXdslRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v4/connection/xdsl/",
header: "X-Fbx-App-Auth",
}
myFreeplugRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v4/freeplug/",
header: "X-Fbx-App-Auth",
}
myLanRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v4/lan/browser/pub/",
header: "X-Fbx-App-Auth",
}
mySystemRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v4/system/",
header: "X-Fbx-App-Auth",
}
myWifiRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v2/wifi/ap/",
header: "X-Fbx-App-Auth",
}
myVpnRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v4/vpn/connection/",
header: "X-Fbx-App-Auth",
}
var mySessionToken string
go func() {
for {
// There is no DSL metric on fiber Freebox
// If you use a fiber Freebox, use -fiber flag to turn off this metric
if !fiber {
// connectionXdsl metrics
connectionXdslStats, err := getConnectionXdsl(myAuthInfo, myConnectionXdslRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with connectionXdsl metrics: %v", err)
}
if connectionXdslStats.Success {
status := connectionXdslStats.Result.Status
result := connectionXdslStats.Result
down := result.Down
up := result.Up
connectionXdslStatusUptimeGauges.
WithLabelValues(status.Status, status.Protocol, status.Modulation).
Set(float64(status.Uptime))
connectionXdslDownAttnGauge.Set(float64(down.Attn10) / 10)
connectionXdslUpAttnGauge.Set(float64(up.Attn10) / 10)
// XXX: sometimes the Freebox is reporting zero as SNR which
// does not make sense so we don't log these
if down.Snr10 > 0 {
connectionXdslDownSnrGauge.Set(float64(down.Snr10) / 10)
}
if up.Snr10 > 0 {
connectionXdslUpSnrGauge.Set(float64(up.Snr10) / 10)
}
connectionXdslNitroGauges.WithLabelValues("down").
Set(bool2float(down.Nitro))
connectionXdslNitroGauges.WithLabelValues("up").
Set(bool2float(up.Nitro))
connectionXdslGinpGauges.WithLabelValues("down", "enabled").
Set(bool2float(down.Ginp))
connectionXdslGinpGauges.WithLabelValues("up", "enabled").
Set(bool2float(up.Ginp))
logFields(result, connectionXdslGinpGauges,
[]string{"rtx_tx", "rtx_c", "rtx_uc"})
logFields(result, connectionXdslErrorGauges,
[]string{"crc", "es", "fec", "hec", "ses"})
}
// dsl metrics
getDslResult, err := getDsl(myAuthInfo, myPostRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with DSL metrics: %v", err)
}
if len(getDslResult) > 0 {
rateUpGauge.Set(float64(getDslResult[0]))
rateDownGauge.Set(float64(getDslResult[1]))
snrUpGauge.Set(float64(getDslResult[2]))
snrDownGauge.Set(float64(getDslResult[3]))
}
}
// freeplug metrics
freeplugStats, err := getFreeplug(myAuthInfo, myFreeplugRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with freeplug metrics: %v", err)
}
for _, freeplugNetwork := range freeplugStats.Result {
for _, freeplugMember := range freeplugNetwork.Members {
if freeplugMember.HasNetwork {
freeplugHasNetworkGauge.WithLabelValues(freeplugMember.ID).Set(float64(1))
} else {
freeplugHasNetworkGauge.WithLabelValues(freeplugMember.ID).Set(float64(0))
}
Mb := 1e6
rxRate := float64(freeplugMember.RxRate) * Mb
txRate := float64(freeplugMember.TxRate) * Mb
if rxRate >= 0 { // -1 if not unavailable
freeplugRxRateGauge.WithLabelValues(freeplugMember.ID).Set(rxRate)
}
if txRate >= 0 { // -1 if not unavailable
freeplugTxRateGauge.WithLabelValues(freeplugMember.ID).Set(txRate)
}
}
}
// net metrics
getNetResult, err := getNet(myAuthInfo, myPostRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with NET metrics: %v", err)
}
if len(getNetResult) > 0 {
bwUpGauge.Set(float64(getNetResult[0]))
bwDownGauge.Set(float64(getNetResult[1]))
netRateUpGauge.Set(float64(getNetResult[2]))
netRateDownGauge.Set(float64(getNetResult[3]))
vpnRateUpGauge.Set(float64(getNetResult[4]))
vpnRateDownGauge.Set(float64(getNetResult[5]))
}
// lan metrics
lanAvailable, err := getLan(myAuthInfo, myLanRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with LAN metrics: %v", err)
}
for _, v := range lanAvailable {
var Ip string
if len(v.L3c) > 0 {
Ip = v.L3c[0].Addr
} else {
Ip = ""
}
if v.Reachable {
lanReachableGauges.With(prometheus.Labels{"name": v.PrimaryName, "vendor":v.Vendor_name, "ip": Ip}).Set(float64(1))
} else {
lanReachableGauges.With(prometheus.Labels{"name": v.PrimaryName, "vendor":v.Vendor_name, "ip": Ip}).Set(float64(0))
}
}
// system metrics
systemStats, err := getSystem(myAuthInfo, mySystemRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with System metrics: %v", err)
}
systemTempGauges.WithLabelValues("Température CPU B").Set(float64(systemStats.Result.TempCpub))
systemTempGauges.WithLabelValues("Température CPU M").Set(float64(systemStats.Result.TempCpum))
systemTempGauges.WithLabelValues("Température Switch").Set(float64(systemStats.Result.TempSW))
systemTempGauges.WithLabelValues("Disque dur").Set(float64(systemStats.Result.TempHDD))
systemFanGauges.WithLabelValues("Ventilateur 1").Set(float64(systemStats.Result.FanRPM))
systemUptimeGauges.
WithLabelValues(systemStats.Result.FirmwareVersion).
Set(float64(systemStats.Result.UptimeVal))
// wifi metrics
wifiStats, err := getWifi(myAuthInfo, myWifiRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with Wifi metrics: %v", err)
}
for _, accessPoint := range wifiStats.Result {
myWifiStationRequest := &postRequest{
method: "GET",
url: mafreebox + "api/v2/wifi/ap/" + strconv.Itoa(accessPoint.ID) + "/stations",
header: "X-Fbx-App-Auth",
}
wifiStationsStats, err := getWifiStations(myAuthInfo, myWifiStationRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with Wifi station metrics: %v", err)
}
for _, station := range wifiStationsStats.Result {
wifiSignalGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.Signal))
wifiInactiveGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.Inactive))
wifiConnectionDurationGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.ConnectionDuration))
wifiRXBytesGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.RXBytes))
wifiTXBytesGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.TXBytes))
wifiRXRateGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.RXRate))
wifiTXRateGauges.With(prometheus.Labels{"access_point": accessPoint.Name, "hostname": station.Hostname, "state": station.State}).Set(float64(station.TXRate))
}
}
// VPN Server Connections List
getVpnServerResult, err := getVpnServer(myAuthInfo, myVpnRequest, &mySessionToken)
if err != nil {
log.Printf("An error occured with VPN station metrics: %v", err)
}
for _, connection := range getVpnServerResult.Result {
vpnServerConnectionsList.With(prometheus.Labels{"user": connection.User, "vpn": connection.Vpn, "src_ip": connection.SrcIP, "local_ip": connection.LocalIP, "name": "rx_bytes"}).Set(float64(connection.RxBytes))
vpnServerConnectionsList.With(prometheus.Labels{"user": connection.User, "vpn": connection.Vpn, "src_ip": connection.SrcIP, "local_ip": connection.LocalIP, "name": "tx_bytes"}).Set(float64(connection.TxBytes))
}
time.Sleep(10 * time.Second)
}
}()
log.Println("freebox_exporter started on port", listen)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(listen, nil))
}
func logFields(result interface{}, gauge *prometheus.GaugeVec, fields []string) error {
resultReflect := reflect.ValueOf(result)
for _, direction := range []string{"down", "up"} {
for _, field := range fields {
value := reflect.Indirect(resultReflect).
FieldByName(strcase.ToCamel(direction)).
FieldByName(strcase.ToCamel(field))
if value.IsZero() {
continue
}
gauge.WithLabelValues(direction, field).
Set(float64(value.Int()))
}
}
return nil
}
func bool2float(b bool) float64 {
if b {
return 1
}
return 0
}