Skip to content

Commit

Permalink
Adding Varnish sudo rules for unprivileged users
Browse files Browse the repository at this point in the history
  • Loading branch information
bstromski committed Aug 7, 2017
1 parent 795f02a commit 0d22302
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions plugins/inputs/varnish/varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

type runner func(cmdName string) (*bytes.Buffer, error)
type runner func(cmdName string, useSudo bool) (*bytes.Buffer, error)

// Varnish is used to store configuration values
type Varnish struct {
Stats []string
Binary string
Sudo bool

filter filter.Filter
run runner
Expand All @@ -32,6 +33,9 @@ var defaultStats = []string{"MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"}
var defaultBinary = "/usr/bin/varnishstat"

var sampleConfig = `
## If running as a restricted user you can prepend sudo for additional access:
sudo = true
## The default location of the varnishstat binary can be overridden with:
binary = "/usr/bin/varnishstat"
Expand All @@ -52,10 +56,15 @@ func (s *Varnish) SampleConfig() string {
}

// Shell out to varnish_stat and return the output
func varnishRunner(cmdName string) (*bytes.Buffer, error) {
func varnishRunner(cmdName string, useSudo bool) (*bytes.Buffer, error) {
cmdArgs := []string{"-1"}

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

if useSudo {
cmdArgs = append([]string{cmdName}, cmdArgs...)
cmd = exec.Command("/bin/sudo", cmdArgs...)
}

var out bytes.Buffer
cmd.Stdout = &out
err := internal.RunTimeout(cmd, time.Millisecond*200)
Expand Down Expand Up @@ -89,7 +98,7 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error {
}
}

out, err := s.run(s.Binary)
out, err := s.run(s.Binary, s.Sudo)
if err != nil {
return fmt.Errorf("error gathering metrics: %s", err)
}
Expand Down Expand Up @@ -148,6 +157,7 @@ func init() {
run: varnishRunner,
Stats: defaultStats,
Binary: defaultBinary,
Sudo: false,
}
})
}

0 comments on commit 0d22302

Please sign in to comment.