Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify alternative path to varnishstat #19

Merged
merged 1 commit into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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