Expose unbound-control config file option (#6770)

This commit is contained in:
Enno Lohmeier 2019-12-18 01:50:00 +01:00 committed by Daniel Nelson
parent 644f2ad847
commit 697963e8cc
4 changed files with 22 additions and 7 deletions

View File

@ -4597,6 +4597,9 @@
# ## The default location of the unbound-control binary can be overridden with: # ## The default location of the unbound-control binary can be overridden with:
# # binary = "/usr/sbin/unbound-control" # # 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: # ## The default timeout of 1s can be overriden with:
# # timeout = "1s" # # timeout = "1s"
# #

View File

@ -18,6 +18,9 @@ a validating, recursive, and caching DNS resolver.
## The default location of the unbound-control binary can be overridden with: ## The default location of the unbound-control binary can be overridden with:
# binary = "/usr/sbin/unbound-control" # 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: ## The default timeout of 1s can be overriden with:
# timeout = "1s" # timeout = "1s"

View File

@ -17,7 +17,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs" "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 // Unbound is used to store configuration values
type Unbound struct { type Unbound struct {
@ -26,6 +26,7 @@ type Unbound struct {
UseSudo bool UseSudo bool
Server string Server string
ThreadAsTag bool ThreadAsTag bool
ConfigFile string
filter filter.Filter filter filter.Filter
run runner run runner
@ -45,6 +46,9 @@ var sampleConfig = `
## The default location of the unbound-control binary can be overridden with: ## The default location of the unbound-control binary can be overridden with:
# binary = "/usr/sbin/unbound-control" # 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: ## The default timeout of 1s can be overriden with:
# timeout = "1s" # timeout = "1s"
@ -67,7 +71,7 @@ func (s *Unbound) SampleConfig() string {
} }
// Shell out to unbound_stat and return the output // 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"} cmdArgs := []string{"stats_noreset"}
if Server != "" { if Server != "" {
@ -96,6 +100,10 @@ func unboundRunner(cmdName string, Timeout internal.Duration, UseSudo bool, Serv
cmdArgs = append([]string{"-s", server}, cmdArgs...) cmdArgs = append([]string{"-s", server}, cmdArgs...)
} }
if ConfigFile != "" {
cmdArgs = append([]string{"-c", ConfigFile}, cmdArgs...)
}
cmd := exec.Command(cmdName, cmdArgs...) cmd := exec.Command(cmdName, cmdArgs...)
if UseSudo { if UseSudo {
@ -125,7 +133,7 @@ func (s *Unbound) Gather(acc telegraf.Accumulator) error {
return err 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 { if err != nil {
return fmt.Errorf("error gathering metrics: %s", err) return fmt.Errorf("error gathering metrics: %s", err)
} }
@ -207,6 +215,7 @@ func init() {
UseSudo: false, UseSudo: false,
Server: "", Server: "",
ThreadAsTag: false, ThreadAsTag: false,
ConfigFile: "",
} }
}) })
} }

View File

@ -12,8 +12,8 @@ import (
var TestTimeout = internal.Duration{Duration: time.Second} 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) { 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) (*bytes.Buffer, error) { return func(string, internal.Duration, bool, string, bool, string) (*bytes.Buffer, error) {
return bytes.NewBuffer([]byte(output)), nil return bytes.NewBuffer([]byte(output)), nil
} }
} }
@ -21,7 +21,7 @@ func UnboundControl(output string, Timeout internal.Duration, useSudo bool, Serv
func TestParseFullOutput(t *testing.T) { func TestParseFullOutput(t *testing.T) {
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
v := &Unbound{ v := &Unbound{
run: UnboundControl(fullOutput, TestTimeout, true, "", false), run: UnboundControl(fullOutput, TestTimeout, true, "", false, ""),
} }
err := v.Gather(acc) err := v.Gather(acc)
@ -38,7 +38,7 @@ func TestParseFullOutput(t *testing.T) {
func TestParseFullOutputThreadAsTag(t *testing.T) { func TestParseFullOutputThreadAsTag(t *testing.T) {
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
v := &Unbound{ v := &Unbound{
run: UnboundControl(fullOutput, TestTimeout, true, "", true), run: UnboundControl(fullOutput, TestTimeout, true, "", true, ""),
ThreadAsTag: true, ThreadAsTag: true,
} }
err := v.Gather(acc) err := v.Gather(acc)