2015-04-07 18:54:21 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2018-01-17 22:57:46 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2015-06-20 18:40:20 +00:00
|
|
|
"net/url"
|
2019-09-12 21:52:03 +00:00
|
|
|
"regexp"
|
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
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
"github.com/go-redis/redis"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-06-06 00:12:30 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
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 {
|
2018-09-10 23:45:36 +00:00
|
|
|
Servers []string
|
|
|
|
Password string
|
2018-06-06 00:12:30 +00:00
|
|
|
tls.ClientConfig
|
2018-01-17 22:57:46 +00:00
|
|
|
|
|
|
|
clients []Client
|
|
|
|
initialized bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client interface {
|
|
|
|
Info() *redis.StringCmd
|
|
|
|
BaseTags() map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisClient struct {
|
|
|
|
client *redis.Client
|
|
|
|
tags map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisClient) Info() *redis.StringCmd {
|
2019-08-16 22:46:48 +00:00
|
|
|
return r.client.Info("ALL")
|
2018-01-17 22:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisClient) BaseTags() map[string]string {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range r.tags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
return tags
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 21:52:03 +00:00
|
|
|
var replicationSlaveMetricPrefix = regexp.MustCompile(`^slave\d+`)
|
|
|
|
|
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
|
2016-07-18 11:03:39 +00:00
|
|
|
## unix:///var/run/redis.sock
|
2016-02-18 21:26:51 +00:00
|
|
|
##
|
|
|
|
## 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"]
|
2018-06-06 00:12:30 +00:00
|
|
|
|
2018-09-10 23:45:36 +00:00
|
|
|
## specify server password
|
|
|
|
# password = "s#cr@t%"
|
|
|
|
|
2018-06-06 00:12:30 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
|
|
|
# insecure_skip_verify = true
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
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{
|
2016-08-31 05:05:11 +00:00
|
|
|
"uptime_in_seconds": "uptime",
|
|
|
|
"connected_clients": "clients",
|
|
|
|
"role": "replication_role",
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
func (r *Redis) init(acc telegraf.Accumulator) error {
|
|
|
|
if r.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-18 11:03:39 +00:00
|
|
|
|
2015-08-31 21:57:52 +00:00
|
|
|
if len(r.Servers) == 0 {
|
2018-01-17 22:57:46 +00:00
|
|
|
r.Servers = []string{"tcp://localhost:6379"}
|
2015-05-18 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
r.clients = make([]Client, len(r.Servers))
|
|
|
|
|
|
|
|
for i, serv := range r.Servers {
|
2016-07-19 14:24:10 +00:00
|
|
|
if !strings.HasPrefix(serv, "tcp://") && !strings.HasPrefix(serv, "unix://") {
|
2018-01-17 22:57:46 +00:00
|
|
|
log.Printf("W! [inputs.redis]: server URL found without scheme; please update your configuration file")
|
2016-07-18 11:03:39 +00:00
|
|
|
serv = "tcp://" + serv
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:40:20 +00:00
|
|
|
u, err := url.Parse(serv)
|
|
|
|
if err != nil {
|
2018-01-17 22:57:46 +00:00
|
|
|
return fmt.Errorf("Unable to parse to address %q: %v", serv, err)
|
2015-06-20 18:40:20 +00:00
|
|
|
}
|
2018-01-17 22:57:46 +00:00
|
|
|
|
|
|
|
password := ""
|
|
|
|
if u.User != nil {
|
|
|
|
pw, ok := u.User.Password()
|
|
|
|
if ok {
|
|
|
|
password = pw
|
2016-07-18 11:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:45:36 +00:00
|
|
|
if len(r.Password) > 0 {
|
|
|
|
password = r.Password
|
|
|
|
}
|
2016-07-18 11:03:39 +00:00
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
var address string
|
|
|
|
if u.Scheme == "unix" {
|
|
|
|
address = u.Path
|
|
|
|
} else {
|
|
|
|
address = u.Host
|
|
|
|
}
|
|
|
|
|
2018-06-06 00:12:30 +00:00
|
|
|
tlsConfig, err := r.ClientConfig.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
client := redis.NewClient(
|
|
|
|
&redis.Options{
|
2018-06-06 00:12:30 +00:00
|
|
|
Addr: address,
|
|
|
|
Password: password,
|
|
|
|
Network: u.Scheme,
|
|
|
|
PoolSize: 1,
|
|
|
|
TLSConfig: tlsConfig,
|
2018-01-17 22:57:46 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
tags := map[string]string{}
|
|
|
|
if u.Scheme == "unix" {
|
|
|
|
tags["socket"] = u.Path
|
|
|
|
} else {
|
|
|
|
tags["server"] = u.Hostname()
|
|
|
|
tags["port"] = u.Port()
|
|
|
|
}
|
|
|
|
|
|
|
|
r.clients[i] = &RedisClient{
|
|
|
|
client: client,
|
|
|
|
tags: tags,
|
|
|
|
}
|
2015-05-18 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
r.initialized = true
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2015-05-18 16:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
// Reads stats from all configured servers accumulates stats.
|
|
|
|
// Returns one of the errors encountered while gather stats (if any).
|
|
|
|
func (r *Redis) Gather(acc telegraf.Accumulator) error {
|
|
|
|
if !r.initialized {
|
|
|
|
err := r.init(acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-10 17:21:40 +00:00
|
|
|
}
|
2015-06-20 18:40:20 +00:00
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
var wg sync.WaitGroup
|
2015-06-20 18:40:20 +00:00
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
for _, client := range r.clients {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(client Client) {
|
|
|
|
defer wg.Done()
|
|
|
|
acc.AddError(r.gatherServer(client, acc))
|
|
|
|
}(client)
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-23 01:13:35 +00:00
|
|
|
|
2018-01-17 22:57:46 +00:00
|
|
|
func (r *Redis) gatherServer(client Client, acc telegraf.Accumulator) error {
|
|
|
|
info, err := client.Info().Result()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-07-18 11:03:39 +00:00
|
|
|
}
|
2018-01-17 22:57:46 +00:00
|
|
|
|
|
|
|
rdr := strings.NewReader(info)
|
|
|
|
return gatherInfoOutput(rdr, acc, client.BaseTags())
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// gatherInfoOutput gathers
|
|
|
|
func gatherInfoOutput(
|
2018-01-17 22:57:46 +00:00
|
|
|
rdr io.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 {
|
2016-08-31 05:05:11 +00:00
|
|
|
var section string
|
2016-12-20 23:21:40 +00:00
|
|
|
var keyspace_hits, keyspace_misses int64
|
2015-10-18 04:05:56 +00:00
|
|
|
|
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()
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-17 22:57:46 +00:00
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
if line[0] == '#' {
|
|
|
|
if len(line) > 2 {
|
|
|
|
section = line[2:]
|
|
|
|
}
|
2015-09-23 01:13:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(line, ":", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := string(parts[0])
|
2016-08-31 05:05:11 +00:00
|
|
|
|
|
|
|
if section == "Server" {
|
2017-07-26 00:07:43 +00:00
|
|
|
if name != "lru_clock" && name != "uptime_in_seconds" && name != "redis_version" {
|
2016-08-31 05:05:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2018-01-30 21:58:44 +00:00
|
|
|
if strings.HasPrefix(name, "master_replid") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
if name == "mem_allocator" {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-18 04:05:56 +00:00
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
if strings.HasSuffix(name, "_human") {
|
|
|
|
continue
|
2015-10-18 04:05:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
metric, ok := Tracking[name]
|
|
|
|
if !ok {
|
|
|
|
if section == "Keyspace" {
|
|
|
|
kline := strings.TrimSpace(string(parts[1]))
|
|
|
|
gatherKeyspaceLine(name, kline, acc, tags)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-16 22:46:48 +00:00
|
|
|
if section == "Commandstats" {
|
|
|
|
kline := strings.TrimSpace(parts[1])
|
|
|
|
gatherCommandstateLine(name, kline, acc, tags)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-12 21:52:03 +00:00
|
|
|
if section == "Replication" && replicationSlaveMetricPrefix.MatchString(name) {
|
|
|
|
kline := strings.TrimSpace(parts[1])
|
|
|
|
gatherReplicationLine(name, kline, acc, tags)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
metric = name
|
2015-10-18 04:05:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
val := strings.TrimSpace(parts[1])
|
|
|
|
|
2019-07-24 20:52:44 +00:00
|
|
|
// Some percentage values have a "%" suffix that we need to get rid of before int/float conversion
|
2019-07-24 21:04:33 +00:00
|
|
|
val = strings.TrimSuffix(val, "%")
|
2019-07-24 20:52:44 +00:00
|
|
|
|
2016-12-20 23:21:40 +00:00
|
|
|
// Try parsing as int
|
2019-07-24 21:04:33 +00:00
|
|
|
if ival, err := strconv.ParseInt(val, 10, 64); err == nil {
|
2016-08-31 05:05:11 +00:00
|
|
|
switch name {
|
|
|
|
case "keyspace_hits":
|
|
|
|
keyspace_hits = ival
|
|
|
|
case "keyspace_misses":
|
|
|
|
keyspace_misses = ival
|
|
|
|
case "rdb_last_save_time":
|
|
|
|
// influxdb can't calculate this, so we have to do it
|
2016-12-20 23:21:40 +00:00
|
|
|
fields["rdb_last_save_time_elapsed"] = time.Now().Unix() - ival
|
2016-08-31 05:05:11 +00:00
|
|
|
}
|
|
|
|
fields[metric] = ival
|
2016-05-18 13:17:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
// Try parsing as a float
|
2019-07-24 21:04:33 +00:00
|
|
|
if fval, err := strconv.ParseFloat(val, 64); err == nil {
|
2016-08-31 05:05:11 +00:00
|
|
|
fields[metric] = fval
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat it as a string
|
|
|
|
|
|
|
|
if name == "role" {
|
|
|
|
tags["replication_role"] = val
|
|
|
|
continue
|
2015-09-23 01:13:35 +00:00
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2016-08-31 05:05:11 +00:00
|
|
|
fields[metric] = val
|
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,
|
2016-06-02 13:25:23 +00:00
|
|
|
global_tags map[string]string,
|
2015-09-23 01:13:35 +00:00
|
|
|
) {
|
|
|
|
if strings.Contains(line, "keys=") {
|
2015-12-15 17:08:13 +00:00
|
|
|
fields := make(map[string]interface{})
|
2016-06-02 13:25:23 +00:00
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range global_tags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
2015-09-23 01:13:35 +00:00
|
|
|
tags["database"] = name
|
|
|
|
dbparts := strings.Split(line, ",")
|
|
|
|
for _, dbp := range dbparts {
|
|
|
|
kv := strings.Split(dbp, "=")
|
2016-12-20 23:21:40 +00:00
|
|
|
ival, err := strconv.ParseInt(kv[1], 10, 64)
|
2015-09-23 01:13:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 22:46:48 +00:00
|
|
|
// Parse the special cmdstat lines.
|
|
|
|
// Example:
|
|
|
|
// cmdstat_publish:calls=33791,usec=208789,usec_per_call=6.18
|
|
|
|
// Tag: cmdstat=publish; Fields: calls=33791i,usec=208789i,usec_per_call=6.18
|
|
|
|
func gatherCommandstateLine(
|
|
|
|
name string,
|
|
|
|
line string,
|
|
|
|
acc telegraf.Accumulator,
|
|
|
|
global_tags map[string]string,
|
|
|
|
) {
|
|
|
|
if !strings.HasPrefix(name, "cmdstat") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range global_tags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
tags["command"] = strings.TrimPrefix(name, "cmdstat_")
|
|
|
|
parts := strings.Split(line, ",")
|
|
|
|
for _, part := range parts {
|
|
|
|
kv := strings.Split(part, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch kv[0] {
|
|
|
|
case "calls":
|
|
|
|
fallthrough
|
|
|
|
case "usec":
|
|
|
|
ival, err := strconv.ParseInt(kv[1], 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
fields[kv[0]] = ival
|
|
|
|
}
|
|
|
|
case "usec_per_call":
|
|
|
|
fval, err := strconv.ParseFloat(kv[1], 64)
|
|
|
|
if err == nil {
|
|
|
|
fields[kv[0]] = fval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc.AddFields("redis_cmdstat", fields, tags)
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:52:03 +00:00
|
|
|
// Parse the special Replication line
|
|
|
|
// Example:
|
|
|
|
// slave0:ip=127.0.0.1,port=7379,state=online,offset=4556468,lag=0
|
|
|
|
// This line will only be visible when a node has a replica attached.
|
|
|
|
func gatherReplicationLine(
|
|
|
|
name string,
|
|
|
|
line string,
|
|
|
|
acc telegraf.Accumulator,
|
|
|
|
global_tags map[string]string,
|
|
|
|
) {
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range global_tags {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
tags["replica_id"] = strings.TrimLeft(name, "slave")
|
|
|
|
tags["replication_role"] = "slave"
|
|
|
|
|
|
|
|
parts := strings.Split(line, ",")
|
|
|
|
for _, part := range parts {
|
|
|
|
kv := strings.Split(part, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch kv[0] {
|
|
|
|
case "ip":
|
|
|
|
tags["replica_ip"] = kv[1]
|
|
|
|
case "port":
|
|
|
|
tags["replica_port"] = kv[1]
|
|
|
|
case "state":
|
|
|
|
tags[kv[0]] = kv[1]
|
|
|
|
default:
|
|
|
|
ival, err := strconv.ParseInt(kv[1], 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
fields[kv[0]] = ival
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("redis_replication", fields, tags)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|