Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert Redis to use URI, support Redis AUTH #16

Merged
merged 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions plugins/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"sync"
Expand All @@ -20,8 +21,9 @@ type Redis struct {
}

var sampleConfig = `
# An array of address to gather stats about. Specify an ip on hostname
# with optional port. ie localhost, 10.10.3.33:18832, etc.
# An array of URI to gather stats about. Specify an ip or hostname
# with optional port add password. ie redis://localhost, redis://10.10.3.33:18832,
# 10.0.0.1:10000, etc.
#
# If no servers are specified, then localhost is used as the host.
servers = ["localhost"]`
Expand Down Expand Up @@ -73,7 +75,10 @@ var ErrProtocolError = errors.New("redis protocol error")
// Returns one of the errors encountered while gather stats (if any).
func (g *Redis) Gather(acc plugins.Accumulator) error {
if len(g.Servers) == 0 {
g.gatherServer(":6379", acc)
url := &url.URL{
Host: ":6379",
}
g.gatherServer(url, acc)
return nil
}

Expand All @@ -82,10 +87,17 @@ func (g *Redis) Gather(acc plugins.Accumulator) error {
var outerr error

for _, serv := range g.Servers {
u, err := url.Parse(serv)
if err != nil {
return fmt.Errorf("Unable to parse to address '%s': %s", serv, err)
} else if u.Scheme == "" {
// fallback to simple string based address (i.e. "10.0.0.1:10000")
u.Host = serv
}
wg.Add(1)
go func(serv string) {
defer wg.Done()
outerr = g.gatherServer(serv, acc)
outerr = g.gatherServer(u, acc)
}(serv)
}

Expand All @@ -96,17 +108,34 @@ func (g *Redis) Gather(acc plugins.Accumulator) error {

const defaultPort = "6379"

func (g *Redis) gatherServer(addr string, acc plugins.Accumulator) error {
func (g *Redis) gatherServer(addr *url.URL, acc plugins.Accumulator) error {
if g.c == nil {

_, _, err := net.SplitHostPort(addr)
_, _, err := net.SplitHostPort(addr.Host)
if err != nil {
addr = addr + ":" + defaultPort
addr.Host = addr.Host + ":" + defaultPort
}

c, err := net.Dial("tcp", addr)
c, err := net.Dial("tcp", addr.Host)
if err != nil {
return fmt.Errorf("Unable to connect to redis server '%s': %s", addr, err)
return fmt.Errorf("Unable to connect to redis server '%s': %s", addr.Host, err)
}

if addr.User != nil {
pwd, set := addr.User.Password()
if set && pwd != "" {
c.Write([]byte(fmt.Sprintf("AUTH %s\n", pwd)))

r := bufio.NewReader(c)

line, err := r.ReadString('\n')
if err != nil {
return err
}
if line[0] != '+' {
return fmt.Errorf("%s", strings.TrimSpace(line)[1:])
}
}
}

g.c = c
Expand Down
4 changes: 2 additions & 2 deletions plugins/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestRedisGeneratesMetrics(t *testing.T) {
}
}()

addr := l.Addr().String()
addr := fmt.Sprintf("redis://%s", l.Addr().String())

r := &Redis{
Servers: []string{addr},
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestRedisCanPullStatsFromMultipleServers(t *testing.T) {
}
}()

addr := l.Addr().String()
addr := fmt.Sprintf("redis://%s", l.Addr().String())

r := &Redis{
Servers: []string{addr},
Expand Down