Skip to content

Commit

Permalink
Merge pull request #1144 from mackerelio/redis-tls
Browse files Browse the repository at this point in the history
added TLS support on mackerel-plugin-redis
  • Loading branch information
yseto authored Dec 26, 2023
2 parents 021564f + ec7727d commit 6d74a39
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mackerel-plugin-redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Redis custom metrics plugin for mackerel.io agent.
## Synopsis

```shell
mackerel-plugin-redis [-host=<hostname>] [-port=<port>] [-password=<password>] [-socket=<unix socket>] [-timeout=<time>] [-metric-key-prefix=<prefix>] [-config-command=<CONFIG command name>]
mackerel-plugin-redis [-host=<hostname>] [-port=<port>] [-password=<password>] [-socket=<unix socket>] [-timeout=<time>] [-metric-key-prefix=<prefix>] [-config-command=<CONFIG command name>] [-tls] [-tls-skip-verify]
```

## Example of mackerel-agent.conf
Expand Down
19 changes: 16 additions & 3 deletions mackerel-plugin-redis/lib/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mpredis

import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
Expand Down Expand Up @@ -36,6 +37,9 @@ type RedisPlugin struct {
Timeout int
Tempfile string
ConfigCommand string

EnableTLS bool
InsecureSkipVerify bool
}

func (m *RedisPlugin) configCmd(key string) (*redis.MapStringStringCmd, error) {
Expand Down Expand Up @@ -125,14 +129,19 @@ func (m *RedisPlugin) Connect() {
network = "unix"
address = m.Socket
}

m.rdb = redis.NewClient(&redis.Options{
options := &redis.Options{
Addr: address,
Password: m.Password,
DB: 0,
Network: network,
DialTimeout: time.Duration(m.Timeout) * time.Second,
})
}
if m.EnableTLS {
options.TLSConfig = &tls.Config{
InsecureSkipVerify: m.InsecureSkipVerify,
}
}
m.rdb = redis.NewClient(options)
}

// FetchMetrics interface for mackerelplugin
Expand Down Expand Up @@ -368,6 +377,8 @@ func Do() {
optTimeout := flag.Int("timeout", 5, "Timeout")
optTempfile := flag.String("tempfile", "", "Temp file name")
optConfigCommand := flag.String("config-command", "CONFIG", "Custom CONFIG command. Disable CONFIG command when passed \"\".")
optEnableTLS := flag.Bool("tls", false, "Enables TLS connection")
optTLSSkipVerify := flag.Bool("tls-skip-verify", false, "Disable TLS certificate verification")

flag.Parse()

Expand All @@ -386,6 +397,8 @@ func Do() {
redis.Host = *optHost
redis.Port = *optPort
redis.Password = *optPassword
redis.EnableTLS = *optEnableTLS
redis.InsecureSkipVerify = *optTLSSkipVerify
}
redis.Connect()
defer redis.rdb.Close()
Expand Down

0 comments on commit 6d74a39

Please sign in to comment.