From 19bcc7b7497ff8abe4e69b8f7b9dc5fca8580471 Mon Sep 17 00:00:00 2001 From: Zdenek Styblik Date: Sun, 8 Oct 2017 19:39:25 +0200 Subject: [PATCH] Allow to specify alternative path to varnishstat (#19) --- main.go | 17 ++++++++++------- varnish.go | 12 ++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 9e7bb0f..35977e3 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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.") diff --git a/varnish.go b/varnish.go index 6128f12..b740610 100644 --- a/varnish.go +++ b/varnish.go @@ -15,10 +15,6 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -const ( - varnishstatExe = "varnishstat" -) - var ( descCache = make(map[string]*prometheus.Desc) mDescCache sync.RWMutex @@ -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) } @@ -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 @@ -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 }