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 256
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];
69 typedef struct redis_node_s redis_node_t;
71 char name[MAX_REDIS_NODE_NAME];
72 char host[HOST_NAME_MAX];
73 char passwd[MAX_REDIS_PASSWD_LENGTH];
75 struct timeval timeout;
76 redis_query_t *queries;
81 static redis_node_t *nodes_head;
83 static int redis_node_add(const redis_node_t *rn) /* {{{ */
85 redis_node_t *rn_copy;
88 /* Check for duplicates first */
89 for (rn_ptr = nodes_head; rn_ptr != NULL; rn_ptr = rn_ptr->next)
90 if (strcmp(rn->name, rn_ptr->name) == 0)
94 ERROR("redis plugin: A node with the name `%s' already exists.", rn->name);
98 rn_copy = malloc(sizeof(*rn_copy));
99 if (rn_copy == NULL) {
100 ERROR("redis plugin: malloc failed adding redis_node to the tree.");
104 memcpy(rn_copy, rn, sizeof(*rn_copy));
105 rn_copy->next = NULL;
107 DEBUG("redis plugin: Adding node \"%s\".", rn->name);
109 if (nodes_head == NULL)
110 nodes_head = rn_copy;
113 while (rn_ptr->next != NULL)
114 rn_ptr = rn_ptr->next;
115 rn_ptr->next = rn_copy;
121 static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */
126 rq = calloc(1, sizeof(*rq));
128 ERROR("redis plugin: calloc failed adding redis_query.");
131 status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
136 * Default to a gauge type.
138 (void)strncpy(rq->type, "gauge", sizeof(rq->type));
139 (void)sstrncpy(rq->instance, rq->query, sizeof(rq->instance));
140 replace_special(rq->instance, sizeof(rq->instance));
144 for (int i = 0; i < ci->children_num; i++) {
145 oconfig_item_t *option = ci->children + i;
147 if (strcasecmp("Type", option->key) == 0) {
148 status = cf_util_get_string_buffer(option, rq->type, sizeof(rq->type));
149 } else if (strcasecmp("Instance", option->key) == 0) {
151 cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance));
152 } else if (strcasecmp("Database", option->key) == 0) {
153 status = cf_util_get_int(option, &rq->database);
154 if (rq->database < 0) {
155 WARNING("redis plugin: The \"Database\" option must be positive "
160 WARNING("redis plugin: unknown configuration option: %s", option->key);
172 static int redis_config_node(oconfig_item_t *ci) /* {{{ */
178 redis_node_t rn = {.port = REDIS_DEF_PORT,
179 .timeout.tv_usec = REDIS_DEF_TIMEOUT};
181 sstrncpy(rn.host, REDIS_DEF_HOST, sizeof(rn.host));
183 status = cf_util_get_string_buffer(ci, rn.name, sizeof(rn.name));
187 for (int i = 0; i < ci->children_num; i++) {
188 oconfig_item_t *option = ci->children + i;
190 if (strcasecmp("Host", option->key) == 0)
191 status = cf_util_get_string_buffer(option, rn.host, sizeof(rn.host));
192 else if (strcasecmp("Port", option->key) == 0) {
193 status = cf_util_get_port_number(option);
198 } else if (strcasecmp("Query", option->key) == 0) {
199 rq = redis_config_query(option);
203 rq->next = rn.queries;
206 } else if (strcasecmp("Timeout", option->key) == 0) {
207 status = cf_util_get_int(option, &timeout);
209 rn.timeout.tv_usec = timeout;
210 } else if (strcasecmp("Password", option->key) == 0)
211 status = cf_util_get_string_buffer(option, rn.passwd, sizeof(rn.passwd));
213 WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
214 "block. I'll ignore this option.",
224 return redis_node_add(&rn);
225 } /* }}} int redis_config_node */
227 static int redis_config(oconfig_item_t *ci) /* {{{ */
229 for (int i = 0; i < ci->children_num; i++) {
230 oconfig_item_t *option = ci->children + i;
232 if (strcasecmp("Node", option->key) == 0)
233 redis_config_node(option);
235 WARNING("redis plugin: Option `%s' not allowed in redis"
236 " configuration. It will be ignored.",
240 if (nodes_head == NULL) {
241 ERROR("redis plugin: No valid node configuration could be found.");
248 __attribute__((nonnull(2))) static void
249 redis_submit(char *plugin_instance, const char *type, const char *type_instance,
250 value_t value) /* {{{ */
252 value_list_t vl = VALUE_LIST_INIT;
256 sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
257 if (plugin_instance != NULL)
258 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
259 sstrncpy(vl.type, type, sizeof(vl.type));
260 if (type_instance != NULL)
261 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
263 plugin_dispatch_values(&vl);
266 static int redis_init(void) /* {{{ */
268 redis_node_t rn = {.name = "default",
269 .host = REDIS_DEF_HOST,
270 .port = REDIS_DEF_PORT,
272 .timeout.tv_usec = REDIS_DEF_TIMEOUT,
275 if (nodes_head == NULL)
279 } /* }}} int redis_init */
281 static int redis_handle_info(char *node, char const *info_line,
282 char const *type, char const *type_instance,
283 char const *field_name, int ds_type) /* {{{ */
285 char *str = strstr(info_line, field_name);
286 static char buf[MAX_REDIS_VAL_SIZE];
291 str += strlen(field_name) + 1; /* also skip the ':' */
292 for (i = 0; (*str && (isdigit((unsigned char)*str) || *str == '.'));
297 if (parse_value(buf, &val, ds_type) == -1) {
298 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
302 redis_submit(node, type, type_instance, val);
307 } /* }}} int redis_handle_info */
309 static int redis_handle_query(redisContext *rh, redis_node_t *rn,
310 redis_query_t *rq) /* {{{ */
313 const data_set_t *ds;
316 ds = plugin_get_ds(rq->type);
318 ERROR("redis plugin: DataSet `%s' not defined.", rq->type);
322 if (ds->ds_num != 1) {
323 ERROR("redis plugin: DS `%s' has too many types.", rq->type);
327 if ((rr = redisCommand(rh, "SELECT %d", rq->database)) == NULL) {
328 WARNING("redis plugin: unable to switch to database `%d' on node `%s'.",
329 rq->database, rn->name);
333 if ((rr = redisCommand(rh, rq->query)) == NULL) {
334 WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
339 case REDIS_REPLY_INTEGER:
340 switch (ds->ds[0].type) {
341 case DS_TYPE_COUNTER:
342 val.counter = (counter_t)rr->integer;
345 val.gauge = (gauge_t)rr->integer;
348 val.gauge = (derive_t)rr->integer;
350 case DS_TYPE_ABSOLUTE:
351 val.gauge = (absolute_t)rr->integer;
355 case REDIS_REPLY_STRING:
356 if (parse_value(rr->str, &val, ds->ds[0].type) == -1) {
357 WARNING("redis plugin: Unable to parse field `%s'.", rq->type);
363 WARNING("redis plugin: Cannot coerce redis type.");
368 redis_submit(rn->name, rq->type,
369 (strlen(rq->instance) > 0) ? rq->instance : NULL, val);
372 } /* }}} int redis_handle_query */
374 static int redis_db_stats(char *node, char const *info_line) /* {{{ */
376 /* redis_db_stats parses and dispatches Redis database statistics,
377 * currently the number of keys for each database.
378 * info_line needs to have the following format:
379 * db0:keys=4,expires=0,avg_ttl=0
382 for (int db = 0; db < REDIS_DEF_DB_COUNT; db++) {
383 static char buf[MAX_REDIS_VAL_SIZE];
384 static char field_name[12];
385 static char db_id[4];
390 snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
392 str = strstr(info_line, field_name);
396 str += strlen(field_name);
397 for (i = 0; (*str && isdigit((int)*str)); i++, str++)
401 if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
402 WARNING("redis plugin: Unable to parse field `%s'.", field_name);
406 snprintf(db_id, sizeof(db_id), "%d", db);
407 redis_submit(node, "records", db_id, val);
411 } /* }}} int redis_db_stats */
413 static int redis_read(void) /* {{{ */
415 for (redis_node_t *rn = nodes_head; rn != NULL; rn = rn->next) {
419 DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
422 rh = redisConnectWithTimeout((char *)rn->host, rn->port, rn->timeout);
424 ERROR("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name,
429 if (strlen(rn->passwd) > 0) {
430 DEBUG("redis plugin: authenticating node `%s' passwd(%s).", rn->name,
433 if ((rr = redisCommand(rh, "AUTH %s", rn->passwd)) == NULL) {
434 WARNING("redis plugin: unable to authenticate on node `%s'.", rn->name);
438 if (rr->type != REDIS_REPLY_STATUS) {
439 WARNING("redis plugin: invalid authentication on node `%s'.", rn->name);
446 if ((rr = redisCommand(rh, "INFO")) == NULL) {
447 WARNING("redis plugin: unable to get info from node `%s'.", rn->name);
451 redis_handle_info(rn->name, rr->str, "uptime", NULL, "uptime_in_seconds",
453 redis_handle_info(rn->name, rr->str, "current_connections", "clients",
454 "connected_clients", DS_TYPE_GAUGE);
455 redis_handle_info(rn->name, rr->str, "blocked_clients", NULL,
456 "blocked_clients", DS_TYPE_GAUGE);
457 redis_handle_info(rn->name, rr->str, "memory", NULL, "used_memory",
459 redis_handle_info(rn->name, rr->str, "memory_lua", NULL, "used_memory_lua",
461 /* changes_since_last_save: Deprecated in redis version 2.6 and above */
462 redis_handle_info(rn->name, rr->str, "volatile_changes", NULL,
463 "changes_since_last_save", DS_TYPE_GAUGE);
464 redis_handle_info(rn->name, rr->str, "total_connections", NULL,
465 "total_connections_received", DS_TYPE_DERIVE);
466 redis_handle_info(rn->name, rr->str, "total_operations", NULL,
467 "total_commands_processed", DS_TYPE_DERIVE);
468 redis_handle_info(rn->name, rr->str, "operations_per_second", NULL,
469 "instantaneous_ops_per_sec", DS_TYPE_GAUGE);
470 redis_handle_info(rn->name, rr->str, "expired_keys", NULL, "expired_keys",
472 redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
474 redis_handle_info(rn->name, rr->str, "pubsub", "channels",
475 "pubsub_channels", DS_TYPE_GAUGE);
476 redis_handle_info(rn->name, rr->str, "pubsub", "patterns",
477 "pubsub_patterns", DS_TYPE_GAUGE);
478 redis_handle_info(rn->name, rr->str, "current_connections", "slaves",
479 "connected_slaves", DS_TYPE_GAUGE);
480 redis_handle_info(rn->name, rr->str, "cache_result", "hits",
481 "keyspace_hits", DS_TYPE_DERIVE);
482 redis_handle_info(rn->name, rr->str, "cache_result", "misses",
483 "keyspace_misses", DS_TYPE_DERIVE);
484 redis_handle_info(rn->name, rr->str, "total_bytes", "input",
485 "total_net_input_bytes", DS_TYPE_DERIVE);
486 redis_handle_info(rn->name, rr->str, "total_bytes", "output",
487 "total_net_output_bytes", DS_TYPE_DERIVE);
489 redis_db_stats(rn->name, rr->str);
491 for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next)
492 redis_handle_query(rh, rn, rq);
504 void module_register(void) /* {{{ */
506 plugin_register_complex_config("redis", redis_config);
507 plugin_register_init("redis", redis_init);
508 plugin_register_read("redis", redis_read);
509 /* TODO: plugin_register_write: one redis list per value id with