-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
290 lines (254 loc) · 11.9 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package redis
import (
"encoding/json"
"fmt"
"strconv"
"strings"
redis "gopkg.in/redis.v5"
"github.com/Sirupsen/logrus"
)
// NAME - name of plugin
const NAME string = "redis"
// PROVIDER -
const PROVIDER string = "redis"
// EVENTTYPE -
const EVENTTYPE string = "RedisInfo"
// ProtocolVersion -
const ProtocolVersion string = "1"
// RedisClientImpl - interface used for mocking
type RedisClientImpl interface {
Info(section ...string) *redis.StringCmd
}
// Config is the keeper of the config
type Config struct {
RedisHost string // Optional: leaving blank will default to localhost
RedisPort string // Optional: leaving blank will default to 6379
RedisPass string // Optional: leaving blank means no password
RedisDB string // Optional: leaving blank will keep DBID at 0
DBID int // Not from external config, but holder for DBID int value if specified
}
// InventoryData is the data type for inventory data produced by a plugin data
// source and emitted to the agent's inventory data store
type InventoryData map[string]interface{}
// MetricData is the data type for events produced by a plugin data source and
// emitted to the agent's metrics data store
type MetricData map[string]interface{}
// EventData is the data type for single shot events
type EventData map[string]interface{}
// PluginData defines the format of the output JSON that plugins will return
type PluginData struct {
Name string `json:"name"`
ProtocolVersion string `json:"protocol_version"`
PluginVersion string `json:"plugin_version"`
Metrics []MetricData `json:"metrics"`
Inventory map[string]InventoryData `json:"inventory"`
Events []EventData `json:"events"`
Status string `json:"status"`
}
// OutputJSON takes an object and prints it as a JSON string to the stdout.
// If the pretty attribute is set to true, the JSON will be idented for easy reading.
func OutputJSON(data interface{}, pretty bool) error {
var output []byte
var err error
if pretty {
output, err = json.MarshalIndent(data, "", "\t")
} else {
output, err = json.Marshal(data)
}
if err != nil {
return fmt.Errorf("Error outputting JSON: %s", err)
}
if string(output) == "null" {
fmt.Println("[]")
} else {
fmt.Println(string(output))
}
return nil
}
// Run -
func Run(log *logrus.Logger, client RedisClientImpl, redisConf Config, prettyPrint bool, version string) {
// Initialize the output structure
var data = PluginData{
Name: NAME,
ProtocolVersion: ProtocolVersion,
PluginVersion: version,
Inventory: make(map[string]InventoryData),
Metrics: make([]MetricData, 0),
Events: make([]EventData, 0),
}
var metric = formatMetric(log, readStats(log, client, redisConf))
data.Metrics = append(data.Metrics, metric)
fatalIfErr(log, OutputJSON(data, prettyPrint))
}
// InitRedisClient - function to create a redis client
func InitRedisClient(conf Config) RedisClientImpl {
return redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", conf.RedisHost, conf.RedisPort),
Password: conf.RedisPass,
DB: conf.DBID,
})
}
func fatalIfErr(log *logrus.Logger, err error) {
if err != nil {
log.WithError(err).Fatal("can't continue")
}
}
// ValidateConfig - function to validate the config and set defaults
func ValidateConfig(log *logrus.Logger, redisConf *Config) {
if redisConf.RedisHost == "" {
redisConf.RedisHost = "localhost"
}
if redisConf.RedisPort == "" {
redisConf.RedisPort = "6379"
} else {
_, err := strconv.Atoi(redisConf.RedisPort)
if err != nil {
log.WithError(err).Fatal("Config Yaml value REDISPORT must be valid integer")
}
}
if redisConf.RedisDB != "" {
val, err := strconv.Atoi(redisConf.RedisDB)
if err != nil {
log.WithError(err).Fatal("Config Yaml value REDISDB must be valid integer")
}
redisConf.DBID = val
}
}
func readStats(log *logrus.Logger, client RedisClientImpl, redisConf Config) string {
output, err := client.Info().Result()
if err != nil {
log.WithError(err).Fatal("Error making stats call to redis")
}
return output
}
func parseRawData(rawMetric string) map[string]string {
results := map[string]string{}
lines := strings.Split(rawMetric, "\n")
for _, line := range lines {
splitLine := strings.Split(line, ":")
if len(splitLine) == 2 {
results[splitLine[0]] = strings.Trim(splitLine[1], "\r")
}
}
return results
}
func formatMetric(log *logrus.Logger, rawMetric string) map[string]interface{} {
log.WithFields(logrus.Fields{
"output": rawMetric,
}).Debugf("Full raw info response text")
rawData := parseRawData(rawMetric)
return map[string]interface{}{
"event_type": EVENTTYPE,
"providor": PROVIDER,
"redis.redis_version": rawData["redis_version"],
"redis.redis_git_sha1": rawData["redis_git_sha1"],
"redis.redis_git_dirty": toInt(log, rawData["redis_git_dirty"]),
"redis.redis_build_id": rawData["redis_build_id"],
"redis.redis_mode": rawData["redis_mode"],
"redis.os": rawData["os"],
"redis.arch_bits": toInt(log, rawData["arch_bits"]),
"redis.multiplexing_api": rawData["multiplexing_api"],
"redis.gcc_version": rawData["gcc_version"],
"redis.process_id": toInt(log, rawData["process_id"]),
"redis.run_id": rawData["run_id"],
"redis.tcp_port": toInt(log, rawData["tcp_port"]),
"redis.uptime_in_seconds": toInt(log, rawData["uptime_in_seconds"]),
"redis.uptime_in_days": toInt(log, rawData["uptime_in_days"]),
"redis.hz": toInt(log, rawData["hz"]),
"redis.lru_clock": toInt(log, rawData["lru_clock"]),
"redis.executable": rawData["executable"],
"redis.config_file": rawData["config_file"],
"redis.connected_clients": toInt(log, rawData["connected_clients"]),
"redis.client_longest_output_list": toInt(log, rawData["client_longest_output_list"]),
"redis.client_biggest_input_buf": toInt(log, rawData["client_biggest_input_buf"]),
"redis.blocked_clients": toInt(log, rawData["blocked_clients"]),
"redis.used_memory": toInt(log, rawData["used_memory"]),
"redis.used_memory_human": rawData["used_memory_human"],
"redis.used_memory_rss": toInt(log, rawData["used_memory_rss"]),
"redis.used_memory_rss_human": rawData["used_memory_rss_human"],
"redis.used_memory_peak": toInt(log, rawData["used_memory_peak"]),
"redis.used_memory_peak_human": rawData["used_memory_peak_human"],
"redis.total_system_memory": toInt(log, rawData["total_system_memory"]),
"redis.total_system_memory_human": rawData["total_system_memory_human"],
"redis.used_memory_lua": toInt(log, rawData["used_memory_lua"]),
"redis.used_memory_lua_human": rawData["used_memory_lua_human"],
"redis.maxmemory": toInt(log, rawData["maxmemory"]),
"redis.maxmemory_human": rawData["maxmemory_human"],
"redis.maxmemory_policy": rawData["maxmemory_policy"],
"redis.mem_fragmentation_ratio": toFloat(log, rawData["mem_fragmentation_ratio"]),
"redis.mem_allocator": rawData["mem_allocator"],
"redis.loading": toInt(log, rawData["loading"]),
"redis.rdb_changes_since_last_save": toInt(log, rawData["rdb_changes_since_last_save"]),
"redis.rdb_bgsave_in_progress": toInt(log, rawData["rdb_bgsave_in_progress"]),
"redis.rdb_last_save_time": toInt(log, rawData["rdb_last_save_time"]),
"redis.rdb_last_bgsave_status": rawData["rdb_last_bgsave_status"],
"redis.rdb_last_bgsave_time_sec": toInt(log, rawData["rdb_last_bgsave_time_sec"]),
"redis.rdb_current_bgsave_time_sec": toInt(log, rawData["rdb_current_bgsave_time_sec"]),
"redis.aof_enabled": toInt(log, rawData["aof_enabled"]),
"redis.aof_rewrite_in_progress": toInt(log, rawData["aof_rewrite_in_progress"]),
"redis.aof_rewrite_scheduled": toInt(log, rawData["aof_rewrite_scheduled"]),
"redis.aof_last_rewrite_time_sec": toInt(log, rawData["aof_last_rewrite_time_sec"]),
"redis.aof_current_rewrite_time_sec": toInt(log, rawData["aof_current_rewrite_time_sec"]),
"redis.aof_last_bgrewrite_status": rawData["aof_last_bgrewrite_status"],
"redis.aof_last_write_status": rawData["aof_last_write_status"],
"redis.total_connections_received": toInt(log, rawData["total_connections_received"]),
"redis.total_commands_processed": toInt(log, rawData["total_commands_processed"]),
"redis.instantaneous_ops_per_sec": toInt(log, rawData["instantaneous_ops_per_sec"]),
"redis.total_net_input_bytes": toInt(log, rawData["total_net_input_bytes"]),
"redis.total_net_output_bytes": toInt(log, rawData["total_net_output_bytes"]),
"redis.instantaneous_input_kbps": toFloat(log, rawData["instantaneous_input_kbps"]),
"redis.instantaneous_output_kbps": toFloat(log, rawData["instantaneous_output_kbps"]),
"redis.rejected_connections": toInt(log, rawData["rejected_connections"]),
"redis.sync_full": toInt(log, rawData["sync_full"]),
"redis.sync_partial_ok": toInt(log, rawData["sync_partial_ok"]),
"redis.sync_partial_err": toInt(log, rawData["sync_partial_err"]),
"redis.expired_keys": toInt(log, rawData["expired_keys"]),
"redis.evicted_keys": toInt(log, rawData["evicted_keys"]),
"redis.keyspace_hits": toInt(log, rawData["keyspace_hits"]),
"redis.keyspace_misses": toInt(log, rawData["keyspace_misses"]),
"redis.pubsub_channels": toInt(log, rawData["pubsub_channels"]),
"redis.pubsub_patterns": toInt(log, rawData["pubsub_patterns"]),
"redis.latest_fork_usec": toInt(log, rawData["latest_fork_usec"]),
"redis.migrate_cached_sockets": toInt(log, rawData["migrate_cached_sockets"]),
"redis.role": rawData["role"],
"redis.connected_slaves": toInt(log, rawData["connected_slaves"]),
"redis.master_repl_offset": toInt(log, rawData["master_repl_offset"]),
"redis.repl_backlog_active": toInt(log, rawData["repl_backlog_active"]),
"redis.repl_backlog_size": toInt(log, rawData["repl_backlog_size"]),
"redis.repl_backlog_first_byte_offset": toInt(log, rawData["repl_backlog_first_byte_offset"]),
"redis.repl_backlog_histlen": toInt(log, rawData["repl_backlog_histlen"]),
"redis.used_cpu_sys": toFloat(log, rawData["used_cpu_sys"]),
"redis.used_cpu_user": toFloat(log, rawData["used_cpu_user"]),
"redis.used_cpu_sys_children": toFloat(log, rawData["used_cpu_sys_children"]),
"redis.used_cpu_user_children": toFloat(log, rawData["used_cpu_user_children"]),
"redis.cluster_enabled": toInt(log, rawData["cluster_enabled"]),
}
}
func toInt(log *logrus.Logger, value string) int {
if value == "" {
return 0
}
valueInt, err := strconv.Atoi(value)
if err != nil {
log.WithFields(logrus.Fields{
"valueInt": valueInt,
"error": err,
}).Debug("Error converting value to int")
return 0
}
return valueInt
}
func toFloat(log *logrus.Logger, value string) float64 {
if value == "" {
return 0.00
}
valueFloat, err := strconv.ParseFloat(value, 64)
if err != nil {
log.WithFields(logrus.Fields{
"valueFloat": valueFloat,
"error": err,
}).Debug("Error converting value to float")
return 0.00
}
return valueFloat
}