-
Notifications
You must be signed in to change notification settings - Fork 120
/
collector.go
204 lines (175 loc) · 4.48 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
package main
import (
"fmt"
"log"
"sync"
"time"
upnp "github.com/ndecker/fritzbox_exporter/fritzbox_upnp"
"github.com/prometheus/client_golang/prometheus"
)
const serviceLoadRetryTime = 1 * time.Minute
var (
numCalls = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_calls",
Help: "Number of calls to a service action.",
})
collectErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "fritzbox_exporter_collect_errors",
Help: "Number of collection errors.",
})
serviceNotFound = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "fritzbox_exporter_service_not_found",
Help: "",
}, []string{"service"})
actionNotFound = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "fritzbox_exporter_action_not_found",
Help: "",
}, []string{"action"})
resultNotFound = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "fritzbox_exporter_result_not_found",
Help: "",
}, []string{"result"})
collectMetrics = []prometheus.Collector{numCalls, collectErrors, serviceNotFound, actionNotFound, resultNotFound}
)
type FritzboxCollector struct {
Parameters upnp.ConnectionParameters
Metrics []*Metric
sync.RWMutex // protects services
services map[string]*upnp.Service
}
func NewCollector(params upnp.ConnectionParameters, metrics []*Metric) *FritzboxCollector {
c := &FritzboxCollector{
Parameters: params,
Metrics: metrics,
services: make(map[string]*upnp.Service),
}
go c.loadServices()
return c
}
// LoadServices tries to load the service information. Retries until success.
func (fc *FritzboxCollector) loadServices() {
igdRoot := fc.loadService(upnp.IGDServiceDescriptor)
log.Printf("%d IGD services loaded\n", len(igdRoot.Services))
fc.Lock()
for _, s := range igdRoot.Services {
fc.services[s.ServiceType] = s
}
fc.Unlock()
if fc.Parameters.Username == "" {
log.Printf("no username set: not loading TR64 services")
return
}
tr64Root := fc.loadService(upnp.TR64ServiceDescriptor)
log.Printf("%d TR64 services loaded\n", len(tr64Root.Services))
fc.Lock()
for _, s := range tr64Root.Services {
fc.services[s.ServiceType] = s
}
fc.Unlock()
}
func (fc *FritzboxCollector) loadService(desc string) *upnp.Root {
for {
root, err := upnp.LoadServiceRoot(fc.Parameters, desc)
if err != nil {
collectErrors.Inc()
fmt.Printf("cannot load services: %s\n", err)
time.Sleep(serviceLoadRetryTime)
continue
}
return root
}
}
func (fc *FritzboxCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range fc.Metrics {
ch <- m.desc
}
}
func (fc *FritzboxCollector) Collect(ch chan<- prometheus.Metric) {
fc.RLock()
defer fc.RUnlock()
type cacheKey struct {
Service string
Action string
}
// Cache Action call result. Multiple metrics might use different results from a call.
resultCache := make(map[cacheKey]upnp.Result)
for _, m := range fc.Metrics {
result, ok := resultCache[cacheKey{
Service: m.Service,
Action: m.Action,
}]
if !ok {
service, ok := fc.services[m.Service]
if !ok {
serviceNotFound.WithLabelValues(m.Service).Inc()
continue
}
action, ok := service.Actions[m.Action]
if !ok {
actionNotFound.WithLabelValues(m.Action).Inc()
continue
}
numCalls.Inc()
var err error
result, err = action.Call()
if err != nil {
fmt.Println(err)
collectErrors.Inc()
continue
}
resultCache[cacheKey{
Service: m.Service,
Action: m.Action,
}] = result
}
val, ok := result[m.Result]
if !ok {
resultNotFound.WithLabelValues(m.Result).Inc()
continue
}
fc.exportMetric(m, ch, val)
}
}
func (fc *FritzboxCollector) exportMetric(m *Metric, ch chan<- prometheus.Metric, val interface{}) {
if m.LabelName == "" {
// normal metric
floatVal, ok := toFloat(val, m.OkValue)
if !ok {
log.Println("cannot convert to float:", val)
collectErrors.Inc()
}
ch <- prometheus.MustNewConstMetric(
m.desc, m.metricType, floatVal,
fc.Parameters.Device,
)
} else {
// value as label metric
stringVal := fmt.Sprintf("%s", val)
ch <- prometheus.MustNewConstMetric(
m.desc, m.metricType, 1.0,
fc.Parameters.Device, stringVal,
)
}
}
func toFloat(val any, okValue string) (float64, bool) {
switch val := val.(type) {
case uint64:
return float64(val), true
case bool:
if val {
return 1, true
} else {
return 0, true
}
case string:
if okValue == "" {
return 0, false
} else if val == okValue {
return 1, true
} else {
return 0, true
}
default:
return 0, false
}
}