| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package redis |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | |
| 8 | "github.com/redis/go-redis/v9" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/tlscfg" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 12 | ) |
| 13 | |
| 14 | func (c *Collector) validateConfig() error { |
| 15 | if c.Address == "" { |
| 16 | return errors.New("'address' not set") |
| 17 | } |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func (c *Collector) initRedisClient() (*redis.Client, error) { |
| 22 | opts, err := redis.ParseURL(c.Address) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | |
| 27 | tlsConfig, err := tlscfg.NewTLSConfig(c.TLSConfig) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | |
| 32 | if opts.TLSConfig != nil && tlsConfig != nil { |
| 33 | tlsConfig.ServerName = opts.TLSConfig.ServerName |
| 34 | } |
| 35 | |
| 36 | if opts.Username == "" && c.Username != "" { |
| 37 | opts.Username = c.Username |
| 38 | } |
| 39 | if opts.Password == "" && c.Password != "" { |
| 40 | opts.Password = c.Password |
| 41 | } |
| 42 | |
| 43 | opts.PoolSize = 1 |
| 44 | if tlsConfig != nil { |
| 45 | opts.TLSConfig = tlsConfig |
| 46 | } |
| 47 | opts.DialTimeout = c.Timeout.Duration() |
| 48 | opts.ReadTimeout = c.Timeout.Duration() |
| 49 | opts.WriteTimeout = c.Timeout.Duration() |
| 50 | |
| 51 | return redis.NewClient(opts), nil |
| 52 | } |
| 53 | |
| 54 | func (c *Collector) initCharts() (*collectorapi.Charts, error) { |
| 55 | return redisCharts.Copy(), nil |
| 56 | } |