Add instance name option to varnish plugin (#3398)

This change add a new configuration option to allow probing of
namespaced varnish instances, usually reached using the '-n' switch on
the varnish cli.
This commit is contained in:
Maximilien Richer 2017-10-27 20:53:59 +02:00 committed by Daniel Nelson
parent b78b8f2b50
commit 626ff99b6e
2 changed files with 30 additions and 18 deletions

View File

@ -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
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)
}
@ -159,6 +169,7 @@ func init() {
Stats: defaultStats,
Binary: defaultBinary,
UseSudo: false,
InstanceName: "",
}
})
}

View File

@ -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)