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>
26 #include "configfile.h"
30 #include <hiredis/hiredis.h>
33 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
36 #define REDIS_DEF_HOST "localhost"
37 #define REDIS_DEF_PASSWD ""
38 #define REDIS_DEF_PORT 6379
39 #define REDIS_DEF_TIMEOUT 2000
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;
61 char query[MAX_REDIS_QUERY];
62 char type[DATA_MAX_NAME_LEN];
63 char instance[DATA_MAX_NAME_LEN];
68 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 = NULL;
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)
95 ERROR ("redis plugin: A node with the name `%s' already exists.",
100 rn_copy = malloc (sizeof (*rn_copy));
103 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
107 memcpy (rn_copy, rn, sizeof (*rn_copy));
108 rn_copy->next = NULL;
110 DEBUG ("redis plugin: Adding node \"%s\".", rn->name);
112 if (nodes_head == NULL)
113 nodes_head = rn_copy;
117 while (rn_ptr->next != NULL)
118 rn_ptr = rn_ptr->next;
119 rn_ptr->next = rn_copy;
125 static redis_query_t *redis_config_query (oconfig_item_t *ci) /* {{{ */
131 rq = calloc(1, sizeof(*rq));
133 ERROR("redis plugin: calloca failed adding redis_query.");
136 status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
141 * Default to a gauge type.
143 (void)strncpy(rq->type, "gauge", sizeof(rq->type));
144 (void)strncpy(rq->instance, rq->query, sizeof(rq->instance));
145 replace_special(rq->instance, sizeof(rq->instance));
147 for (i = 0; i < ci->children_num; i++) {
148 oconfig_item_t *option = ci->children + i;
150 if (strcasecmp("Type", option->key) == 0) {
151 status = cf_util_get_string_buffer(option, rq->type, sizeof(rq->type));
152 } else if (strcasecmp("Instance", option->key) == 0) {
153 status = cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance));
155 WARNING("redis plugin: unknown configuration option: %s", option->key);
167 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
175 memset (&rn, 0, sizeof (rn));
176 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
177 rn.port = REDIS_DEF_PORT;
178 rn.timeout.tv_usec = REDIS_DEF_TIMEOUT;
181 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
185 for (i = 0; i < ci->children_num; i++)
187 oconfig_item_t *option = ci->children + i;
189 if (strcasecmp ("Host", option->key) == 0)
190 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
191 else if (strcasecmp ("Port", option->key) == 0)
193 status = cf_util_get_port_number (option);
200 else if (strcasecmp ("Query", option->key) == 0)
202 rq = redis_config_query(option);
206 rq->next = rn.queries;
210 else if (strcasecmp ("Timeout", option->key) == 0)
212 status = cf_util_get_int (option, &timeout);
213 if (status == 0) rn.timeout.tv_usec = timeout;
215 else if (strcasecmp ("Password", option->key) == 0)
216 status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
218 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
219 "block. I'll ignore this option.", option->key);
228 return (redis_node_add (&rn));
229 } /* }}} int redis_config_node */
231 static int redis_config (oconfig_item_t *ci) /* {{{ */
235 for (i = 0; i < ci->children_num; i++)
237 oconfig_item_t *option = ci->children + i;
239 if (strcasecmp ("Node", option->key) == 0)
240 redis_config_node (option);
242 WARNING ("redis plugin: Option `%s' not allowed in redis"
243 " configuration. It will be ignored.", option->key);
246 if (nodes_head == NULL)
248 ERROR ("redis plugin: No valid node configuration could be found.");
255 __attribute__ ((nonnull(2)))
256 static void redis_submit (char *plugin_instance,
257 const char *type, const char *type_instance,
258 value_t value) /* {{{ */
261 value_list_t vl = VALUE_LIST_INIT;
267 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
268 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
269 if (plugin_instance != NULL)
270 sstrncpy (vl.plugin_instance, plugin_instance,
271 sizeof (vl.plugin_instance));
272 sstrncpy (vl.type, type, sizeof (vl.type));
273 if (type_instance != NULL)
274 sstrncpy (vl.type_instance, type_instance,
275 sizeof (vl.type_instance));
277 plugin_dispatch_values (&vl);
280 static int redis_init (void) /* {{{ */
284 .host = REDIS_DEF_HOST,
285 .port = REDIS_DEF_PORT,
287 .timeout.tv_usec = REDIS_DEF_TIMEOUT,
291 if (nodes_head == NULL)
292 redis_node_add (&rn);
295 } /* }}} int redis_init */
297 int redis_handle_info (char *node, char const *info_line, char const *type, char const *type_instance, char const *field_name, int ds_type) /* {{{ */
299 char *str = strstr (info_line, field_name);
300 static char buf[MAX_REDIS_VAL_SIZE];
306 str += strlen (field_name) + 1; /* also skip the ':' */
307 for(i=0;(*str && (isdigit(*str) || *str == '.'));i++,str++)
311 if(parse_value (buf, &val, ds_type) == -1)
313 WARNING ("redis plugin: Unable to parse field `%s'.", field_name);
317 redis_submit (node, type, type_instance, val);
322 } /* }}} int redis_handle_info */
324 int redis_handle_query (redisContext *rh, redis_node_t *rn, redis_query_t *rq) /* {{{ */
327 const data_set_t *ds;
330 ds = plugin_get_ds (rq->type);
332 ERROR ("redis plugin: DataSet `%s' not defined.", rq->type);
336 if (ds->ds_num != 1) {
337 ERROR ("redis plugin: DS `%s' has too many types.", rq->type);
341 if ((rr = redisCommand(rh, rq->query)) == NULL) {
342 WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
347 case REDIS_REPLY_INTEGER:
348 switch (ds->ds[0].type) {
349 case DS_TYPE_COUNTER:
350 val.counter = (counter_t)rr->integer;
353 val.gauge = (gauge_t)rr->integer;
356 val.gauge = (derive_t)rr->integer;
358 case DS_TYPE_ABSOLUTE:
359 val.gauge = (absolute_t)rr->integer;
363 case REDIS_REPLY_STRING:
364 if (parse_value (rr->str, &val, ds->ds[0].type) == -1) {
365 WARNING("redis plugin: Unable to parse field `%s'.", rq->type);
366 freeReplyObject (rr);
371 WARNING("redis plugin: Cannot coerce redis type.");
376 redis_submit(rn->name, rq->type, (strlen(rq->instance) >0)?rq->instance:NULL, val);
377 freeReplyObject (rr);
379 } /* }}} int redis_handle_info */
381 static int redis_read (void) /* {{{ */
386 for (rn = nodes_head; rn != NULL; rn = rn->next)
391 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
393 rh = redisConnectWithTimeout ((char *)rn->host, rn->port, rn->timeout);
396 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
400 if (strlen (rn->passwd) > 0)
402 DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
403 rr = redisCommand (rh, "AUTH %s", rn->passwd);
405 if (rr == NULL || rr->type != REDIS_REPLY_STATUS)
407 WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
409 freeReplyObject (rr);
416 if ((rr = redisCommand(rh, "INFO")) == NULL)
418 WARNING ("redis plugin: unable to connect to node `%s'.", rn->name);
423 redis_handle_info (rn->name, rr->str, "uptime", NULL, "uptime_in_seconds", DS_TYPE_GAUGE);
424 redis_handle_info (rn->name, rr->str, "current_connections", "clients", "connected_clients", DS_TYPE_GAUGE);
425 redis_handle_info (rn->name, rr->str, "blocked_clients", NULL, "blocked_clients", DS_TYPE_GAUGE);
426 redis_handle_info (rn->name, rr->str, "memory", NULL, "used_memory", DS_TYPE_GAUGE);
427 redis_handle_info (rn->name, rr->str, "memory_lua", NULL, "used_memory_lua", DS_TYPE_GAUGE);
428 /* changes_since_last_save: Deprecated in redis version 2.6 and above */
429 redis_handle_info (rn->name, rr->str, "volatile_changes", NULL, "changes_since_last_save", DS_TYPE_GAUGE);
430 redis_handle_info (rn->name, rr->str, "total_connections", NULL, "total_connections_received", DS_TYPE_DERIVE);
431 redis_handle_info (rn->name, rr->str, "total_operations", NULL, "total_commands_processed", DS_TYPE_DERIVE);
432 redis_handle_info (rn->name, rr->str, "expired_keys", NULL, "expired_keys", DS_TYPE_GAUGE);
433 redis_handle_info (rn->name, rr->str, "pubsub", "channels", "pubsub_channels", DS_TYPE_GAUGE);
434 redis_handle_info (rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns", DS_TYPE_GAUGE);
435 redis_handle_info (rn->name, rr->str, "current_connections", "slaves", "connected_slaves", DS_TYPE_GAUGE);
437 freeReplyObject (rr);
439 for (rq = rn->queries; rq != NULL; rq = rq->next)
440 redis_handle_query(rh, rn, rq);
449 void module_register (void) /* {{{ */
451 plugin_register_complex_config ("redis", redis_config);
452 plugin_register_init ("redis", redis_init);
453 plugin_register_read ("redis", redis_read);
454 /* TODO: plugin_register_write: one redis list per value id with
459 /* vim: set sw=2 sts=2 et fdm=marker : */