2017-11-20 22:32:06 +00:00
|
|
|
package unbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-02-21 00:06:13 +00:00
|
|
|
"context"
|
2017-11-20 22:32:06 +00:00
|
|
|
"fmt"
|
2018-02-21 00:06:13 +00:00
|
|
|
"net"
|
2017-11-20 22:32:06 +00:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/filter"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2019-12-18 00:50:00 +00:00
|
|
|
type runner func(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool, ConfigFile string) (*bytes.Buffer, error)
|
2017-11-20 22:32:06 +00:00
|
|
|
|
|
|
|
// Unbound is used to store configuration values
|
|
|
|
type Unbound struct {
|
2018-05-23 19:03:49 +00:00
|
|
|
Binary string
|
|
|
|
Timeout internal.Duration
|
|
|
|
UseSudo bool
|
|
|
|
Server string
|
|
|
|
ThreadAsTag bool
|
2019-12-18 00:50:00 +00:00
|
|
|
ConfigFile string
|
2017-11-20 22:32:06 +00:00
|
|
|
|
|
|
|
filter filter.Filter
|
|
|
|
run runner
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultBinary = "/usr/sbin/unbound-control"
|
|
|
|
var defaultTimeout = internal.Duration{Duration: time.Second}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2018-05-23 19:22:25 +00:00
|
|
|
## Address of server to connect to, read from unbound conf default, optionally ':port'
|
|
|
|
## Will lookup IP if given a hostname
|
|
|
|
server = "127.0.0.1:8953"
|
|
|
|
|
2017-11-20 22:32:06 +00:00
|
|
|
## If running as a restricted user you can prepend sudo for additional access:
|
2018-05-23 19:22:25 +00:00
|
|
|
# use_sudo = false
|
2017-11-20 22:32:06 +00:00
|
|
|
|
|
|
|
## The default location of the unbound-control binary can be overridden with:
|
2018-05-23 19:22:25 +00:00
|
|
|
# binary = "/usr/sbin/unbound-control"
|
2017-11-20 22:32:06 +00:00
|
|
|
|
2019-12-18 00:50:00 +00:00
|
|
|
## The default location of the unbound config file can be overridden with:
|
|
|
|
# config_file = "/etc/unbound/unbound.conf"
|
|
|
|
|
2020-05-14 07:41:58 +00:00
|
|
|
## The default timeout of 1s can be overridden with:
|
2018-05-23 19:22:25 +00:00
|
|
|
# timeout = "1s"
|
2018-05-23 19:03:49 +00:00
|
|
|
|
2018-05-23 19:22:25 +00:00
|
|
|
## When set to true, thread metrics are tagged with the thread id.
|
|
|
|
##
|
2018-11-30 23:00:38 +00:00
|
|
|
## The default is false for backwards compatibility, and will be changed to
|
2018-05-23 19:22:25 +00:00
|
|
|
## true in a future version. It is recommended to set to true on new
|
|
|
|
## deployments.
|
2018-05-23 19:03:49 +00:00
|
|
|
thread_as_tag = false
|
2017-11-20 22:32:06 +00:00
|
|
|
`
|
|
|
|
|
2018-05-23 19:03:49 +00:00
|
|
|
// Description displays what this plugin is about
|
2017-11-20 22:32:06 +00:00
|
|
|
func (s *Unbound) Description() string {
|
2018-05-23 19:22:25 +00:00
|
|
|
return "A plugin to collect stats from the Unbound DNS resolver"
|
2017-11-20 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SampleConfig displays configuration instructions
|
|
|
|
func (s *Unbound) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shell out to unbound_stat and return the output
|
2019-12-18 00:50:00 +00:00
|
|
|
func unboundRunner(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool, ConfigFile string) (*bytes.Buffer, error) {
|
2017-11-20 22:32:06 +00:00
|
|
|
cmdArgs := []string{"stats_noreset"}
|
|
|
|
|
2018-02-21 00:06:13 +00:00
|
|
|
if Server != "" {
|
|
|
|
host, port, err := net.SplitHostPort(Server)
|
|
|
|
if err != nil { // No port was specified
|
|
|
|
host = Server
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unbound control requires an IP address, and we want to be nice to the user
|
|
|
|
resolver := net.Resolver{}
|
|
|
|
ctx, lookUpCancel := context.WithTimeout(context.Background(), Timeout.Duration)
|
|
|
|
defer lookUpCancel()
|
|
|
|
serverIps, err := resolver.LookupIPAddr(ctx, host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error looking up ip for server: %s: %s", Server, err)
|
|
|
|
}
|
|
|
|
if len(serverIps) == 0 {
|
|
|
|
return nil, fmt.Errorf("error no ip for server: %s: %s", Server, err)
|
|
|
|
}
|
|
|
|
server := serverIps[0].IP.String()
|
|
|
|
if port != "" {
|
|
|
|
server = server + "@" + port
|
|
|
|
}
|
|
|
|
|
2018-04-23 20:27:29 +00:00
|
|
|
cmdArgs = append([]string{"-s", server}, cmdArgs...)
|
2018-02-21 00:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 00:50:00 +00:00
|
|
|
if ConfigFile != "" {
|
|
|
|
cmdArgs = append([]string{"-c", ConfigFile}, cmdArgs...)
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:32:06 +00:00
|
|
|
cmd := exec.Command(cmdName, cmdArgs...)
|
|
|
|
|
|
|
|
if UseSudo {
|
|
|
|
cmdArgs = append([]string{cmdName}, cmdArgs...)
|
|
|
|
cmd = exec.Command("sudo", cmdArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
err := internal.RunTimeout(cmd, Timeout.Duration)
|
|
|
|
if err != nil {
|
2018-04-23 20:27:29 +00:00
|
|
|
return &out, fmt.Errorf("error running unbound-control: %s (%s %v)", err, cmdName, cmdArgs)
|
2017-11-20 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather collects stats from unbound-control and adds them to the Accumulator
|
|
|
|
//
|
|
|
|
// All the dots in stat name will replaced by underscores. Histogram statistics will not be collected.
|
|
|
|
func (s *Unbound) Gather(acc telegraf.Accumulator) error {
|
|
|
|
|
2020-05-14 07:41:58 +00:00
|
|
|
// Always exclude histogram statistics
|
2018-05-23 19:03:49 +00:00
|
|
|
statExcluded := []string{"histogram.*"}
|
|
|
|
filterExcluded, err := filter.Compile(statExcluded)
|
2017-11-20 22:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 00:50:00 +00:00
|
|
|
out, err := s.run(s.Binary, s.Timeout, s.UseSudo, s.Server, s.ThreadAsTag, s.ConfigFile)
|
2017-11-20 22:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error gathering metrics: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process values
|
|
|
|
fields := make(map[string]interface{})
|
2018-05-23 19:03:49 +00:00
|
|
|
fieldsThreads := make(map[string]map[string]interface{})
|
|
|
|
|
2017-11-20 22:32:06 +00:00
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
|
|
cols := strings.Split(scanner.Text(), "=")
|
|
|
|
|
|
|
|
// Check split correctness
|
|
|
|
if len(cols) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := cols[0]
|
|
|
|
value := cols[1]
|
|
|
|
|
|
|
|
// Filter value
|
2018-05-23 19:03:49 +00:00
|
|
|
if filterExcluded.Match(stat) {
|
2017-11-20 22:32:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:03:49 +00:00
|
|
|
fieldValue, err := strconv.ParseFloat(value, 64)
|
2017-11-20 22:32:06 +00:00
|
|
|
if err != nil {
|
2018-05-23 19:03:49 +00:00
|
|
|
acc.AddError(fmt.Errorf("Expected a numerical value for %s = %v",
|
2017-11-20 22:32:06 +00:00
|
|
|
stat, value))
|
2018-05-23 19:03:49 +00:00
|
|
|
continue
|
2017-11-20 22:32:06 +00:00
|
|
|
}
|
2018-05-23 19:03:49 +00:00
|
|
|
|
|
|
|
// is this a thread related value?
|
|
|
|
if s.ThreadAsTag && strings.HasPrefix(stat, "thread") {
|
|
|
|
// split the stat
|
|
|
|
statTokens := strings.Split(stat, ".")
|
|
|
|
// make sure we split something
|
|
|
|
if len(statTokens) > 1 {
|
|
|
|
// set the thread identifier
|
|
|
|
threadID := strings.TrimPrefix(statTokens[0], "thread")
|
|
|
|
// make sure we have a proper thread ID
|
|
|
|
if _, err = strconv.Atoi(threadID); err == nil {
|
|
|
|
// create new slice without the thread identifier (skip first token)
|
|
|
|
threadTokens := statTokens[1:]
|
|
|
|
// re-define stat
|
|
|
|
field := strings.Join(threadTokens[:], "_")
|
|
|
|
if fieldsThreads[threadID] == nil {
|
|
|
|
fieldsThreads[threadID] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
fieldsThreads[threadID][field] = fieldValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
field := strings.Replace(stat, ".", "_", -1)
|
|
|
|
fields[field] = fieldValue
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("unbound", fields, nil)
|
|
|
|
|
2018-05-23 19:03:49 +00:00
|
|
|
if s.ThreadAsTag && len(fieldsThreads) > 0 {
|
|
|
|
for thisThreadID, thisThreadFields := range fieldsThreads {
|
|
|
|
thisThreadTag := map[string]string{"thread": thisThreadID}
|
|
|
|
acc.AddFields("unbound_threads", thisThreadFields, thisThreadTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:32:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("unbound", func() telegraf.Input {
|
|
|
|
return &Unbound{
|
2018-05-23 19:03:49 +00:00
|
|
|
run: unboundRunner,
|
|
|
|
Binary: defaultBinary,
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
UseSudo: false,
|
|
|
|
Server: "",
|
|
|
|
ThreadAsTag: false,
|
2019-12-18 00:50:00 +00:00
|
|
|
ConfigFile: "",
|
2017-11-20 22:32:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|