Skip to content

Commit

Permalink
Add environment variables as config options
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
Mohamad Arab committed Mar 10, 2019
1 parent b9efcda commit b3ed1d9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ Simple usage:
target_groups:
- targets: ['localhost:9301']

To get all the parameteres
To get all the parameteres, command line arguments always override default and environment variables configs:

squid-exporter -help

The following environmnet variables can be used to override default parameters:

```
SQUID_EXPORTER_LISTEN
SQUID_EXPORTER_METRICS_PATH
SQUID_HOSTNAME
SQUID_PORT
SQUID_LOGIN
SQUID_PASSWORD
```

Usage with docker:
------
Expand All @@ -38,6 +48,10 @@ Setup with Squid running on a different host

docker run -p 9301:9301 -d boynux/squid-exporter -squid-hostname "192.168.0.2" -squid-port 3128 -listen ":9301"

With environment variables

docker run -p 9301:9301 -d -e SQUID_PORT="3128" -e SQUID_HOSTNAME="192.168.0.2" -e SQUID_EXPORTER_LISTEN=":9301" boynux/squid-exporter


Build:
--------
Expand Down
43 changes: 37 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"flag"
"log"
"strconv"
"os"
)

const (
Expand Down Expand Up @@ -32,17 +35,45 @@ type Config struct {
func NewConfig() *Config {
c := &Config{}

flag.StringVar(&c.ListenAddress, "listen", defaultListenAddress, "Address and Port to bind exporter, in host:port format")
flag.StringVar(&c.MetricPath, "metrics-path", defaultMetricsPath, "Metrics path to expose prometheus metrics")
flag.StringVar(&c.ListenAddress, "listen",
loadEnvStringVar("SQUID_EXPORTER_LISTEN", defaultListenAddress), "Address and Port to bind exporter, in host:port format")
flag.StringVar(&c.MetricPath, "metrics-path",
loadEnvStringVar("SQUID_EXPORTER_METRICS_PATH", defaultMetricsPath), "Metrics path to expose prometheus metrics")

flag.StringVar(&c.SquidHostname, "squid-hostname", defaultSquidHostname, "Squid hostname")
flag.IntVar(&c.SquidPort, "squid-port", defaultSquidPort, "Squid port to read metrics")
flag.StringVar(&c.SquidHostname, "squid-hostname",
loadEnvStringVar("SQUID_HOSTNAME", defaultSquidHostname), "Squid hostname")
flag.IntVar(&c.SquidPort, "squid-port",
loadEnvIntVar("SQUID_PORT", defaultSquidPort), "Squid port to read metrics")

flag.StringVar(&c.Login, "squid-login", loadEnvStringVar("SQUID_LOGIN", ""), "Login to squid service")
flag.StringVar(&c.Password, "squid-password", loadEnvStringVar("SQUID_PASSWORD", ""), "Password to squid service")

flag.StringVar(&c.Login, "squid-login", "", "Login to squid service")
flag.StringVar(&c.Password, "squid-password", "", "Password to squid service")
versionFlag = flag.Bool("version", false, "Print the version and exit")

flag.Parse()

return c
}

func loadEnvStringVar(key, def string) string {
val := os.Getenv(key)
if val == "" {
return def
}

return val
}

func loadEnvIntVar(key string, def int) int {
valStr := os.Getenv(key)
if valStr != "" {
val, err := strconv.ParseInt(valStr, 0, 32)
if err == nil {
return int(val)
}

log.Printf("Error parsing %s='%s'. Integer value expected", key, valStr)
}

return def
}

0 comments on commit b3ed1d9

Please sign in to comment.