feat(inputs.redis_sentinel): Allow username and password (#13864)

This commit is contained in:
Joshua Powers 2023-09-11 02:05:17 -06:00 committed by GitHub
parent d807dd3c05
commit 4a5e3a4d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 26 deletions

View File

@ -17,10 +17,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# Read metrics from one or many redis servers # Read metrics from one or many redis servers
[[inputs.redis]] [[inputs.redis]]
## specify servers via a url matching: ## specify servers via a url matching:
## [protocol://][:password]@address[:port] ## [protocol://][username:password]@address[:port]
## e.g. ## e.g.
## tcp://localhost:6379 ## tcp://localhost:6379
## tcp://:password@192.168.99.100 ## tcp://username:password@192.168.99.100
## unix:///var/run/redis.sock ## unix:///var/run/redis.sock
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.
@ -37,11 +37,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# # Can be "string", "integer", or "float" # # Can be "string", "integer", or "float"
# type = "string" # type = "string"
## specify server password ## Specify username and password for ACL auth (Redis 6.0+). You can add this
# password = "s#cr@t%" ## to the server URI above or specify it here. The values here take
## precidence.
## specify username for ACL auth (Redis 6.0+) # username = ""
# username = "default" # password = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"

View File

@ -26,16 +26,17 @@ import (
var sampleConfig string var sampleConfig string
type RedisCommand struct { type RedisCommand struct {
Command []interface{} Command []interface{} `toml:"command"`
Field string Field string `toml:"field"`
Type string Type string `toml:"type"`
} }
type Redis struct { type Redis struct {
Commands []*RedisCommand Commands []*RedisCommand `toml:"commands"`
Servers []string Servers []string `toml:"servers"`
Username string Username string `toml:"username"`
Password string Password string `toml:"passowrd"`
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger `toml:"-"` Log telegraf.Logger `toml:"-"`

View File

@ -1,10 +1,10 @@
# Read metrics from one or many redis servers # Read metrics from one or many redis servers
[[inputs.redis]] [[inputs.redis]]
## specify servers via a url matching: ## specify servers via a url matching:
## [protocol://][:password]@address[:port] ## [protocol://][username:password]@address[:port]
## e.g. ## e.g.
## tcp://localhost:6379 ## tcp://localhost:6379
## tcp://:password@192.168.99.100 ## tcp://username:password@192.168.99.100
## unix:///var/run/redis.sock ## unix:///var/run/redis.sock
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.
@ -21,11 +21,11 @@
# # Can be "string", "integer", or "float" # # Can be "string", "integer", or "float"
# type = "string" # type = "string"
## specify server password ## Specify username and password for ACL auth (Redis 6.0+). You can add this
# password = "s#cr@t%" ## to the server URI above or specify it here. The values here take
## precidence.
## specify username for ACL auth (Redis 6.0+) # username = ""
# username = "default" # password = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"

View File

@ -18,10 +18,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# Read metrics from one or many redis-sentinel servers # Read metrics from one or many redis-sentinel servers
[[inputs.redis_sentinel]] [[inputs.redis_sentinel]]
## specify servers via a url matching: ## specify servers via a url matching:
## [protocol://][:password]@address[:port] ## [protocol://][username:password]@address[:port]
## e.g. ## e.g.
## tcp://localhost:26379 ## tcp://localhost:26379
## tcp://:password@192.168.99.100 ## tcp://username:password@192.168.99.100
## unix:///var/run/redis-sentinel.sock ## unix:///var/run/redis-sentinel.sock
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.

View File

@ -65,9 +65,14 @@ func (r *RedisSentinel) Init() error {
return fmt.Errorf("unable to parse to address %q: %w", serv, err) return fmt.Errorf("unable to parse to address %q: %w", serv, err)
} }
username := ""
password := "" password := ""
if u.User != nil { if u.User != nil {
password, _ = u.User.Password() username = u.User.Username()
pw, ok := u.User.Password()
if ok {
password = pw
}
} }
var address string var address string
@ -88,6 +93,7 @@ func (r *RedisSentinel) Init() error {
sentinel := redis.NewSentinelClient( sentinel := redis.NewSentinelClient(
&redis.Options{ &redis.Options{
Addr: address, Addr: address,
Username: username,
Password: password, Password: password,
Network: u.Scheme, Network: u.Scheme,
PoolSize: 1, PoolSize: 1,

View File

@ -1,10 +1,10 @@
# Read metrics from one or many redis-sentinel servers # Read metrics from one or many redis-sentinel servers
[[inputs.redis_sentinel]] [[inputs.redis_sentinel]]
## specify servers via a url matching: ## specify servers via a url matching:
## [protocol://][:password]@address[:port] ## [protocol://][username:password]@address[:port]
## e.g. ## e.g.
## tcp://localhost:26379 ## tcp://localhost:26379
## tcp://:password@192.168.99.100 ## tcp://username:password@192.168.99.100
## unix:///var/run/redis-sentinel.sock ## unix:///var/run/redis-sentinel.sock
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.