-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert Redis to use URI, support Redis AUTH
- Loading branch information
1 parent
051cd03
commit c9e2bfa
Showing
1 changed file
with
35 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
@@ -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 | ||
# redis://x:[email protected]:10000, etc. | ||
# | ||
# If no servers are specified, then localhost is used as the host. | ||
servers = ["localhost"]` | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -82,10 +87,14 @@ 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) | ||
} | ||
wg.Add(1) | ||
go func(serv string) { | ||
defer wg.Done() | ||
outerr = g.gatherServer(serv, acc) | ||
outerr = g.gatherServer(u, acc) | ||
}(serv) | ||
} | ||
|
||
|
@@ -96,17 +105,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 | ||
|