-
Notifications
You must be signed in to change notification settings - Fork 36
/
powerdns_exporter.go
350 lines (305 loc) · 8.71 KB
/
powerdns_exporter.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof"
"net/url"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const (
namespace = "powerdns"
apiInfoEndpoint = "servers/localhost"
apiStatsEndpoint = "servers/localhost/statistics"
)
var (
client = &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, 5*time.Second)
if err != nil {
return nil, err
}
if err := c.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
return nil, err
}
return c, nil
},
},
}
)
// ServerInfo is used to parse JSON data from 'server/localhost' endpoint
type ServerInfo struct {
Kind string `json:"type"`
ID string `json:"id"`
URL string `json:"url"`
DaemonType string `json:"daemon_type"`
Version string `json:"version"`
ConfigUrl string `json:"config_url"`
ZonesUrl string `json:"zones_url"`
}
// StatsEntry is used to parse JSON data from 'server/localhost/statistics' endpoint
type StatsEntry struct {
Name string `json:"name"`
Kind string `json:"type"`
Value float64 `json:"value,string"`
}
// Exporter collects PowerDNS stats from the given HostURL and exports them using
// the prometheus metrics package.
type Exporter struct {
HostURL *url.URL
ServerType string
ApiKey string
mutex sync.RWMutex
up prometheus.Gauge
totalScrapes prometheus.Counter
jsonParseFailures prometheus.Counter
gaugeMetrics map[int]prometheus.Gauge
counterVecMetrics map[int]*prometheus.CounterVec
gaugeDefs []gaugeDefinition
counterVecDefs []counterVecDefinition
client *http.Client
}
func newCounterVecMetric(serverType, metricName, docString string, labelNames []string) *prometheus.CounterVec {
return prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: serverType,
Name: metricName,
Help: docString,
},
labelNames,
)
}
func newGaugeMetric(serverType, metricName, docString string) prometheus.Gauge {
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: serverType,
Name: metricName,
Help: docString,
},
)
}
// NewExporter returns an initialized Exporter.
func NewExporter(apiKey, serverType string, hostURL *url.URL) *Exporter {
var gaugeDefs []gaugeDefinition
var counterVecDefs []counterVecDefinition
gaugeMetrics := make(map[int]prometheus.Gauge)
counterVecMetrics := make(map[int]*prometheus.CounterVec)
switch serverType {
case "recursor":
gaugeDefs = recursorGaugeDefs
counterVecDefs = recursorCounterVecDefs
case "authoritative":
gaugeDefs = authoritativeGaugeDefs
counterVecDefs = authoritativeCounterVecDefs
case "dnsdist":
gaugeDefs = dnsdistGaugeDefs
counterVecDefs = dnsdistCounterVecDefs
}
for _, def := range gaugeDefs {
gaugeMetrics[def.id] = newGaugeMetric(serverType, def.name, def.desc)
}
for _, def := range counterVecDefs {
counterVecMetrics[def.id] = newCounterVecMetric(serverType, def.name, def.desc, []string{def.label})
}
return &Exporter{
HostURL: hostURL,
ServerType: serverType,
ApiKey: apiKey,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: serverType,
Name: "up",
Help: "Was the last scrape of PowerDNS successful.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: serverType,
Name: "exporter_total_scrapes",
Help: "Current total PowerDNS scrapes.",
}),
jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: serverType,
Name: "exporter_json_parse_failures",
Help: "Number of errors while parsing PowerDNS JSON stats.",
}),
gaugeMetrics: gaugeMetrics,
counterVecMetrics: counterVecMetrics,
gaugeDefs: gaugeDefs,
counterVecDefs: counterVecDefs,
}
}
// Describe describes all the metrics ever exported by the PowerDNS exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.counterVecMetrics {
m.Describe(ch)
}
for _, m := range e.gaugeMetrics {
ch <- m.Desc()
}
ch <- e.up.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.jsonParseFailures.Desc()
}
// Collect fetches the stats from configured PowerDNS API URI and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
jsonStats := make(chan []StatsEntry)
go e.scrape(jsonStats)
e.mutex.Lock()
defer e.mutex.Unlock()
e.resetMetrics()
statsMap := e.setMetrics(jsonStats)
ch <- e.up
ch <- e.totalScrapes
ch <- e.jsonParseFailures
e.collectMetrics(ch, statsMap)
}
func (e *Exporter) scrape(jsonStats chan<- []StatsEntry) {
defer close(jsonStats)
e.totalScrapes.Inc()
var data []StatsEntry
url := apiURL(e.HostURL, apiStatsEndpoint)
err := getJSON(url, e.ApiKey, &data)
if err != nil {
e.up.Set(0)
e.jsonParseFailures.Inc()
log.Errorf("Error scraping PowerDNS: %v", err)
return
}
e.up.Set(1)
jsonStats <- data
}
func (e *Exporter) resetMetrics() {
for _, m := range e.counterVecMetrics {
m.Reset()
}
}
func (e *Exporter) collectMetrics(ch chan<- prometheus.Metric, statsMap map[string]float64) {
for _, m := range e.counterVecMetrics {
m.Collect(ch)
}
for _, m := range e.gaugeMetrics {
ch <- m
}
if e.ServerType == "recursor" {
h, err := makeRecursorRTimeHistogram(statsMap)
if err != nil {
log.Errorf("Could not create response time histogram: %v", err)
return
}
ch <- h
}
}
func (e *Exporter) setMetrics(jsonStats <-chan []StatsEntry) (statsMap map[string]float64) {
statsMap = make(map[string]float64)
stats := <-jsonStats
for _, s := range stats {
statsMap[s.Name] = s.Value
}
if len(statsMap) == 0 {
return
}
for _, def := range e.gaugeDefs {
if value, ok := statsMap[def.key]; ok {
// latency gauges need to be converted from microseconds to seconds
if strings.HasSuffix(def.key, "latency") {
value = value / 1000000
}
e.gaugeMetrics[def.id].Set(value)
} else {
log.Errorf("Expected PowerDNS stats key not found: %s", def.key)
e.jsonParseFailures.Inc()
}
}
for _, def := range e.counterVecDefs {
for key, label := range def.labelMap {
if value, ok := statsMap[key]; ok {
e.counterVecMetrics[def.id].WithLabelValues(label).Set(value)
} else {
log.Errorf("Expected PowerDNS stats key not found: %s", key)
e.jsonParseFailures.Inc()
}
}
}
return
}
func getServerInfo(hostURL *url.URL, apiKey string) (*ServerInfo, error) {
var info ServerInfo
url := apiURL(hostURL, apiInfoEndpoint)
err := getJSON(url, apiKey, &info)
if err != nil {
return nil, err
}
return &info, nil
}
func getJSON(url, apiKey string, data interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("X-API-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(content))
}
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return err
}
return nil
}
func apiURL(hostURL *url.URL, path string) string {
endpointURI, _ := url.Parse(path)
u := hostURL.ResolveReference(endpointURI)
return u.String()
}
func main() {
var (
listenAddress = flag.String("listen-address", ":9120", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("metric-path", "/metrics", "Path under which to expose metrics.")
apiURL = flag.String("api-url", "http://localhost:8001/", "Base-URL of PowerDNS authoritative server/recursor API.")
apiKey = flag.String("api-key", "", "PowerDNS API Key")
)
flag.Parse()
hostURL, err := url.Parse(*apiURL)
if err != nil {
log.Fatalf("Error parsing api-url: %v", err)
}
server, err := getServerInfo(hostURL, *apiKey)
if err != nil {
log.Fatalf("Could not fetch PowerDNS server info: %v", err)
}
exporter := NewExporter(*apiKey, server.DaemonType, hostURL)
prometheus.MustRegister(exporter)
log.Infof("Starting Server: %s", *listenAddress)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>PowerDNS Exporter</title></head>
<body>
<h1>PowerDNS Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}