2 * collectd - src/redis.c, based on src/memcached.c
3 * Copyright (C) 2010 Andrés J. Díaz <ajdiaz@connectical.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Andrés J. Díaz <ajdiaz@connectical.com>
28 #include <hiredis/hiredis.h>
31 #define REDIS_DEF_HOST "localhost"
32 #define REDIS_DEF_PASSWD ""
33 #define REDIS_DEF_PORT 6379
34 #define REDIS_DEF_TIMEOUT_SEC 2
35 #define REDIS_DEF_DB_COUNT 256
36 #define MAX_REDIS_VAL_SIZE 256
37 #define MAX_REDIS_QUERY 2048
39 /* Redis plugin configuration example:
52 typedef struct redis_query_s redis_query_t;
53 struct redis_query_s {
54 char query[MAX_REDIS_QUERY];
55 char type[DATA_MAX_NAME_LEN];
56 char instance[DATA_MAX_NAME_LEN];
63 typedef struct redis_node_s redis_node_t;
69 struct timeval timeout;
70 redisContext *redisContext;
71 redis_query_t *queries;
76 static bool redis_have_instances;
77 static int redis_read(user_data_t *user_data);
79 static void redis_node_free(void *arg) {
80 redis_node_t *rn = arg;
84 redis_query_t *rq = rn->queries;
86 redis_query_t *next = rq->next;
91 redisFree(rn->redisContext);
96 } /* void redis_node_free */
98 static int redis_node_add(redis_node_t *rn) /* {{{ */
100 DEBUG("redis plugin: Adding node \"%s\".", rn->name);
102 /* Disable automatic generation of default instance in the init callback. */
103 redis_have_instances = true;
105 char cb_name[sizeof("redis/") + DATA_MAX_NAME_LEN];
106 snprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
108 return plugin_register_complex_read(
109 /* group = */ "redis",
110 /* name = */ cb_name,
111 /* callback = */ redis_read,
114 .data = rn, .free_func = redis_node_free,
118 static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */
123 rq = calloc(1, sizeof(*rq));
125 ERROR("redis plugin: calloc failed adding redis_query.");
128 status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
133 * Default to a gauge type.
135 (void)strncpy(rq->type, "gauge", sizeof(rq->type));
136 (void)sstrncpy(rq->instance, rq->query, sizeof(rq->instance));
137 replace_special(rq->instance, sizeof(rq->instance));
141 for (int i = 0; i < ci->children_num; i++) {
142 oconfig_item_t *option = ci->children + i;
144 if (strcasecmp("Type", option->key) == 0) {
145 status = cf_util_get_string_buffer(option, rq->type, sizeof(rq->type));
146 } else if (strcasecmp("Instance", option->key) == 0) {
148 cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance));
149 } else if (strcasecmp("Database", option->key) == 0) {
150 status = cf_util_get_int(option, &rq->database);
151 if (rq->database < 0) {
152 WARNING("redis plugin: The \"Database\" option must be positive "
157 WARNING("redis plugin: unknown configuration option: %s", option->key);
169 static int redis_config_node(oconfig_item_t *ci) /* {{{ */
171 redis_node_t *rn = calloc(1, sizeof(*rn));
173 ERROR("redis plugin: calloc failed adding node.");
177 rn->port = REDIS_DEF_PORT;
178 rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
180 rn->host = strdup(REDIS_DEF_HOST);
181 if (rn->host == NULL) {
182 ERROR("redis plugin: strdup failed adding node.");
187 int status = cf_util_get_string(ci, &rn->name);
194 for (int i = 0; i < ci->children_num; i++) {
195 oconfig_item_t *option = ci->children + i;
197 if (strcasecmp("Host", option->key) == 0)
198 status = cf_util_get_string(option, &rn->host);
199 else if (strcasecmp("Port", option->key) == 0) {
200 status = cf_util_get_port_number(option);
205 } else if (strcasecmp("Query", option->key) == 0) {
206 redis_query_t *rq = redis_config_query(option);
210 rq->next = rn->queries;
213 } else if (strcasecmp("Timeout", option->key) == 0) {
215 status = cf_util_get_int(option, &timeout);
217 rn->timeout.tv_usec = timeout * 1000;
218 rn->timeout.tv_sec = rn->timeout.tv_usec / 1000000L;
219 rn->timeout.tv_usec %= 1000000L;
221 } else if (strcasecmp("Password", option->key) == 0)
222 status = cf_util_get_string(option, &rn->passwd);
224 WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
225 "block. I'll ignore this option.",
237 return redis_node_add(rn);
238 } /* }}} int redis_config_node */
240 static int redis_config(oconfig_item_t *ci) /* {{{ */
242 for (int i = 0; i < ci->children_num; i++) {
243 oconfig_item_t *option = ci->children + i;
245 if (strcasecmp("Node", option->key) == 0)
246 redis_config_node(option);
248 WARNING("redis plugin: Option `%s' not allowed in redis"
249 " configuration. It will be ignored.",
256 __attribute__((nonnull(2))) static void
257 redis_submit(char *plugin_instance, const char *type, const char *type_instance,
258 value_t value) /* {{{ */
260 value_list_t vl = VALUE_LIST_INIT;
264 sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
265 if (plugin_instance != NULL)
266 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
267 sstrncpy(vl.type, type, sizeof(vl.type));
268 if (type_instance != NULL)
269 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
271 plugin_dispatch_values(&vl);
274 static int redis_init(void) /* {{{ */
276 if (redis_have_instances)
279 redis_node_t *rn = calloc(1, sizeof(*rn));
283 rn->port = REDIS_DEF_PORT;
284 rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
286 rn->name = strdup("default");
287 rn->host = strdup(REDIS_DEF_HOST);
289 if (rn->name == NULL || rn->host == NULL)
292 return redis_node_add(rn);
293 } /* }}} int redis_init */
295 static void *c_redisCommand(redis_node_t *rn, const char *format, ...) {
296 redisContext *c = rn->redisContext;
302 va_start(ap, format);
303 void *reply = redisvCommand(c, format, ap);
307 ERROR("redis plugin: Connection error: %s", c->errstr);
308 redisFree(rn->redisContext);
309 rn->redisContext = NULL;
313 } /* void c_redisCommand */
315 static int redis_handle_info(char *node, char const *info_line,
316 char const *type, char const *type_instance,
317 char const *field_name, int ds_type) /* {{{ */
319 char *str = strstr(info_line, field_name);
320 static char buf[MAX_REDIS_VAL_SIZE];
325 str += strlen(field_name) + 1; /* also skip the ':' */
326 for (i = 0; (*str && (isdigit((unsigned char)*str) || *str == '.'));
331 if (parse_value(buf, &val, ds_type) == -1) {
332 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
336 redis_submit(node, type, type_instance, val);
341 } /* }}} int redis_handle_info */
343 static int redis_handle_query(redis_node_t *rn, redis_query_t *rq) /* {{{ */
346 const data_set_t *ds;
349 ds = plugin_get_ds(rq->type);
351 ERROR("redis plugin: DS type `%s' not defined.", rq->type);
355 if (ds->ds_num != 1) {
356 ERROR("redis plugin: DS type `%s' has too many datasources. This is not "
357 "supported currently.",
362 if ((rr = c_redisCommand(rn, "SELECT %d", rq->database)) == NULL) {
363 WARNING("redis plugin: unable to switch to database `%d' on node `%s'.",
364 rq->database, rn->name);
368 if ((rr = c_redisCommand(rn, rq->query)) == NULL) {
369 WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
374 case REDIS_REPLY_INTEGER:
375 switch (ds->ds[0].type) {
376 case DS_TYPE_COUNTER:
377 val.counter = (counter_t)rr->integer;
380 val.gauge = (gauge_t)rr->integer;
383 val.gauge = (derive_t)rr->integer;
385 case DS_TYPE_ABSOLUTE:
386 val.gauge = (absolute_t)rr->integer;
390 case REDIS_REPLY_STRING:
391 if (parse_value(rr->str, &val, ds->ds[0].type) == -1) {
392 WARNING("redis plugin: Query `%s': Unable to parse value.", rq->query);
397 case REDIS_REPLY_ERROR:
398 WARNING("redis plugin: Query `%s' failed: %s.", rq->query, rr->str);
401 case REDIS_REPLY_ARRAY:
402 WARNING("redis plugin: Query `%s' should return string or integer. Arrays "
403 "are not supported.",
408 WARNING("redis plugin: Query `%s': Cannot coerce redis type (%i).",
409 rq->query, rr->type);
414 redis_submit(rn->name, rq->type,
415 (strlen(rq->instance) > 0) ? rq->instance : NULL, val);
418 } /* }}} int redis_handle_query */
420 static int redis_db_stats(char *node, char const *info_line) /* {{{ */
422 /* redis_db_stats parses and dispatches Redis database statistics,
423 * currently the number of keys for each database.
424 * info_line needs to have the following format:
425 * db0:keys=4,expires=0,avg_ttl=0
428 for (int db = 0; db < REDIS_DEF_DB_COUNT; db++) {
429 static char buf[MAX_REDIS_VAL_SIZE];
430 static char field_name[12];
431 static char db_id[4];
436 snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
438 str = strstr(info_line, field_name);
442 str += strlen(field_name);
443 for (i = 0; (*str && isdigit((int)*str)); i++, str++)
447 if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
448 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
452 snprintf(db_id, sizeof(db_id), "%d", db);
453 redis_submit(node, "records", db_id, val);
457 } /* }}} int redis_db_stats */
459 static void redis_check_connection(redis_node_t *rn) {
460 if (rn->redisContext)
463 redisContext *rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
466 ERROR("redis plugin: can't allocate redis context");
470 ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", rn->name,
471 rn->host, rn->port, rh->errstr);
476 rn->redisContext = rh;
481 DEBUG("redis plugin: authenticating node `%s' passwd(%s).", rn->name,
484 if ((rr = c_redisCommand(rn, "AUTH %s", rn->passwd)) == NULL) {
485 WARNING("redis plugin: unable to authenticate on node `%s'.", rn->name);
489 if (rr->type != REDIS_REPLY_STATUS) {
490 WARNING("redis plugin: invalid authentication on node `%s'.", rn->name);
492 redisFree(rn->redisContext);
493 rn->redisContext = NULL;
500 } /* void redis_check_connection */
502 static void redis_read_server_info(redis_node_t *rn) {
505 if ((rr = c_redisCommand(rn, "INFO")) == NULL) {
506 WARNING("redis plugin: unable to get INFO from node `%s'.", rn->name);
510 redis_handle_info(rn->name, rr->str, "uptime", NULL, "uptime_in_seconds",
512 redis_handle_info(rn->name, rr->str, "current_connections", "clients",
513 "connected_clients", DS_TYPE_GAUGE);
514 redis_handle_info(rn->name, rr->str, "blocked_clients", NULL,
515 "blocked_clients", DS_TYPE_GAUGE);
516 redis_handle_info(rn->name, rr->str, "memory", NULL, "used_memory",
518 redis_handle_info(rn->name, rr->str, "memory_lua", NULL, "used_memory_lua",
520 /* changes_since_last_save: Deprecated in redis version 2.6 and above */
521 redis_handle_info(rn->name, rr->str, "volatile_changes", NULL,
522 "changes_since_last_save", DS_TYPE_GAUGE);
523 redis_handle_info(rn->name, rr->str, "total_connections", NULL,
524 "total_connections_received", DS_TYPE_DERIVE);
525 redis_handle_info(rn->name, rr->str, "total_operations", NULL,
526 "total_commands_processed", DS_TYPE_DERIVE);
527 redis_handle_info(rn->name, rr->str, "operations_per_second", NULL,
528 "instantaneous_ops_per_sec", DS_TYPE_GAUGE);
529 redis_handle_info(rn->name, rr->str, "expired_keys", NULL, "expired_keys",
531 redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
533 redis_handle_info(rn->name, rr->str, "pubsub", "channels", "pubsub_channels",
535 redis_handle_info(rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns",
537 redis_handle_info(rn->name, rr->str, "current_connections", "slaves",
538 "connected_slaves", DS_TYPE_GAUGE);
539 redis_handle_info(rn->name, rr->str, "cache_result", "hits", "keyspace_hits",
541 redis_handle_info(rn->name, rr->str, "cache_result", "misses",
542 "keyspace_misses", DS_TYPE_DERIVE);
543 redis_handle_info(rn->name, rr->str, "total_bytes", "input",
544 "total_net_input_bytes", DS_TYPE_DERIVE);
545 redis_handle_info(rn->name, rr->str, "total_bytes", "output",
546 "total_net_output_bytes", DS_TYPE_DERIVE);
548 redis_db_stats(rn->name, rr->str);
551 } /* void redis_read_server_info */
553 static int redis_read(user_data_t *user_data) /* {{{ */
555 redis_node_t *rn = user_data->data;
557 DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
560 redis_check_connection(rn);
562 if (!rn->redisContext) /* no connection */
565 redis_read_server_info(rn);
567 if (!rn->redisContext) /* connection lost */
570 for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next) {
571 redis_handle_query(rn, rq);
572 if (!rn->redisContext) /* connection lost */
580 void module_register(void) /* {{{ */
582 plugin_register_complex_config("redis", redis_config);
583 plugin_register_init("redis", redis_init);