2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2019-04-04 06:59:47 +08:00
|
|
|
package bind
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2019-04-04 06:59:47 +08:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-12-04 03:42:50 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2019-04-04 06:59:47 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-27 21:13:47 +08:00
|
|
|
// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2019-04-04 06:59:47 +08:00
|
|
|
type Bind struct {
|
|
|
|
|
Urls []string
|
|
|
|
|
GatherMemoryContexts bool
|
|
|
|
|
GatherViews bool
|
2020-12-04 03:42:50 +08:00
|
|
|
Timeout config.Duration `toml:"timeout"`
|
|
|
|
|
|
|
|
|
|
client http.Client
|
2019-04-04 06:59:47 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*Bind) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 03:42:50 +08:00
|
|
|
func (b *Bind) Init() error {
|
|
|
|
|
b.client = http.Client{
|
|
|
|
|
Timeout: time.Duration(b.Timeout),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 06:59:47 +08:00
|
|
|
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 {
|
2021-02-09 00:18:40 +08:00
|
|
|
acc.AddError(fmt.Errorf("unable to parse address '%s': %s", u, err))
|
2019-04-04 06:59:47 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(addr *url.URL) {
|
|
|
|
|
defer wg.Done()
|
2021-03-02 05:04:35 +08:00
|
|
|
acc.AddError(b.gatherURL(addr, acc))
|
2019-04-04 06:59:47 +08:00
|
|
|
}(addr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
func (b *Bind) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
|
2019-04-04 06:59:47 +08:00
|
|
|
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:
|
2021-02-09 00:18:40 +08:00
|
|
|
return fmt.Errorf("provided URL %s is ambiguous, please check plugin documentation for supported URL formats",
|
2019-04-04 06:59:47 +08:00
|
|
|
addr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("bind", func() telegraf.Input { return &Bind{} })
|
|
|
|
|
}
|