Skip to content

Commit

Permalink
Expose unbound-control config file option (influxdata#6770)
Browse files Browse the repository at this point in the history
  • Loading branch information
elohmeier authored and danielnelson committed Dec 18, 2019
1 parent ef7b4d9 commit 02d60ca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4597,6 +4597,9 @@
# ## The default location of the unbound-control binary can be overridden with:
# # binary = "/usr/sbin/unbound-control"
#
# ## The default location of the unbound config file can be overridden with:
# # config_file = "/etc/unbound/unbound.conf"
#
# ## The default timeout of 1s can be overriden with:
# # timeout = "1s"
#
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/unbound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ a validating, recursive, and caching DNS resolver.
## The default location of the unbound-control binary can be overridden with:
# binary = "/usr/sbin/unbound-control"

## The default location of the unbound config file can be overridden with:
# config_file = "/etc/unbound/unbound.conf"

## The default timeout of 1s can be overriden with:
# timeout = "1s"

Expand Down
15 changes: 12 additions & 3 deletions plugins/inputs/unbound/unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

type runner func(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool) (*bytes.Buffer, error)
type runner func(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool, ConfigFile string) (*bytes.Buffer, error)

// Unbound is used to store configuration values
type Unbound struct {
Expand All @@ -26,6 +26,7 @@ type Unbound struct {
UseSudo bool
Server string
ThreadAsTag bool
ConfigFile string

filter filter.Filter
run runner
Expand All @@ -45,6 +46,9 @@ var sampleConfig = `
## The default location of the unbound-control binary can be overridden with:
# binary = "/usr/sbin/unbound-control"
## The default location of the unbound config file can be overridden with:
# config_file = "/etc/unbound/unbound.conf"
## The default timeout of 1s can be overriden with:
# timeout = "1s"
Expand All @@ -67,7 +71,7 @@ func (s *Unbound) SampleConfig() string {
}

// Shell out to unbound_stat and return the output
func unboundRunner(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool) (*bytes.Buffer, error) {
func unboundRunner(cmdName string, Timeout internal.Duration, UseSudo bool, Server string, ThreadAsTag bool, ConfigFile string) (*bytes.Buffer, error) {
cmdArgs := []string{"stats_noreset"}

if Server != "" {
Expand Down Expand Up @@ -96,6 +100,10 @@ func unboundRunner(cmdName string, Timeout internal.Duration, UseSudo bool, Serv
cmdArgs = append([]string{"-s", server}, cmdArgs...)
}

if ConfigFile != "" {
cmdArgs = append([]string{"-c", ConfigFile}, cmdArgs...)
}

cmd := exec.Command(cmdName, cmdArgs...)

if UseSudo {
Expand Down Expand Up @@ -125,7 +133,7 @@ func (s *Unbound) Gather(acc telegraf.Accumulator) error {
return err
}

out, err := s.run(s.Binary, s.Timeout, s.UseSudo, s.Server, s.ThreadAsTag)
out, err := s.run(s.Binary, s.Timeout, s.UseSudo, s.Server, s.ThreadAsTag, s.ConfigFile)
if err != nil {
return fmt.Errorf("error gathering metrics: %s", err)
}
Expand Down Expand Up @@ -207,6 +215,7 @@ func init() {
UseSudo: false,
Server: "",
ThreadAsTag: false,
ConfigFile: "",
}
})
}
8 changes: 4 additions & 4 deletions plugins/inputs/unbound/unbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (

var TestTimeout = internal.Duration{Duration: time.Second}

func UnboundControl(output string, Timeout internal.Duration, useSudo bool, Server string, ThreadAsTag bool) func(string, internal.Duration, bool, string, bool) (*bytes.Buffer, error) {
return func(string, internal.Duration, bool, string, bool) (*bytes.Buffer, error) {
func UnboundControl(output string, Timeout internal.Duration, useSudo bool, Server string, ThreadAsTag bool, ConfigFile string) func(string, internal.Duration, bool, string, bool, string) (*bytes.Buffer, error) {
return func(string, internal.Duration, bool, string, bool, string) (*bytes.Buffer, error) {
return bytes.NewBuffer([]byte(output)), nil
}
}

func TestParseFullOutput(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Unbound{
run: UnboundControl(fullOutput, TestTimeout, true, "", false),
run: UnboundControl(fullOutput, TestTimeout, true, "", false, ""),
}
err := v.Gather(acc)

Expand All @@ -38,7 +38,7 @@ func TestParseFullOutput(t *testing.T) {
func TestParseFullOutputThreadAsTag(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Unbound{
run: UnboundControl(fullOutput, TestTimeout, true, "", true),
run: UnboundControl(fullOutput, TestTimeout, true, "", true, ""),
ThreadAsTag: true,
}
err := v.Gather(acc)
Expand Down

0 comments on commit 02d60ca

Please sign in to comment.