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

Fix powerdns_recursor socket_mode option #6572

Merged
merged 2 commits into from
Oct 23, 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
10 changes: 10 additions & 0 deletions plugins/inputs/powerdns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ The powerdns plugin gathers metrics about PowerDNS using unix socket.
unix_sockets = ["/var/run/pdns.controlsocket"]
```

#### Permissions

Telegraf will need read access to the powerdns control socket.

On many systems this can be accomplished by adding the `telegraf` user to the
`pdns` group:
```
usermod telegraf -a -G pdns
```

### Measurements & Fields:

- powerdns
Expand Down
29 changes: 20 additions & 9 deletions plugins/inputs/powerdns_recursor/powerdns_recursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
)

type PowerdnsRecursor struct {
UnixSockets []string
UnixSockets []string `toml:"unix_sockets"`
SocketDir string `toml:"socket_dir"`
SocketMode string `toml:"socket_mode"`

SocketDir string `toml:"socket_dir"`
SocketMode uint32 `toml:"socket_mode"`
mode uint32
}

var defaultTimeout = 5 * time.Second
Expand All @@ -45,6 +46,18 @@ func (p *PowerdnsRecursor) Description() string {
return "Read metrics from one or many PowerDNS Recursor servers"
}

func (p *PowerdnsRecursor) Init() error {
if p.SocketMode != "" {
mode, err := strconv.ParseUint(p.SocketMode, 8, 32)
if err != nil {
return fmt.Errorf("could not parse socket_mode: %v", err)
}

p.mode = uint32(mode)
}
return nil
}

func (p *PowerdnsRecursor) Gather(acc telegraf.Accumulator) error {
if len(p.UnixSockets) == 0 {
return p.gatherServer("/var/run/pdns_recursor.controlsocket", acc)
Expand Down Expand Up @@ -79,11 +92,7 @@ func (p *PowerdnsRecursor) gatherServer(address string, acc telegraf.Accumulator
if err != nil {
return err
}
perm := uint32(0666)
if p.SocketMode > 0 {
perm = p.SocketMode
}
if err := os.Chmod(recvSocket, os.FileMode(perm)); err != nil {
if err := os.Chmod(recvSocket, os.FileMode(p.mode)); err != nil {
return err
}
defer conn.Close()
Expand Down Expand Up @@ -151,6 +160,8 @@ func parseResponse(metrics string) map[string]interface{} {

func init() {
inputs.Add("powerdns_recursor", func() telegraf.Input {
return &PowerdnsRecursor{}
return &PowerdnsRecursor{
mode: uint32(0666),
}
})
}
3 changes: 3 additions & 0 deletions plugins/inputs/powerdns_recursor/powerdns_recursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func TestPowerdnsRecursorGeneratesMetrics(t *testing.T) {
p := &PowerdnsRecursor{
UnixSockets: []string{controlSocket},
SocketDir: "/tmp",
SocketMode: "0666",
}
err = p.Init()
require.NoError(t, err)

var acc testutil.Accumulator

Expand Down