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

Add server option to unbound plugin #3713

Merged
merged 3 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ configuration options.
* [teamspeak](./plugins/inputs/teamspeak)
* [tomcat](./plugins/inputs/tomcat)
* [twemproxy](./plugins/inputs/twemproxy)
* [unbound](./plugins/input/unbound)
* [unbound](./plugins/inputs/unbound)
* [varnish](./plugins/inputs/varnish)
* [zfs](./plugins/inputs/zfs)
* [zookeeper](./plugins/inputs/zookeeper)
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/unbound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This plugin gathers stats from [Unbound - a validating, recursive, and caching D

## Use the builtin fielddrop/fieldpass telegraf filters in order to keep only specific fields
fieldpass = ["total_*", "num_*","time_up", "mem_*"]

## IP 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"
```

### Measurements & Fields:
Expand Down
22 changes: 19 additions & 3 deletions plugins/inputs/unbound/unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"net"
"os/exec"
"strconv"
"strings"
Expand All @@ -15,13 +16,14 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

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

// Unbound is used to store configuration values
type Unbound struct {
Binary string
Timeout internal.Duration
UseSudo bool
Server string

filter filter.Filter
run runner
Expand All @@ -42,6 +44,10 @@ var sampleConfig = `

## Use the builtin fielddrop/fieldpass telegraf filters in order to keep/remove specific fields
fieldpass = ["total_*", "num_*","time_up", "mem_*"]

## IP 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use : as separator

`

func (s *Unbound) Description() string {
Expand All @@ -54,9 +60,18 @@ func (s *Unbound) SampleConfig() string {
}

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

if Server != "" {
// Unbound control requires an IP address, and we want to be nice to the user
serverIp, err := net.LookupIP(Server)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you will need to net.SplitHostPort and pass in only the host here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a resolver that can accept a timeout.

if err != nil {
return nil, fmt.Errorf("error looking up ip for server: %s: %s", Server, err)
}
cmdArgs = append(cmdArgs, "-s", serverIp[0].String())
}

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

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

out, err := s.run(s.Binary, s.Timeout, s.UseSudo)
out, err := s.run(s.Binary, s.Timeout, s.UseSudo, s.Server)
if err != nil {
return fmt.Errorf("error gathering metrics: %s", err)
}
Expand Down Expand Up @@ -132,6 +147,7 @@ func init() {
Binary: defaultBinary,
Timeout: defaultTimeout,
UseSudo: false,
Server: "",
}
})
}
6 changes: 3 additions & 3 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) func(string, internal.Duration, bool) (*bytes.Buffer, error) {
return func(string, internal.Duration, bool) (*bytes.Buffer, error) {
func UnboundControl(output string, Timeout internal.Duration, useSudo bool, Server string) func(string, internal.Duration, bool, string) (*bytes.Buffer, error) {
return func(string, internal.Duration, 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),
run: UnboundControl(fullOutput, TestTimeout, true, ""),
}
err := v.Gather(acc)

Expand Down