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

Add instance option to varnish plugin #3398

Merged
merged 1 commit into from
Oct 27, 2017
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
31 changes: 21 additions & 10 deletions plugins/inputs/varnish/varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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: "",
}
})
}
17 changes: 9 additions & 8 deletions plugins/inputs/varnish/varnish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ 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
}
}

func TestGather(t *testing.T) {
acc := &testutil.Accumulator{}
v := &Varnish{
run: fakeVarnishStat(smOutput, false),
run: fakeVarnishStat(smOutput, false, ""),
Stats: []string{"*"},
}
v.Gather(acc)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down