Skip to content

Commit

Permalink
Allow to specify alternative path to varnishstat (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
zstyblik authored and Jonne Nauha committed Oct 8, 2017
1 parent b99d22c commit 19bcc7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ var (
ExitHandler = &exitHandler{}

StartParams = &startParams{
ListenAddress: ":9131", // Reserved and publicly announced at https://github.com/prometheus/prometheus/wiki/Default-port-allocations
Path: "/metrics",
Params: &varnishstatParams{},
ListenAddress: ":9131", // Reserved and publicly announced at https://github.com/prometheus/prometheus/wiki/Default-port-allocations
Path: "/metrics",
VarnishstatExe: "varnishstat",
Params: &varnishstatParams{},
}
logger *log.Logger
)

type startParams struct {
ListenAddress string
Path string
HealthPath string
Params *varnishstatParams
ListenAddress string
Path string
HealthPath string
VarnishstatExe string
Params *varnishstatParams

Verbose bool
NoExit bool
Expand Down Expand Up @@ -71,6 +73,7 @@ func init() {
flag.StringVar(&StartParams.HealthPath, "web.health-path", StartParams.HealthPath, "Path under which to expose healthcheck. Disabled unless configured.")

// varnish
flag.StringVar(&StartParams.VarnishstatExe, "varnishstat-path", StartParams.VarnishstatExe, "Path to varnishstat.")
flag.StringVar(&StartParams.Params.Instance, "n", StartParams.Params.Instance, "varnishstat -n value.")
flag.StringVar(&StartParams.Params.VSM, "N", StartParams.Params.VSM, "varnishstat -N value.")

Expand Down
12 changes: 4 additions & 8 deletions varnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const (
varnishstatExe = "varnishstat"
)

var (
descCache = make(map[string]*prometheus.Desc)
mDescCache sync.RWMutex
Expand All @@ -34,9 +30,9 @@ func ScrapeVarnish(ch chan<- prometheus.Metric) ([]byte, error) {
if !StartParams.Params.isEmpty() {
params = append(params, StartParams.Params.make()...)
}
buf, errExec := executeVarnishstat(params...)
buf, errExec := executeVarnishstat(StartParams.VarnishstatExe, params...)
if errExec != nil {
return buf.Bytes(), fmt.Errorf("%s scrape failed: %s", varnishstatExe, errExec)
return buf.Bytes(), fmt.Errorf("%s scrape failed: %s", StartParams.VarnishstatExe, errExec)
}
return ScrapeVarnishFrom(buf.Bytes(), ch)
}
Expand Down Expand Up @@ -118,7 +114,7 @@ func ScrapeVarnishFrom(buf []byte, ch chan<- prometheus.Metric) ([]byte, error)
}

// Returns the result of 'varnishtat' with optional command line params.
func executeVarnishstat(params ...string) (*bytes.Buffer, error) {
func executeVarnishstat(varnishstatExe string, params ...string) (*bytes.Buffer, error) {
buf := &bytes.Buffer{}
cmd := exec.Command(varnishstatExe, params...)
cmd.Stdout = buf
Expand Down Expand Up @@ -159,7 +155,7 @@ func (v *varnishVersion) Initialize() error {
}

func (v *varnishVersion) queryVersion() error {
buf, err := executeVarnishstat("-V")
buf, err := executeVarnishstat(StartParams.VarnishstatExe, "-V")
if err != nil {
return err
}
Expand Down

0 comments on commit 19bcc7b

Please sign in to comment.