master
go 72 lines 1.71 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package powerdns_recursor
4
5 import (
6 "errors"
7 "strconv"
8
9 "github.com/netdata/netdata/go/plugins/pkg/web"
10 )
11
12 const (
13 urlPathLocalStatistics = "/api/v1/servers/localhost/statistics"
14 )
15
16 func (c *Collector) collect() (map[string]int64, error) {
17 statistics, err := c.scrapeStatistics()
18 if err != nil {
19 return nil, err
20 }
21
22 collected := make(map[string]int64)
23
24 c.collectStatistics(collected, statistics)
25
26 if !isPowerDNSRecursorMetrics(collected) {
27 return nil, errors.New("returned metrics aren't PowerDNS Recursor metrics")
28 }
29
30 return collected, nil
31 }
32
33 func isPowerDNSRecursorMetrics(collected map[string]int64) bool {
34 // PowerDNS Authoritative Server has same endpoint and returns data in the same format.
35 _, ok1 := collected["over-capacity-drops"]
36 _, ok2 := collected["tcp-questions"]
37 return ok1 && ok2
38 }
39
40 func (c *Collector) collectStatistics(collected map[string]int64, statistics statisticMetrics) {
41 for _, s := range statistics {
42 // https://doc.powerdns.com/authoritative/http-api/statistics.html#statisticitem
43 if s.Type != "StatisticItem" {
44 continue
45 }
46
47 value, ok := s.Value.(string)
48 if !ok {
49 c.Debugf("%s value (%v) unexpected type: want=string, got=%T.", s.Name, s.Value, s.Value)
50 continue
51 }
52
53 v, err := strconv.ParseInt(value, 10, 64)
54 if err != nil {
55 c.Debugf("%s value (%v) parse error: %v", s.Name, s.Value, err)
56 continue
57 }
58
59 collected[s.Name] = v
60 }
61 }
62
63 func (c *Collector) scrapeStatistics() ([]statisticMetric, error) {
64 req, _ := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathLocalStatistics)
65
66 var stats statisticMetrics
67 if err := web.DoHTTP(c.httpClient).RequestJSON(req, &stats); err != nil {
68 return nil, err
69 }
70
71 return stats, nil
72 }