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:
# # 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"
#

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:
# 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"

View File

@ -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 {
@ -26,6 +26,7 @@ type Unbound struct {
UseSudo bool
Server string
ThreadAsTag bool
ConfigFile string
filter filter.Filter
run runner
@ -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"
@ -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 != "" {
@ -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 {
@ -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)
}
@ -207,6 +215,7 @@ func init() {
UseSudo: false,
Server: "",
ThreadAsTag: false,
ConfigFile: "",
}
})
}

View File

@ -12,8 +12,8 @@ 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
}
}
@ -21,7 +21,7 @@ func UnboundControl(output string, Timeout internal.Duration, useSudo bool, Serv
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)
@ -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)