diff --git a/README.md b/README.md index 4085efc4..1db4f1bc 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,18 @@ and adjust the host name accordingly. Name | Description -------------------|------------ redis.addr | Address of one or more redis nodes, comma separated, defaults to `localhost:6379`. +redis.password | Password to use when authenticating to Redis namespace | Namespace for the metrics, defaults to `redis`. web.listen-address | Address to listen on for web interface and telemetry, defaults to `0.0.0.0:9121`. web.telemetry-path | Path under which to expose metrics, defaults to `metrics`. +These settings take precedence over any configurations provided by [environment variables](#environment-variables). + +### Environment Variables + +Name | Description +-------------------|------------ +REDIS_PASSWORD | Password to use when authenticating to Redis ### What's exported? diff --git a/redis_exporter.go b/redis_exporter.go index 5c1e9c1b..7402c2da 100644 --- a/redis_exporter.go +++ b/redis_exporter.go @@ -5,6 +5,7 @@ import ( // "fmt" "log" "net/http" + "os" "strconv" "strings" "sync" @@ -16,13 +17,19 @@ import ( var ( redisAddr = flag.String("redis.addr", "localhost:6379", "Address of one or more redis nodes, comma separated") + redisPassword = flag.String("redis.password", os.Getenv("REDIS_PASSWORD"), "Password for Redis server") namespace = flag.String("namespace", "redis", "Namespace for metrics") listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.") metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") ) +type RedisHost struct { + addrs []string + password string +} + type Exporter struct { - addrs []string + redis RedisHost namespace string duration prometheus.Gauge scrapeErrors prometheus.Gauge @@ -58,9 +65,9 @@ func (e *Exporter) initGauges() { }, []string{"addr", "db"}) } -func NewRedisExporter(addrs []string, namespace string) *Exporter { +func NewRedisExporter(redis RedisHost, namespace string) *Exporter { e := Exporter{ - addrs: addrs, + redis: redis, namespace: namespace, duration: prometheus.NewGauge(prometheus.GaugeOpts{ @@ -219,7 +226,7 @@ func (e *Exporter) scrape(scrapes chan<- scrapeResult) { //var err error errorCount := 0 - for _, addr := range e.addrs { + for _, addr := range e.redis.addrs { // log.Printf("opening connection to redis node %s", addr) c, err := redis.Dial("tcp", addr) if err != nil { @@ -227,6 +234,13 @@ func (e *Exporter) scrape(scrapes chan<- scrapeResult) { errorCount++ continue } + if e.redis.password != "" { + if _, err := c.Do("AUTH", e.redis.password); err != nil { + log.Printf("redis err: %s", err) + errorCount++ + continue + } + } info, err := redis.String(c.Do("INFO")) c.Close() if err != nil { @@ -276,7 +290,7 @@ func main() { log.Fatal("Invalid parameter --redis.addr") } - e := NewRedisExporter(addrs, *namespace) + e := NewRedisExporter(RedisHost{addrs, *redisPassword}, *namespace) prometheus.MustRegister(e) http.Handle(*metricPath, prometheus.Handler()) diff --git a/redis_exporter_test.go b/redis_exporter_test.go index de3737d3..75c64939 100644 --- a/redis_exporter_test.go +++ b/redis_exporter_test.go @@ -71,7 +71,7 @@ func deleteKeysFromDB(t *testing.T, db string) error { func TestCountingKeys(t *testing.T) { - e := NewRedisExporter(addrs, "test") + e := NewRedisExporter(RedisHost{addrs: addrs}, "test") scrapes := make(chan scrapeResult) go e.scrape(scrapes) @@ -118,7 +118,7 @@ func TestCountingKeys(t *testing.T) { func TestExporter(t *testing.T) { - e := NewRedisExporter(addrs, "test") + e := NewRedisExporter(RedisHost{addrs: addrs}, "test") scrapes := make(chan scrapeResult) go e.scrape(scrapes)