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:
parent
8b4708c82a
commit
ffa8a4a716
|
@ -17,13 +17,14 @@ import (
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"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
|
// Varnish is used to store configuration values
|
||||||
type Varnish struct {
|
type Varnish struct {
|
||||||
Stats []string
|
Stats []string
|
||||||
Binary string
|
Binary string
|
||||||
UseSudo bool
|
UseSudo bool
|
||||||
|
InstanceName string
|
||||||
|
|
||||||
filter filter.Filter
|
filter filter.Filter
|
||||||
run runner
|
run runner
|
||||||
|
@ -44,6 +45,10 @@ var sampleConfig = `
|
||||||
## Glob matching can be used, ie, stats = ["MAIN.*"]
|
## Glob matching can be used, ie, stats = ["MAIN.*"]
|
||||||
## stats may also be set to ["*"], which will collect all stats
|
## stats may also be set to ["*"], which will collect all stats
|
||||||
stats = ["MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"]
|
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 {
|
func (s *Varnish) Description() string {
|
||||||
|
@ -56,8 +61,13 @@ func (s *Varnish) SampleConfig() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shell out to varnish_stat and return the output
|
// 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"}
|
cmdArgs := []string{"-1"}
|
||||||
|
|
||||||
|
if InstanceName != "" {
|
||||||
|
cmdArgs = append(cmdArgs, []string{"-n", InstanceName}...)
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(cmdName, cmdArgs...)
|
cmd := exec.Command(cmdName, cmdArgs...)
|
||||||
|
|
||||||
if UseSudo {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("error gathering metrics: %s", err)
|
return fmt.Errorf("error gathering metrics: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -155,10 +165,11 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error {
|
||||||
func init() {
|
func init() {
|
||||||
inputs.Add("varnish", func() telegraf.Input {
|
inputs.Add("varnish", func() telegraf.Input {
|
||||||
return &Varnish{
|
return &Varnish{
|
||||||
run: varnishRunner,
|
run: varnishRunner,
|
||||||
Stats: defaultStats,
|
Stats: defaultStats,
|
||||||
Binary: defaultBinary,
|
Binary: defaultBinary,
|
||||||
UseSudo: false,
|
UseSudo: false,
|
||||||
|
InstanceName: "",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,15 @@ package varnish
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/influxdata/telegraf/testutil"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/testutil"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fakeVarnishStat(output string, useSudo bool) 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) (*bytes.Buffer, error) {
|
return func(string, bool, string) (*bytes.Buffer, error) {
|
||||||
return bytes.NewBuffer([]byte(output)), nil
|
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) {
|
func TestGather(t *testing.T) {
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
v := &Varnish{
|
v := &Varnish{
|
||||||
run: fakeVarnishStat(smOutput, false),
|
run: fakeVarnishStat(smOutput, false, ""),
|
||||||
Stats: []string{"*"},
|
Stats: []string{"*"},
|
||||||
}
|
}
|
||||||
v.Gather(acc)
|
v.Gather(acc)
|
||||||
|
@ -36,7 +37,7 @@ func TestGather(t *testing.T) {
|
||||||
func TestParseFullOutput(t *testing.T) {
|
func TestParseFullOutput(t *testing.T) {
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
v := &Varnish{
|
v := &Varnish{
|
||||||
run: fakeVarnishStat(fullOutput, true),
|
run: fakeVarnishStat(fullOutput, true, ""),
|
||||||
Stats: []string{"*"},
|
Stats: []string{"*"},
|
||||||
}
|
}
|
||||||
err := v.Gather(acc)
|
err := v.Gather(acc)
|
||||||
|
@ -51,7 +52,7 @@ func TestParseFullOutput(t *testing.T) {
|
||||||
func TestFilterSomeStats(t *testing.T) {
|
func TestFilterSomeStats(t *testing.T) {
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
v := &Varnish{
|
v := &Varnish{
|
||||||
run: fakeVarnishStat(fullOutput, false),
|
run: fakeVarnishStat(fullOutput, false, ""),
|
||||||
Stats: []string{"MGT.*", "VBE.*"},
|
Stats: []string{"MGT.*", "VBE.*"},
|
||||||
}
|
}
|
||||||
err := v.Gather(acc)
|
err := v.Gather(acc)
|
||||||
|
@ -74,7 +75,7 @@ func TestFieldConfig(t *testing.T) {
|
||||||
for fieldCfg, expected := range expect {
|
for fieldCfg, expected := range expect {
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
v := &Varnish{
|
v := &Varnish{
|
||||||
run: fakeVarnishStat(fullOutput, true),
|
run: fakeVarnishStat(fullOutput, true, ""),
|
||||||
Stats: strings.Split(fieldCfg, ","),
|
Stats: strings.Split(fieldCfg, ","),
|
||||||
}
|
}
|
||||||
err := v.Gather(acc)
|
err := v.Gather(acc)
|
||||||
|
|
Loading…
Reference in New Issue