diff --git a/plugins/inputs/varnish/varnish.go b/plugins/inputs/varnish/varnish.go index 08c885e61..7ceb7d250 100644 --- a/plugins/inputs/varnish/varnish.go +++ b/plugins/inputs/varnish/varnish.go @@ -17,13 +17,14 @@ import ( "github.com/influxdata/telegraf/plugins/inputs" ) -type runner func(cmdName string, UseSudo bool) (*bytes.Buffer, error) +type runner func(cmdName string, UseSudo bool, InstanceName string) (*bytes.Buffer, error) // Varnish is used to store configuration values type Varnish struct { - Stats []string - Binary string - UseSudo bool + Stats []string + Binary string + UseSudo bool + InstanceName string filter filter.Filter run runner @@ -44,6 +45,10 @@ var sampleConfig = ` ## Glob matching can be used, ie, stats = ["MAIN.*"] ## stats may also be set to ["*"], which will collect all stats stats = ["MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"] + + ## Optional name for the varnish instance (or working directory) to query + ## Usually appened after -n in varnish cli + #name = instanceName ` func (s *Varnish) Description() string { @@ -56,8 +61,13 @@ func (s *Varnish) SampleConfig() string { } // Shell out to varnish_stat and return the output -func varnishRunner(cmdName string, UseSudo bool) (*bytes.Buffer, error) { +func varnishRunner(cmdName string, UseSudo bool, InstanceName string) (*bytes.Buffer, error) { cmdArgs := []string{"-1"} + + if InstanceName != "" { + cmdArgs = append(cmdArgs, []string{"-n", InstanceName}...) + } + cmd := exec.Command(cmdName, cmdArgs...) if UseSudo { @@ -99,7 +109,7 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error { } } - out, err := s.run(s.Binary, s.UseSudo) + out, err := s.run(s.Binary, s.UseSudo, s.InstanceName) if err != nil { return fmt.Errorf("error gathering metrics: %s", err) } @@ -155,10 +165,11 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error { func init() { inputs.Add("varnish", func() telegraf.Input { return &Varnish{ - run: varnishRunner, - Stats: defaultStats, - Binary: defaultBinary, - UseSudo: false, + run: varnishRunner, + Stats: defaultStats, + Binary: defaultBinary, + UseSudo: false, + InstanceName: "", } }) } diff --git a/plugins/inputs/varnish/varnish_test.go b/plugins/inputs/varnish/varnish_test.go index a2b388a0b..30f91e237 100644 --- a/plugins/inputs/varnish/varnish_test.go +++ b/plugins/inputs/varnish/varnish_test.go @@ -5,14 +5,15 @@ package varnish import ( "bytes" "fmt" - "github.com/influxdata/telegraf/testutil" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/assert" ) -func fakeVarnishStat(output string, useSudo bool) func(string, bool) (*bytes.Buffer, error) { - return func(string, bool) (*bytes.Buffer, error) { +func fakeVarnishStat(output string, useSudo bool, InstanceName string) func(string, bool, string) (*bytes.Buffer, error) { + return func(string, bool, string) (*bytes.Buffer, error) { return bytes.NewBuffer([]byte(output)), nil } } @@ -20,7 +21,7 @@ func fakeVarnishStat(output string, useSudo bool) func(string, bool) (*bytes.Buf func TestGather(t *testing.T) { acc := &testutil.Accumulator{} v := &Varnish{ - run: fakeVarnishStat(smOutput, false), + run: fakeVarnishStat(smOutput, false, ""), Stats: []string{"*"}, } v.Gather(acc) @@ -36,7 +37,7 @@ func TestGather(t *testing.T) { func TestParseFullOutput(t *testing.T) { acc := &testutil.Accumulator{} v := &Varnish{ - run: fakeVarnishStat(fullOutput, true), + run: fakeVarnishStat(fullOutput, true, ""), Stats: []string{"*"}, } err := v.Gather(acc) @@ -51,7 +52,7 @@ func TestParseFullOutput(t *testing.T) { func TestFilterSomeStats(t *testing.T) { acc := &testutil.Accumulator{} v := &Varnish{ - run: fakeVarnishStat(fullOutput, false), + run: fakeVarnishStat(fullOutput, false, ""), Stats: []string{"MGT.*", "VBE.*"}, } err := v.Gather(acc) @@ -74,7 +75,7 @@ func TestFieldConfig(t *testing.T) { for fieldCfg, expected := range expect { acc := &testutil.Accumulator{} v := &Varnish{ - run: fakeVarnishStat(fullOutput, true), + run: fakeVarnishStat(fullOutput, true, ""), Stats: strings.Split(fieldCfg, ","), } err := v.Gather(acc)