Skip to content

Commit

Permalink
Fix powerdns_recursor socket_mode option (#6572)
Browse files Browse the repository at this point in the history
(cherry picked from commit 504ccc2)
  • Loading branch information
danielnelson committed Oct 23, 2019
1 parent 1a4ae5f commit ac4d482
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
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

0 comments on commit ac4d482

Please sign in to comment.