2016-05-24 10:06:25 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package varnish
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-05-26 09:40:03 +00:00
|
|
|
"time"
|
|
|
|
|
2016-05-24 10:06:25 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-06-02 17:47:15 +00:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
2016-05-24 10:06:25 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2017-10-27 18:53:59 +00:00
|
|
|
type runner func(cmdName string, UseSudo bool, InstanceName string) (*bytes.Buffer, error)
|
2016-05-24 10:06:25 +00:00
|
|
|
|
|
|
|
// Varnish is used to store configuration values
|
|
|
|
type Varnish struct {
|
2017-10-27 18:53:59 +00:00
|
|
|
Stats []string
|
|
|
|
Binary string
|
|
|
|
UseSudo bool
|
|
|
|
InstanceName string
|
2016-05-26 09:40:03 +00:00
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
filter filter.Filter
|
2016-05-26 09:40:03 +00:00
|
|
|
run runner
|
2016-05-24 10:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultStats = []string{"MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"}
|
|
|
|
var defaultBinary = "/usr/bin/varnishstat"
|
|
|
|
|
2016-05-26 09:40:03 +00:00
|
|
|
var sampleConfig = `
|
2017-08-09 18:38:54 +00:00
|
|
|
## If running as a restricted user you can prepend sudo for additional access:
|
|
|
|
#use_sudo = false
|
|
|
|
|
2016-05-24 10:06:25 +00:00
|
|
|
## The default location of the varnishstat binary can be overridden with:
|
|
|
|
binary = "/usr/bin/varnishstat"
|
|
|
|
|
|
|
|
## By default, telegraf gather stats for 3 metric points.
|
|
|
|
## Setting stats will override the defaults shown below.
|
2016-05-26 09:40:03 +00:00
|
|
|
## Glob matching can be used, ie, stats = ["MAIN.*"]
|
|
|
|
## stats may also be set to ["*"], which will collect all stats
|
2016-05-24 10:06:25 +00:00
|
|
|
stats = ["MAIN.cache_hit", "MAIN.cache_miss", "MAIN.uptime"]
|
2017-10-27 18:53:59 +00:00
|
|
|
|
|
|
|
## Optional name for the varnish instance (or working directory) to query
|
|
|
|
## Usually appened after -n in varnish cli
|
2018-06-05 01:06:59 +00:00
|
|
|
# instance_name = instanceName
|
2016-05-24 10:06:25 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (s *Varnish) Description() string {
|
|
|
|
return "A plugin to collect stats from Varnish HTTP Cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
// SampleConfig displays configuration instructions
|
|
|
|
func (s *Varnish) SampleConfig() string {
|
2016-05-26 09:40:03 +00:00
|
|
|
return sampleConfig
|
2016-05-24 10:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shell out to varnish_stat and return the output
|
2017-10-27 18:53:59 +00:00
|
|
|
func varnishRunner(cmdName string, UseSudo bool, InstanceName string) (*bytes.Buffer, error) {
|
2016-05-24 10:06:25 +00:00
|
|
|
cmdArgs := []string{"-1"}
|
2017-10-27 18:53:59 +00:00
|
|
|
|
|
|
|
if InstanceName != "" {
|
|
|
|
cmdArgs = append(cmdArgs, []string{"-n", InstanceName}...)
|
|
|
|
}
|
|
|
|
|
2016-05-24 10:06:25 +00:00
|
|
|
cmd := exec.Command(cmdName, cmdArgs...)
|
2017-08-09 18:38:54 +00:00
|
|
|
|
|
|
|
if UseSudo {
|
|
|
|
cmdArgs = append([]string{cmdName}, cmdArgs...)
|
|
|
|
cmdArgs = append([]string{"-n"}, cmdArgs...)
|
|
|
|
cmd = exec.Command("sudo", cmdArgs...)
|
|
|
|
}
|
|
|
|
|
2016-05-24 10:06:25 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
err := internal.RunTimeout(cmd, time.Millisecond*200)
|
|
|
|
if err != nil {
|
|
|
|
return &out, fmt.Errorf("error running varnishstat: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather collects the configured stats from varnish_stat and adds them to the
|
|
|
|
// Accumulator
|
|
|
|
//
|
|
|
|
// The prefix of each stat (eg MAIN, MEMPOOL, LCK, etc) will be used as a
|
|
|
|
// 'section' tag and all stats that share that prefix will be reported as fields
|
|
|
|
// with that tag
|
|
|
|
func (s *Varnish) Gather(acc telegraf.Accumulator) error {
|
2016-05-26 09:40:03 +00:00
|
|
|
if s.filter == nil {
|
|
|
|
var err error
|
|
|
|
if len(s.Stats) == 0 {
|
2016-09-05 15:16:37 +00:00
|
|
|
s.filter, err = filter.Compile(defaultStats)
|
2016-05-26 09:40:03 +00:00
|
|
|
} else {
|
|
|
|
// legacy support, change "all" -> "*":
|
|
|
|
if s.Stats[0] == "all" {
|
|
|
|
s.Stats[0] = "*"
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
s.filter, err = filter.Compile(s.Stats)
|
2016-05-26 09:40:03 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 18:53:59 +00:00
|
|
|
out, err := s.run(s.Binary, s.UseSudo, s.InstanceName)
|
2016-05-24 10:06:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error gathering metrics: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionMap := make(map[string]map[string]interface{})
|
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
for scanner.Scan() {
|
|
|
|
cols := strings.Fields(scanner.Text())
|
|
|
|
if len(cols) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.Contains(cols[0], ".") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := cols[0]
|
|
|
|
value := cols[1]
|
|
|
|
|
2016-05-26 09:40:03 +00:00
|
|
|
if s.filter != nil && !s.filter.Match(stat) {
|
2016-05-24 10:06:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(stat, ".", 2)
|
|
|
|
section := parts[0]
|
|
|
|
field := parts[1]
|
|
|
|
|
|
|
|
// Init the section if necessary
|
|
|
|
if _, ok := sectionMap[section]; !ok {
|
|
|
|
sectionMap[section] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2016-05-26 09:40:03 +00:00
|
|
|
sectionMap[section][field], err = strconv.ParseUint(value, 10, 64)
|
2016-05-24 10:06:25 +00:00
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(fmt.Errorf("Expected a numeric value for %s = %v\n",
|
|
|
|
stat, value))
|
2016-05-24 10:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for section, fields := range sectionMap {
|
|
|
|
tags := map[string]string{
|
|
|
|
"section": section,
|
|
|
|
}
|
|
|
|
if len(fields) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("varnish", fields, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-05-26 09:40:03 +00:00
|
|
|
inputs.Add("varnish", func() telegraf.Input {
|
|
|
|
return &Varnish{
|
2017-10-27 18:53:59 +00:00
|
|
|
run: varnishRunner,
|
|
|
|
Stats: defaultStats,
|
|
|
|
Binary: defaultBinary,
|
|
|
|
UseSudo: false,
|
|
|
|
InstanceName: "",
|
2016-05-26 09:40:03 +00:00
|
|
|
}
|
|
|
|
})
|
2016-05-24 10:06:25 +00:00
|
|
|
}
|