Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
ability to configure timeout for varnish input plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kamsz committed Jan 2, 2019
1 parent bf4175b commit fbbd885
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions plugins/inputs/varnish/varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

type runner func(cmdName string, UseSudo bool, InstanceName string) (*bytes.Buffer, error)
type runner func(cmdName string, UseSudo bool, InstanceName string, Timeout string) (*bytes.Buffer, error)

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

filter filter.Filter
run runner
}

var defaultStats = []string{"MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"}
var defaultBinary = "/usr/bin/varnishstat"
var defaultTimeout = "1s"

var sampleConfig = `
## If running as a restricted user you can prepend sudo for additional access:
Expand All @@ -49,6 +51,9 @@ var sampleConfig = `
## Optional name for the varnish instance (or working directory) to query
## Usually appened after -n in varnish cli
# instance_name = instanceName
## Timeout for varnishstat command
# timeout = "1s"
`

func (s *Varnish) Description() string {
Expand All @@ -61,7 +66,7 @@ func (s *Varnish) SampleConfig() string {
}

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

if InstanceName != "" {
Expand All @@ -78,7 +83,13 @@ func varnishRunner(cmdName string, UseSudo bool, InstanceName string) (*bytes.Bu

var out bytes.Buffer
cmd.Stdout = &out
err := internal.RunTimeout(cmd, time.Millisecond*500)

duration, err := time.ParseDuration(Timeout)
if err != nil {
return nil, fmt.Errorf("error parsing timeout value: %s", err)
}

err = internal.RunTimeout(cmd, duration)
if err != nil {
return &out, fmt.Errorf("error running varnishstat: %s", err)
}
Expand Down Expand Up @@ -109,7 +120,7 @@ func (s *Varnish) Gather(acc telegraf.Accumulator) error {
}
}

out, err := s.run(s.Binary, s.UseSudo, s.InstanceName)
out, err := s.run(s.Binary, s.UseSudo, s.InstanceName, s.Timeout)
if err != nil {
return fmt.Errorf("error gathering metrics: %s", err)
}
Expand Down Expand Up @@ -170,6 +181,7 @@ func init() {
Binary: defaultBinary,
UseSudo: false,
InstanceName: "",
Timeout: defaultTimeout,
}
})
}

0 comments on commit fbbd885

Please sign in to comment.