Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unbound: expose unbound-control config file option #6770

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4590,6 +4590,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