renaming plugins -> inputs
This commit is contained in:
252
plugins/inputs/redis/redis.go
Normal file
252
plugins/inputs/redis/redis.go
Normal file
@@ -0,0 +1,252 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/influxdb/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
type Redis struct {
|
||||
Servers []string
|
||||
}
|
||||
|
||||
var sampleConfig = `
|
||||
# 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
|
||||
servers = ["tcp://localhost:6379"]
|
||||
`
|
||||
|
||||
func (r *Redis) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (r *Redis) Description() string {
|
||||
return "Read metrics from one or many redis servers"
|
||||
}
|
||||
|
||||
var Tracking = map[string]string{
|
||||
"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",
|
||||
"instantaneous_input_kbps": "instantaneous_input_kbps",
|
||||
"instantaneous_output_kbps": "instantaneous_output_kbps",
|
||||
"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",
|
||||
}
|
||||
|
||||
var ErrProtocolError = errors.New("redis protocol error")
|
||||
|
||||
// Reads stats from all configured servers accumulates stats.
|
||||
// Returns one of the errors encountered while gather stats (if any).
|
||||
func (r *Redis) Gather(acc inputs.Accumulator) error {
|
||||
if len(r.Servers) == 0 {
|
||||
url := &url.URL{
|
||||
Host: ":6379",
|
||||
}
|
||||
r.gatherServer(url, acc)
|
||||
return nil
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
var outerr error
|
||||
|
||||
for _, serv := range r.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.Scheme = "tcp"
|
||||
u.Host = serv
|
||||
u.Path = ""
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(serv string) {
|
||||
defer wg.Done()
|
||||
outerr = r.gatherServer(u, acc)
|
||||
}(serv)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return outerr
|
||||
}
|
||||
|
||||
const defaultPort = "6379"
|
||||
|
||||
func (r *Redis) gatherServer(addr *url.URL, acc inputs.Accumulator) error {
|
||||
_, _, err := net.SplitHostPort(addr.Host)
|
||||
if err != nil {
|
||||
addr.Host = addr.Host + ":" + defaultPort
|
||||
}
|
||||
|
||||
c, err := net.Dial("tcp", addr.Host)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to connect to redis server '%s': %s", addr.Host, err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if addr.User != nil {
|
||||
pwd, set := addr.User.Password()
|
||||
if set && pwd != "" {
|
||||
c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd)))
|
||||
|
||||
rdr := bufio.NewReader(c)
|
||||
|
||||
line, err := rdr.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if line[0] != '+' {
|
||||
return fmt.Errorf("%s", strings.TrimSpace(line)[1:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.Write([]byte("INFO\r\n"))
|
||||
c.Write([]byte("EOF\r\n"))
|
||||
rdr := bufio.NewReader(c)
|
||||
|
||||
// Setup tags for all redis metrics
|
||||
host, port := "unknown", "unknown"
|
||||
// If there's an error, ignore and use 'unknown' tags
|
||||
host, port, _ = net.SplitHostPort(addr.Host)
|
||||
tags := map[string]string{"server": host, "port": port}
|
||||
|
||||
return gatherInfoOutput(rdr, acc, tags)
|
||||
}
|
||||
|
||||
// gatherInfoOutput gathers
|
||||
func gatherInfoOutput(
|
||||
rdr *bufio.Reader,
|
||||
acc inputs.Accumulator,
|
||||
tags map[string]string,
|
||||
) error {
|
||||
var keyspace_hits, keyspace_misses uint64 = 0, 0
|
||||
|
||||
scanner := bufio.NewScanner(rdr)
|
||||
fields := make(map[string]interface{})
|
||||
for scanner.Scan() {
|
||||
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
|
||||
}
|
||||
|
||||
val := strings.TrimSpace(parts[1])
|
||||
ival, err := strconv.ParseUint(val, 10, 64)
|
||||
|
||||
if name == "keyspace_hits" {
|
||||
keyspace_hits = ival
|
||||
}
|
||||
|
||||
if name == "keyspace_misses" {
|
||||
keyspace_misses = ival
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
fields[metric] = ival
|
||||
continue
|
||||
}
|
||||
|
||||
fval, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fields[metric] = fval
|
||||
}
|
||||
var keyspace_hitrate float64 = 0.0
|
||||
if keyspace_hits != 0 || keyspace_misses != 0 {
|
||||
keyspace_hitrate = float64(keyspace_hits) / float64(keyspace_hits+keyspace_misses)
|
||||
}
|
||||
fields["keyspace_hitrate"] = keyspace_hitrate
|
||||
acc.AddFields("redis", fields, tags)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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,
|
||||
acc inputs.Accumulator,
|
||||
tags map[string]string,
|
||||
) {
|
||||
if strings.Contains(line, "keys=") {
|
||||
fields := make(map[string]interface{})
|
||||
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 {
|
||||
fields[kv[0]] = ival
|
||||
}
|
||||
}
|
||||
acc.AddFields("redis_keyspace", fields, tags)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("redis", func() inputs.Input {
|
||||
return &Redis{}
|
||||
})
|
||||
}
|
||||
170
plugins/inputs/redis/redis_test.go
Normal file
170
plugins/inputs/redis/redis_test.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdb/telegraf/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRedisConnect(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf(testutil.GetLocalHost() + ":6379")
|
||||
|
||||
r := &Redis{
|
||||
Servers: []string{addr},
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
err := r.Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRedis_ParseMetrics(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
tags := map[string]string{"host": "redis.net"}
|
||||
rdr := bufio.NewReader(strings.NewReader(testOutput))
|
||||
|
||||
err := gatherInfoOutput(rdr, &acc, tags)
|
||||
require.NoError(t, err)
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"uptime": uint64(238),
|
||||
"clients": uint64(1),
|
||||
"used_memory": uint64(1003936),
|
||||
"used_memory_rss": uint64(811008),
|
||||
"used_memory_peak": uint64(1003936),
|
||||
"used_memory_lua": uint64(33792),
|
||||
"rdb_changes_since_last_save": uint64(0),
|
||||
"total_connections_received": uint64(2),
|
||||
"total_commands_processed": uint64(1),
|
||||
"instantaneous_ops_per_sec": uint64(0),
|
||||
"sync_full": uint64(0),
|
||||
"sync_partial_ok": uint64(0),
|
||||
"sync_partial_err": uint64(0),
|
||||
"expired_keys": uint64(0),
|
||||
"evicted_keys": uint64(0),
|
||||
"keyspace_hits": uint64(1),
|
||||
"keyspace_misses": uint64(1),
|
||||
"pubsub_channels": uint64(0),
|
||||
"pubsub_patterns": uint64(0),
|
||||
"latest_fork_usec": uint64(0),
|
||||
"connected_slaves": uint64(0),
|
||||
"master_repl_offset": uint64(0),
|
||||
"repl_backlog_active": uint64(0),
|
||||
"repl_backlog_size": uint64(1048576),
|
||||
"repl_backlog_histlen": uint64(0),
|
||||
"mem_fragmentation_ratio": float64(0.81),
|
||||
"instantaneous_input_kbps": float64(876.16),
|
||||
"instantaneous_output_kbps": float64(3010.23),
|
||||
"used_cpu_sys": float64(0.14),
|
||||
"used_cpu_user": float64(0.05),
|
||||
"used_cpu_sys_children": float64(0.00),
|
||||
"used_cpu_user_children": float64(0.00),
|
||||
"keyspace_hitrate": float64(0.50),
|
||||
}
|
||||
keyspaceFields := map[string]interface{}{
|
||||
"avg_ttl": uint64(0),
|
||||
"expires": uint64(0),
|
||||
"keys": uint64(2),
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "redis", fields, tags)
|
||||
acc.AssertContainsTaggedFields(t, "redis_keyspace", keyspaceFields, tags)
|
||||
}
|
||||
|
||||
const testOutput = `# Server
|
||||
redis_version:2.8.9
|
||||
redis_git_sha1:00000000
|
||||
redis_git_dirty:0
|
||||
redis_build_id:9ccc8119ea98f6e1
|
||||
redis_mode:standalone
|
||||
os:Darwin 14.1.0 x86_64
|
||||
arch_bits:64
|
||||
multiplexing_api:kqueue
|
||||
gcc_version:4.2.1
|
||||
process_id:40235
|
||||
run_id:37d020620aadf0627282c0f3401405d774a82664
|
||||
tcp_port:6379
|
||||
uptime_in_seconds:238
|
||||
uptime_in_days:0
|
||||
hz:10
|
||||
lru_clock:2364819
|
||||
config_file:/usr/local/etc/redis.conf
|
||||
|
||||
# Clients
|
||||
connected_clients:1
|
||||
client_longest_output_list:0
|
||||
client_biggest_input_buf:0
|
||||
blocked_clients:0
|
||||
|
||||
# Memory
|
||||
used_memory:1003936
|
||||
used_memory_human:980.41K
|
||||
used_memory_rss:811008
|
||||
used_memory_peak:1003936
|
||||
used_memory_peak_human:980.41K
|
||||
used_memory_lua:33792
|
||||
mem_fragmentation_ratio:0.81
|
||||
mem_allocator:libc
|
||||
|
||||
# Persistence
|
||||
loading:0
|
||||
rdb_changes_since_last_save:0
|
||||
rdb_bgsave_in_progress:0
|
||||
rdb_last_save_time:1428427941
|
||||
rdb_last_bgsave_status:ok
|
||||
rdb_last_bgsave_time_sec:-1
|
||||
rdb_current_bgsave_time_sec:-1
|
||||
aof_enabled:0
|
||||
aof_rewrite_in_progress:0
|
||||
aof_rewrite_scheduled:0
|
||||
aof_last_rewrite_time_sec:-1
|
||||
aof_current_rewrite_time_sec:-1
|
||||
aof_last_bgrewrite_status:ok
|
||||
aof_last_write_status:ok
|
||||
|
||||
# Stats
|
||||
total_connections_received:2
|
||||
total_commands_processed:1
|
||||
instantaneous_ops_per_sec:0
|
||||
instantaneous_input_kbps:876.16
|
||||
instantaneous_output_kbps:3010.23
|
||||
rejected_connections:0
|
||||
sync_full:0
|
||||
sync_partial_ok:0
|
||||
sync_partial_err:0
|
||||
expired_keys:0
|
||||
evicted_keys:0
|
||||
keyspace_hits:1
|
||||
keyspace_misses:1
|
||||
pubsub_channels:0
|
||||
pubsub_patterns:0
|
||||
latest_fork_usec:0
|
||||
|
||||
# Replication
|
||||
role:master
|
||||
connected_slaves:0
|
||||
master_repl_offset:0
|
||||
repl_backlog_active:0
|
||||
repl_backlog_size:1048576
|
||||
repl_backlog_first_byte_offset:0
|
||||
repl_backlog_histlen:0
|
||||
|
||||
# CPU
|
||||
used_cpu_sys:0.14
|
||||
used_cpu_user:0.05
|
||||
used_cpu_sys_children:0.00
|
||||
used_cpu_user_children:0.00
|
||||
|
||||
# Keyspace
|
||||
db0:keys=2,expires=0,avg_ttl=0
|
||||
|
||||
(error) ERR unknown command 'eof'
|
||||
`
|
||||
Reference in New Issue
Block a user