-
Notifications
You must be signed in to change notification settings - Fork 21
/
redis.go
181 lines (147 loc) · 4.07 KB
/
redis.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package redis
import (
"encoding/json"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/mitchellh/mapstructure"
"gopkg.in/redis.v3"
"github.com/amonapp/amonagent/plugins"
)
func (p PerformanceStruct) String() string {
s, _ := json.Marshal(p)
return string(s)
}
// PerformanceStruct - XXX
type PerformanceStruct struct {
Gauges map[string]interface{} `json:"gauges"`
}
// RedisPerformanceFields - XXX
var RedisPerformanceFields = map[string]string{
"aof_last_rewrite_time_sec": "aof.last_rewrite_time",
"aof_rewrite_in_progress": "aof.rewrite",
"aof_current_size": "aof.size",
"aof_buffer_length": "aof.buffer_length",
// Network
"connected_clients": "net.clients",
"connected_slaves": "net.slaves",
"rejected_connections": "net.rejected",
// clients
"blocked_clients": "clients.blocked",
"client_biggest_input_buf": "clients.biggest_input_buf",
"client_longest_output_list": "clients.longest_output_list",
// Keys
"evicted_keys": "keys.evicted",
"expired_keys": "keys.expired",
// stats
"keyspace_hits": "stats.keyspace_hits",
"keyspace_misses": "stats.keyspace_misses",
"latest_fork_usec": "perf.latest_fork_usec",
// pubsub
"pubsub_channels": "pubsub.channels",
"pubsub_patterns": "pubsub.patterns",
// rdb
"rdb_bgsave_in_progress": "rdb.bgsave",
"rdb_changes_since_last_save": "rdb.changes_since_last",
"rdb_last_bgsave_time_sec": "rdb.last_bgsave_time",
// memory
"mem_fragmentation_ratio": "mem.fragmentation_ratio",
"used_memory": "mem.used",
"used_memory_lua": "mem.lua",
"used_memory_peak": "mem.peak",
"used_memory_rss": "mem.rss",
// replication
"master_last_io_seconds_ago": "replication.last_io_seconds_ago",
"master_sync_in_progress": "replication.sync",
"master_sync_left_bytes": "replication.sync_left_bytes",
}
// Config - XXX
type Config struct {
Host string
DB int64
Password string
}
var sampleConfig = `
# Available config options for Redis plugin:
#
# host: [protocol://][:password]@address[:port]
#
# If the config file is empty, fallback to the following default options:
#
# {"host": "tcp://localhost:6379", "db": 0, "password": ""}
#
# Config location: /etc/opt/amonagent/plugins-enabled/redis.conf
`
// SampleConfig - XXX
func (r *Redis) SampleConfig() string {
return sampleConfig
}
// Start - XXX
func (r *Redis) Start() error {
return nil
}
// Stop - XXX
func (r *Redis) Stop() {
}
// SetConfigDefaults - XXX
func (r *Redis) SetConfigDefaults() error {
configFile, err := plugins.UmarshalPluginConfig("redis")
if err != nil {
log.WithFields(log.Fields{"plugin": "redis", "error": err.Error()}).Error("Can't read config file")
}
var config Config
decodeError := mapstructure.Decode(configFile, &config)
if decodeError != nil {
log.WithFields(log.Fields{"plugin": "redis", "error": decodeError.Error()}).Error("Can't decode config file")
}
if len(config.Host) == 0 {
config.Host = "127.0.0.1:6379"
}
r.Config = config
return nil
}
// Description - XXX
func (r *Redis) Description() string {
return "Read metrics from a Redis server"
}
// Redis - XXX
type Redis struct {
Config Config
}
// Collect - XXX
func (r *Redis) Collect() (interface{}, error) {
r.SetConfigDefaults()
PerformanceStruct := PerformanceStruct{}
client := redis.NewClient(&redis.Options{
Addr: r.Config.Host,
DB: r.Config.DB,
Password: r.Config.Password,
})
defer client.Close()
val, err := client.Info().Result()
if err != nil {
log.Errorf("Can't get Redis INFO': %v", err)
return PerformanceStruct, err
}
gauges := make(map[string]interface{})
split := strings.Split(val, "\n")
for _, line := range split {
if len(line) == 0 || line[0] == '#' {
continue
}
parts := strings.SplitN(line, ":", 2)
name := string(parts[0])
metric, ok := RedisPerformanceFields[name]
if !ok {
continue
}
val := strings.TrimSpace(parts[1])
gauges[metric] = val
}
PerformanceStruct.Gauges = gauges
return PerformanceStruct, nil
}
func init() {
plugins.Add("redis", func() plugins.Plugin {
return &Redis{}
})
}