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>
32 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
35 #define REDIS_DEF_HOST "localhost"
36 #define REDIS_DEF_PASSWD ""
37 #define REDIS_DEF_PORT 6379
38 #define REDIS_DEF_TIMEOUT 2000
39 #define REDIS_DEF_DB_COUNT 16
40 #define MAX_REDIS_NODE_NAME 64
41 #define MAX_REDIS_PASSWD_LENGTH 512
42 #define MAX_REDIS_VAL_SIZE 256
43 #define MAX_REDIS_QUERY 2048
45 /* Redis plugin configuration example:
58 typedef struct redis_query_s redis_query_t;
59 struct redis_query_s {
60 char query[MAX_REDIS_QUERY];
61 char type[DATA_MAX_NAME_LEN];
62 char instance[DATA_MAX_NAME_LEN];
67 typedef struct redis_node_s redis_node_t;
69 char name[MAX_REDIS_NODE_NAME];
70 char host[HOST_NAME_MAX];
71 char passwd[MAX_REDIS_PASSWD_LENGTH];
73 struct timeval timeout;
74 redis_query_t *queries;
79 static redis_node_t *nodes_head = NULL;
81 static int redis_node_add(const redis_node_t *rn) /* {{{ */
83 redis_node_t *rn_copy;
86 /* Check for duplicates first */
87 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
88 if (strcmp(rn->name, rn_ptr->name) == 0)
92 ERROR("redis plugin: A node with the name `%s' already exists.", rn->name);
96 rn_copy = malloc(sizeof(*rn_copy));
97 if (rn_copy == NULL) {
98 ERROR("redis plugin: malloc failed adding redis_node to the tree.");
102 memcpy(rn_copy, rn, sizeof(*rn_copy));
103 rn_copy->next = NULL;
105 DEBUG("redis plugin: Adding node \"%s\".", rn->name);
107 if (nodes_head == NULL)
108 nodes_head = rn_copy;
111 while (rn_ptr->next != NULL)
112 rn_ptr = rn_ptr->next;
113 rn_ptr->next = rn_copy;
119 static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */
124 rq = calloc(1, sizeof(*rq));
126 ERROR("redis plugin: calloc failed adding redis_query.");
129 status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
134 * Default to a gauge type.
136 (void)strncpy(rq->type, "gauge", sizeof(rq->type));
137 (void)sstrncpy(rq->instance, rq->query, sizeof(rq->instance));
138 replace_special(rq->instance, sizeof(rq->instance));
140 for (int i = 0; i < ci->children_num; i++) {
141 oconfig_item_t *option = ci->children + i;
143 if (strcasecmp("Type", option->key) == 0) {
144 status = cf_util_get_string_buffer(option, rq->type, sizeof(rq->type));
145 } else if (strcasecmp("Instance", option->key) == 0) {
147 cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance));
149 WARNING("redis plugin: unknown configuration option: %s", option->key);
161 static int redis_config_node(oconfig_item_t *ci) /* {{{ */
167 redis_node_t rn = {.port = REDIS_DEF_PORT,
168 .timeout.tv_usec = REDIS_DEF_TIMEOUT};
170 sstrncpy(rn.host, REDIS_DEF_HOST, sizeof(rn.host));
172 status = cf_util_get_string_buffer(ci, rn.name, sizeof(rn.name));
176 for (int i = 0; i < ci->children_num; i++) {
177 oconfig_item_t *option = ci->children + i;
179 if (strcasecmp("Host", option->key) == 0)
180 status = cf_util_get_string_buffer(option, rn.host, sizeof(rn.host));
181 else if (strcasecmp("Port", option->key) == 0) {
182 status = cf_util_get_port_number(option);
187 } else if (strcasecmp("Query", option->key) == 0) {
188 rq = redis_config_query(option);
192 rq->next = rn.queries;
195 } else if (strcasecmp("Timeout", option->key) == 0) {
196 status = cf_util_get_int(option, &timeout);
198 rn.timeout.tv_usec = timeout;
199 } else if (strcasecmp("Password", option->key) == 0)
200 status = cf_util_get_string_buffer(option, rn.passwd, sizeof(rn.passwd));
202 WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
203 "block. I'll ignore this option.",
213 return redis_node_add(&rn);
214 } /* }}} int redis_config_node */
216 static int redis_config(oconfig_item_t *ci) /* {{{ */
218 for (int i = 0; i < ci->children_num; i++) {
219 oconfig_item_t *option = ci->children + i;
221 if (strcasecmp("Node", option->key) == 0)
222 redis_config_node(option);
224 WARNING("redis plugin: Option `%s' not allowed in redis"
225 " configuration. It will be ignored.",
229 if (nodes_head == NULL) {
230 ERROR("redis plugin: No valid node configuration could be found.");
237 __attribute__((nonnull(2))) static void
238 redis_submit(char *plugin_instance, const char *type, const char *type_instance,
239 value_t value) /* {{{ */
241 value_list_t vl = VALUE_LIST_INIT;
245 sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
246 if (plugin_instance != NULL)
247 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
248 sstrncpy(vl.type, type, sizeof(vl.type));
249 if (type_instance != NULL)
250 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
252 plugin_dispatch_values(&vl);
255 static int redis_init(void) /* {{{ */
257 redis_node_t rn = {.name = "default",
258 .host = REDIS_DEF_HOST,
259 .port = REDIS_DEF_PORT,
261 .timeout.tv_usec = REDIS_DEF_TIMEOUT,
264 if (nodes_head == NULL)
268 } /* }}} int redis_init */
270 static int redis_handle_info(char *node, char const *info_line,
271 char const *type, char const *type_instance,
272 char const *field_name, int ds_type) /* {{{ */
274 char *str = strstr(info_line, field_name);
275 static char buf[MAX_REDIS_VAL_SIZE];
280 str += strlen(field_name) + 1; /* also skip the ':' */
281 for (i = 0; (*str && (isdigit((unsigned char)*str) || *str == '.'));
286 if (parse_value(buf, &val, ds_type) == -1) {
287 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
291 redis_submit(node, type, type_instance, val);
296 } /* }}} int redis_handle_info */
298 static int redis_handle_query(redisContext *rh, redis_node_t *rn,
299 redis_query_t *rq) /* {{{ */
302 const data_set_t *ds;
305 ds = plugin_get_ds(rq->type);
307 ERROR("redis plugin: DataSet `%s' not defined.", rq->type);
311 if (ds->ds_num != 1) {
312 ERROR("redis plugin: DS `%s' has too many types.", rq->type);
316 if ((rr = redisCommand(rh, rq->query)) == NULL) {
317 WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
322 case REDIS_REPLY_INTEGER:
323 switch (ds->ds[0].type) {
324 case DS_TYPE_COUNTER:
325 val.counter = (counter_t)rr->integer;
328 val.gauge = (gauge_t)rr->integer;
331 val.gauge = (derive_t)rr->integer;
333 case DS_TYPE_ABSOLUTE:
334 val.gauge = (absolute_t)rr->integer;
338 case REDIS_REPLY_STRING:
339 if (parse_value(rr->str, &val, ds->ds[0].type) == -1) {
340 WARNING("redis plugin: Unable to parse field `%s'.", rq->type);
346 WARNING("redis plugin: Cannot coerce redis type.");
351 redis_submit(rn->name, rq->type,
352 (strlen(rq->instance) > 0) ? rq->instance : NULL, val);
355 } /* }}} int redis_handle_query */
357 static int redis_db_stats(char *node, char const *info_line) /* {{{ */
359 /* redis_db_stats parses and dispatches Redis database statistics,
360 * currently the number of keys for each database.
361 * info_line needs to have the following format:
362 * db0:keys=4,expires=0,avg_ttl=0
365 for (int db = 0; db < REDIS_DEF_DB_COUNT; db++) {
366 static char buf[MAX_REDIS_VAL_SIZE];
367 static char field_name[11];
368 static char db_id[3];
373 snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
375 str = strstr(info_line, field_name);
379 str += strlen(field_name);
380 for (i = 0; (*str && isdigit((int)*str)); i++, str++)
384 if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
385 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
389 snprintf(db_id, sizeof(db_id), "%d", db);
390 redis_submit(node, "records", db_id, val);
394 } /* }}} int redis_db_stats */
396 static int redis_read(void) /* {{{ */
398 for (redis_node_t *rn = nodes_head; rn != NULL; rn = rn->next) {
402 DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
405 rh = redisConnectWithTimeout((char *)rn->host, rn->port, rn->timeout);
407 ERROR("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name,
412 if (strlen(rn->passwd) > 0) {
413 DEBUG("redis plugin: authenticating node `%s' passwd(%s).", rn->name,
416 if ((rr = redisCommand(rh, "AUTH %s", rn->passwd)) == NULL) {
417 WARNING("redis plugin: unable to authenticate on node `%s'.", rn->name);
421 if (rr->type != REDIS_REPLY_STATUS) {
422 WARNING("redis plugin: invalid authentication on node `%s'.", rn->name);
429 if ((rr = redisCommand(rh, "INFO")) == NULL) {
430 WARNING("redis plugin: unable to get info from node `%s'.", rn->name);
434 redis_handle_info(rn->name, rr->str, "uptime", NULL, "uptime_in_seconds",
436 redis_handle_info(rn->name, rr->str, "current_connections", "clients",
437 "connected_clients", DS_TYPE_GAUGE);
438 redis_handle_info(rn->name, rr->str, "blocked_clients", NULL,
439 "blocked_clients", DS_TYPE_GAUGE);
440 redis_handle_info(rn->name, rr->str, "memory", NULL, "used_memory",
442 redis_handle_info(rn->name, rr->str, "memory_lua", NULL, "used_memory_lua",
444 /* changes_since_last_save: Deprecated in redis version 2.6 and above */
445 redis_handle_info(rn->name, rr->str, "volatile_changes", NULL,
446 "changes_since_last_save", DS_TYPE_GAUGE);
447 redis_handle_info(rn->name, rr->str, "total_connections", NULL,
448 "total_connections_received", DS_TYPE_DERIVE);
449 redis_handle_info(rn->name, rr->str, "total_operations", NULL,
450 "total_commands_processed", DS_TYPE_DERIVE);
451 redis_handle_info(rn->name, rr->str, "operations_per_second", NULL,
452 "instantaneous_ops_per_sec", DS_TYPE_GAUGE);
453 redis_handle_info(rn->name, rr->str, "expired_keys", NULL, "expired_keys",
455 redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
457 redis_handle_info(rn->name, rr->str, "pubsub", "channels",
458 "pubsub_channels", DS_TYPE_GAUGE);
459 redis_handle_info(rn->name, rr->str, "pubsub", "patterns",
460 "pubsub_patterns", DS_TYPE_GAUGE);
461 redis_handle_info(rn->name, rr->str, "current_connections", "slaves",
462 "connected_slaves", DS_TYPE_GAUGE);
463 redis_handle_info(rn->name, rr->str, "cache_result", "hits",
464 "keyspace_hits", DS_TYPE_DERIVE);
465 redis_handle_info(rn->name, rr->str, "cache_result", "misses",
466 "keyspace_misses", DS_TYPE_DERIVE);
467 redis_handle_info(rn->name, rr->str, "total_bytes", "input",
468 "total_net_input_bytes", DS_TYPE_DERIVE);
469 redis_handle_info(rn->name, rr->str, "total_bytes", "output",
470 "total_net_output_bytes", DS_TYPE_DERIVE);
472 redis_db_stats(rn->name, rr->str);
474 for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next)
475 redis_handle_query(rh, rn, rq);
487 void module_register(void) /* {{{ */
489 plugin_register_complex_config("redis", redis_config);
490 plugin_register_init("redis", redis_init);
491 plugin_register_read("redis", redis_read);
492 /* TODO: plugin_register_write: one redis list per value id with