forked from ndecker/fritzbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
328 lines (291 loc) · 7.24 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
// Copyright 2016 Nils Decker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"flag"
"fmt"
"net/http"
"sync"
"time"
"log"
"github.com/prometheus/client_golang/prometheus"
upnp "github.com/ndecker/fritzbox_exporter/fritzbox_upnp"
)
const serviceLoadRetryTime = 1 * time.Minute
var (
flag_test = flag.Bool("test", false, "print all available metrics to stdout")
flag_addr = flag.String("listen-address", ":9133", "The address to listen on for HTTP requests.")
flag_gateway_address = flag.String("gateway-address", "fritz.box", "The hostname or IP of the FRITZ!Box")
flag_gateway_port = flag.Int("gateway-port", 49000, "The port of the FRITZ!Box UPnP service")
)
var (
collect_errors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_collect_errors",
Help: "Number of collection errors.",
})
)
type Metric struct {
Service string
Action string
Result string
OkValue string
Desc *prometheus.Desc
MetricType prometheus.ValueType
}
var metrics = []*Metric{
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetTotalPacketsReceived",
Result: "TotalPacketsReceived",
Desc: prometheus.NewDesc(
"gateway_wan_packets_received",
"packets received on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetTotalPacketsSent",
Result: "TotalPacketsSent",
Desc: prometheus.NewDesc(
"gateway_wan_packets_sent",
"packets sent on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetAddonInfos",
Result: "TotalBytesReceived",
Desc: prometheus.NewDesc(
"gateway_wan_bytes_received",
"bytes received on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetAddonInfos",
Result: "TotalBytesSent",
Desc: prometheus.NewDesc(
"gateway_wan_bytes_sent",
"bytes sent on gateway WAN interface",
[]string{"gateway"},
nil,
),
MetricType: prometheus.CounterValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "Layer1UpstreamMaxBitRate",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_upstream_max_bitrate",
"Layer1 upstream max bitrate",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "Layer1DownstreamMaxBitRate",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_downstream_max_bitrate",
"Layer1 downstream max bitrate",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
Action: "GetCommonLinkProperties",
Result: "PhysicalLinkStatus",
OkValue: "Up",
Desc: prometheus.NewDesc(
"gateway_wan_layer1_link_status",
"Status of physical link (Up = 1)",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANIPConnection:1",
Action: "GetStatusInfo",
Result: "ConnectionStatus",
OkValue: "Connected",
Desc: prometheus.NewDesc(
"gateway_wan_connection_status",
"WAN connection status (Connected = 1)",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
{
Service: "urn:schemas-upnp-org:service:WANIPConnection:1",
Action: "GetStatusInfo",
Result: "Uptime",
Desc: prometheus.NewDesc(
"gateway_wan_connection_uptime_seconds",
"WAN connection uptime",
[]string{"gateway"},
nil,
),
MetricType: prometheus.GaugeValue,
},
}
type FritzboxCollector struct {
Gateway string
Port uint16
sync.Mutex // protects Root
Root *upnp.Root
}
// LoadServices tries to load the service information. Retries until success.
func (fc *FritzboxCollector) LoadServices() {
for {
root, err := upnp.LoadServices(fc.Gateway, fc.Port)
if err != nil {
fmt.Printf("cannot load services: %s\n", err)
time.Sleep(serviceLoadRetryTime)
continue
}
fmt.Printf("services loaded\n")
fc.Lock()
fc.Root = root
fc.Unlock()
return
}
}
func (fc *FritzboxCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range metrics {
ch <- m.Desc
}
}
func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
fc.Lock()
root := fc.Root
fc.Unlock()
if root == nil {
// Services not loaded yet
return
}
var err error
var last_service string
var last_method string
var last_result upnp.Result
for _, m := range metrics {
if m.Service != last_service || m.Action != last_method {
service, ok := root.Services[m.Service]
if !ok {
// TODO
fmt.Println("cannot find service", m.Service)
fmt.Println(root.Services)
continue
}
action, ok := service.Actions[m.Action]
if !ok {
// TODO
fmt.Println("cannot find action", m.Action)
continue
}
last_result, err = action.Call()
if err != nil {
fmt.Println(err)
collect_errors.Inc()
continue
}
}
val, ok := last_result[m.Result]
if !ok {
fmt.Println("result not found", m.Result)
collect_errors.Inc()
continue
}
var floatval float64
switch tval := val.(type) {
case uint64:
floatval = float64(tval)
case bool:
if tval {
floatval = 1
} else {
floatval = 0
}
case string:
if tval == m.OkValue {
floatval = 1
} else {
floatval = 0
}
default:
fmt.Println("unknown", val)
collect_errors.Inc()
continue
}
ch <- prometheus.MustNewConstMetric(
m.Desc,
m.MetricType,
floatval,
fc.Gateway,
)
}
}
func test() {
root, err := upnp.LoadServices(*flag_gateway_address, uint16(*flag_gateway_port))
if err != nil {
panic(err)
}
for _, s := range root.Services {
fmt.Printf("%s: %s\n", s.Device.FriendlyName, s.ServiceType)
for _, a := range s.Actions {
if !a.IsGetOnly() {
continue
}
res, err := a.Call()
if err != nil {
panic(err)
}
fmt.Printf(" %s\n", a.Name)
for _, arg := range a.Arguments {
fmt.Printf(" %s: %v\n", arg.RelatedStateVariable, res[arg.StateVariable.Name])
}
}
}
}
func main() {
flag.Parse()
if *flag_test {
test()
return
}
collector := &FritzboxCollector{
Gateway: *flag_gateway_address,
Port: uint16(*flag_gateway_port),
}
go collector.LoadServices()
prometheus.MustRegister(collector)
prometheus.MustRegister(collect_errors)
http.Handle("/metrics", prometheus.Handler())
log.Fatal(http.ListenAndServe(*flag_addr, nil))
}