forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bind.go
87 lines (73 loc) · 1.71 KB
/
bind.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
package bind
import (
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type Bind struct {
Urls []string
GatherMemoryContexts bool
GatherViews bool
}
var sampleConfig = `
## An array of BIND XML statistics URI to gather stats.
## Default is "http://localhost:8053/xml/v3".
# urls = ["http://localhost:8053/xml/v3"]
# gather_memory_contexts = false
# gather_views = false
`
var client = &http.Client{
Timeout: time.Duration(4 * time.Second),
}
func (b *Bind) Description() string {
return "Read BIND nameserver XML statistics"
}
func (b *Bind) SampleConfig() string {
return sampleConfig
}
func (b *Bind) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
if len(b.Urls) == 0 {
b.Urls = []string{"http://localhost:8053/xml/v3"}
}
for _, u := range b.Urls {
addr, err := url.Parse(u)
if err != nil {
acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err))
continue
}
wg.Add(1)
go func(addr *url.URL) {
defer wg.Done()
acc.AddError(b.gatherUrl(addr, acc))
}(addr)
}
wg.Wait()
return nil
}
func (b *Bind) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
switch addr.Path {
case "":
// BIND 9.6 - 9.8
return b.readStatsXMLv2(addr, acc)
case "/json/v1":
// BIND 9.10+
return b.readStatsJSON(addr, acc)
case "/xml/v2":
// BIND 9.9
return b.readStatsXMLv2(addr, acc)
case "/xml/v3":
// BIND 9.9+
return b.readStatsXMLv3(addr, acc)
default:
return fmt.Errorf("URL %s is ambiguous. Please check plugin documentation for supported URL formats.",
addr)
}
}
func init() {
inputs.Add("bind", func() telegraf.Input { return &Bind{} })
}