2015-04-07 18:54:21 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-06-20 18:40:20 +00:00
|
|
|
"net/url"
|
2015-09-23 01:13:35 +00:00
|
|
|
"strconv"
|
2015-04-07 18:54:21 +00:00
|
|
|
"strings"
|
2015-05-18 16:26:10 +00:00
|
|
|
"sync"
|
2016-02-29 16:52:58 +00:00
|
|
|
"time"
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-04-07 18:54:21 +00:00
|
|
|
)
|
|
|
|
|
2015-05-18 16:26:10 +00:00
|
|
|
type Redis struct {
|
2015-05-18 22:10:11 +00:00
|
|
|
Servers []string
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:10:11 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## specify servers via a url matching:
|
|
|
|
## [protocol://][:password]@address[:port]
|
|
|
|
## e.g.
|
|
|
|
## tcp://localhost:6379
|
|
|
|
## tcp://:password@192.168.99.100
|
|
|
|
##
|
|
|
|
## If no servers are specified, then localhost is used as the host.
|
|
|
|
## If no port is specified, 6379 is used
|
2015-10-15 21:53:29 +00:00
|
|
|
servers = ["tcp://localhost:6379"]
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-05-18 22:10:11 +00:00
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
var defaultTimeout = 5 * time.Second
|
|
|
|
|
2015-05-18 22:10:11 +00:00
|
|
|
func (r *Redis) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Redis) Description() string {
|
|
|
|
return "Read metrics from one or many redis servers"
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:54:21 +00:00
|
|
|
var Tracking = map[string]string{
|
2015-05-18 19:15:15 +00:00
|
|
|
"uptime_in_seconds": "uptime",
|
|
|
|
"connected_clients": "clients",
|
|
|
|
"used_memory": "used_memory",
|
|
|
|
"used_memory_rss": "used_memory_rss",
|
|
|
|
"used_memory_peak": "used_memory_peak",
|
|
|
|
"used_memory_lua": "used_memory_lua",
|
|
|
|
"rdb_changes_since_last_save": "rdb_changes_since_last_save",
|
|
|
|
"total_connections_received": "total_connections_received",
|
|
|
|
"total_commands_processed": "total_commands_processed",
|
|
|
|
"instantaneous_ops_per_sec": "instantaneous_ops_per_sec",
|
2015-12-02 15:08:56 +00:00
|
|
|
"instantaneous_input_kbps": "instantaneous_input_kbps",
|
|
|
|
"instantaneous_output_kbps": "instantaneous_output_kbps",
|
2015-05-18 19:15:15 +00:00
|
|
|
"sync_full": "sync_full",
|
|
|
|
"sync_partial_ok": "sync_partial_ok",
|
|
|
|
"sync_partial_err": "sync_partial_err",
|
|
|
|
"expired_keys": "expired_keys",
|
|
|
|
"evicted_keys": "evicted_keys",
|
|
|
|
"keyspace_hits": "keyspace_hits",
|
|
|
|
"keyspace_misses": "keyspace_misses",
|
|
|
|
"pubsub_channels": "pubsub_channels",
|
|
|
|
"pubsub_patterns": "pubsub_patterns",
|
|
|
|
"latest_fork_usec": "latest_fork_usec",
|
|
|
|
"connected_slaves": "connected_slaves",
|
|
|
|
"master_repl_offset": "master_repl_offset",
|
|
|
|
"repl_backlog_active": "repl_backlog_active",
|
|
|
|
"repl_backlog_size": "repl_backlog_size",
|
|
|
|
"repl_backlog_histlen": "repl_backlog_histlen",
|
|
|
|
"mem_fragmentation_ratio": "mem_fragmentation_ratio",
|
|
|
|
"used_cpu_sys": "used_cpu_sys",
|
|
|
|
"used_cpu_user": "used_cpu_user",
|
|
|
|
"used_cpu_sys_children": "used_cpu_sys_children",
|
|
|
|
"used_cpu_user_children": "used_cpu_user_children",
|
2016-05-18 13:17:14 +00:00
|
|
|
"role": "role",
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ErrProtocolError = errors.New("redis protocol error")
|
|
|
|
|
2015-05-18 16:26:10 +00:00
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
2016-01-27 21:21:36 +00:00
|
|
|
func (r *Redis) Gather(acc telegraf.Accumulator) error {
|
2015-08-31 21:57:52 +00:00
|
|
|
if len(r.Servers) == 0 {
|
2015-06-20 18:40:20 +00:00
|
|
|
url := &url.URL{
|
|
|
|
Host: ":6379",
|
|
|
|
}
|
2015-08-31 21:57:52 +00:00
|
|
|
r.gatherServer(url, acc)
|
2015-05-18 18:53:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
var outerr error
|
|
|
|
|
2015-08-31 21:57:52 +00:00
|
|
|
for _, serv := range r.Servers {
|
2015-06-20 18:40:20 +00:00
|
|
|
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")
|
2015-06-23 21:51:55 +00:00
|
|
|
u.Scheme = "tcp"
|
2015-06-20 18:40:20 +00:00
|
|
|
u.Host = serv
|
2015-06-23 21:51:55 +00:00
|
|
|
u.Path = ""
|
2015-06-20 18:40:20 +00:00
|
|
|
}
|
2015-05-18 16:26:10 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(serv string) {
|
|
|
|
defer wg.Done()
|
2015-08-31 21:57:52 +00:00
|
|
|
outerr = r.gatherServer(u, acc)
|
2015-05-18 16:26:10 +00:00
|
|
|
}(serv)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return outerr
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:22:04 +00:00
|
|
|
const defaultPort = "6379"
|
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (r *Redis) gatherServer(addr *url.URL, acc telegraf.Accumulator) error {
|
2015-09-10 17:21:40 +00:00
|
|
|
_, _, err := net.SplitHostPort(addr.Host)
|
|
|
|
if err != nil {
|
2015-11-11 22:33:58 +00:00
|
|
|
addr.Host = addr.Host + ":" + defaultPort
|
2015-09-10 17:21:40 +00:00
|
|
|
}
|
2015-05-18 22:22:04 +00:00
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
c, err := net.DialTimeout("tcp", addr.Host, defaultTimeout)
|
2015-09-10 17:21:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to connect to redis server '%s': %s", addr.Host, err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2015-06-20 18:40:20 +00:00
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
// Extend connection
|
|
|
|
c.SetDeadline(time.Now().Add(defaultTimeout))
|
|
|
|
|
2015-09-10 17:21:40 +00:00
|
|
|
if addr.User != nil {
|
|
|
|
pwd, set := addr.User.Password()
|
|
|
|
if set && pwd != "" {
|
|
|
|
c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd)))
|
2015-06-20 18:40:20 +00:00
|
|
|
|
2015-09-10 17:21:40 +00:00
|
|
|
rdr := bufio.NewReader(c)
|
2015-06-20 18:40:20 +00:00
|
|
|
|
2015-09-10 17:21:40 +00:00
|
|
|
line, err := rdr.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if line[0] != '+' {
|
|
|
|
return fmt.Errorf("%s", strings.TrimSpace(line)[1:])
|
2015-06-20 18:40:20 +00:00
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:13:35 +00:00
|
|
|
c.Write([]byte("INFO\r\n"))
|
|
|
|
c.Write([]byte("EOF\r\n"))
|
2015-09-10 17:21:40 +00:00
|
|
|
rdr := bufio.NewReader(c)
|
2015-09-23 01:13:35 +00:00
|
|
|
|
|
|
|
// Setup tags for all redis metrics
|
2015-09-23 19:00:23 +00:00
|
|
|
host, port := "unknown", "unknown"
|
|
|
|
// If there's an error, ignore and use 'unknown' tags
|
|
|
|
host, port, _ = net.SplitHostPort(addr.Host)
|
2015-11-02 18:29:28 +00:00
|
|
|
tags := map[string]string{"server": host, "port": port}
|
2015-09-23 01:13:35 +00:00
|
|
|
|
|
|
|
return gatherInfoOutput(rdr, acc, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gatherInfoOutput gathers
|
|
|
|
func gatherInfoOutput(
|
|
|
|
rdr *bufio.Reader,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-09-23 01:13:35 +00:00
|
|
|
tags map[string]string,
|
|
|
|
) error {
|
2015-10-18 04:05:56 +00:00
|
|
|
var keyspace_hits, keyspace_misses uint64 = 0, 0
|
|
|
|
|
2015-09-22 22:59:34 +00:00
|
|
|
scanner := bufio.NewScanner(rdr)
|
2015-12-15 17:08:13 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-09-22 22:59:34 +00:00
|
|
|
for scanner.Scan() {
|
2015-09-23 01:13:35 +00:00
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Contains(line, "ERR") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(line, ":", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := string(parts[0])
|
|
|
|
metric, ok := Tracking[name]
|
|
|
|
if !ok {
|
|
|
|
kline := strings.TrimSpace(string(parts[1]))
|
|
|
|
gatherKeyspaceLine(name, kline, acc, tags)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2015-09-23 01:13:35 +00:00
|
|
|
val := strings.TrimSpace(parts[1])
|
|
|
|
ival, err := strconv.ParseUint(val, 10, 64)
|
2015-10-18 04:05:56 +00:00
|
|
|
|
|
|
|
if name == "keyspace_hits" {
|
|
|
|
keyspace_hits = ival
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "keyspace_misses" {
|
|
|
|
keyspace_misses = ival
|
|
|
|
}
|
|
|
|
|
2016-05-18 13:17:14 +00:00
|
|
|
if name == "role" {
|
|
|
|
tags["role"] = val
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:13:35 +00:00
|
|
|
if err == nil {
|
2015-12-15 17:08:13 +00:00
|
|
|
fields[metric] = ival
|
2015-09-23 01:13:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fval, err := strconv.ParseFloat(val, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2015-12-15 17:08:13 +00:00
|
|
|
fields[metric] = fval
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
2015-10-18 04:05:56 +00:00
|
|
|
var keyspace_hitrate float64 = 0.0
|
|
|
|
if keyspace_hits != 0 || keyspace_misses != 0 {
|
|
|
|
keyspace_hitrate = float64(keyspace_hits) / float64(keyspace_hits+keyspace_misses)
|
|
|
|
}
|
2015-12-15 17:08:13 +00:00
|
|
|
fields["keyspace_hitrate"] = keyspace_hitrate
|
|
|
|
acc.AddFields("redis", fields, tags)
|
2015-04-07 18:54:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:13:35 +00:00
|
|
|
// Parse the special Keyspace line at end of redis stats
|
|
|
|
// This is a special line that looks something like:
|
|
|
|
// db0:keys=2,expires=0,avg_ttl=0
|
|
|
|
// And there is one for each db on the redis instance
|
|
|
|
func gatherKeyspaceLine(
|
|
|
|
name string,
|
|
|
|
line string,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2015-09-23 01:13:35 +00:00
|
|
|
tags map[string]string,
|
|
|
|
) {
|
|
|
|
if strings.Contains(line, "keys=") {
|
2015-12-15 17:08:13 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-09-23 01:13:35 +00:00
|
|
|
tags["database"] = name
|
|
|
|
dbparts := strings.Split(line, ",")
|
|
|
|
for _, dbp := range dbparts {
|
|
|
|
kv := strings.Split(dbp, "=")
|
|
|
|
ival, err := strconv.ParseUint(kv[1], 10, 64)
|
|
|
|
if err == nil {
|
2015-12-15 17:08:13 +00:00
|
|
|
fields[kv[0]] = ival
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 17:08:13 +00:00
|
|
|
acc.AddFields("redis_keyspace", fields, tags)
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:54:21 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("redis", func() telegraf.Input {
|
2015-05-18 16:26:10 +00:00
|
|
|
return &Redis{}
|
2015-04-07 18:54:21 +00:00
|
|
|
})
|
|
|
|
}
|