-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathnodes.go
52 lines (41 loc) · 1.33 KB
/
nodes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package exporter
import (
"regexp"
"strings"
"github.com/gomodule/redigo/redis"
log "github.com/sirupsen/logrus"
)
var reNodeAddress = regexp.MustCompile(`^(?P<ip>.+):(?P<port>\d+)@(?P<cport>\d+)(?:,(?P<hostname>.+))?`)
func (e *Exporter) getClusterNodes(c redis.Conn) ([]string, error) {
output, err := redis.String(doRedisCmd(c, "CLUSTER", "NODES"))
if err != nil {
log.Errorf("Error getting cluster nodes: %s", err)
return nil, err
}
lines := strings.Split(output, "\n")
nodes := []string{}
for _, line := range lines {
if node, ok := parseClusterNodeString(line); ok {
nodes = append(nodes, node)
}
}
return nodes, nil
}
/*
<id> <ip:port@cport[,hostname]> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
eaf69c70d876558a948ba62af0884a37d42c9627 127.0.0.1:7002@17002 master - 0 1742836359057 3 connected 10923-16383
*/
func parseClusterNodeString(node string) (string, bool) {
log.Debugf("parseClusterNodeString node: [%s]", node)
fields := strings.Fields(node)
if len(fields) < 2 {
log.Debugf("Invalid field count for node: %s", node)
return "", false
}
address := reNodeAddress.FindStringSubmatch(fields[1])
if len(address) < 3 {
log.Debugf("Invalid format for node address, got: %s", fields[1])
return "", false
}
return address[1] + ":" + address[2], true
}