ea997ac9571e7792254846c717b188f0bf2e76a9
1 /**
2 * collectd - src/redis.c, based on src/memcached.c
3 * Copyright (C) 2010 Andrés J. Díaz <ajdiaz@connectical.com>
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Andrés J. Díaz <ajdiaz@connectical.com>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #include <pthread.h>
29 #include <sys/time.h>
30 #include <hiredis/hiredis.h>
32 #ifndef HOST_NAME_MAX
33 # define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
34 #endif
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:
46 *
47 * <Plugin redis>
48 * <Node "mynode">
49 * Host "localhost"
50 * Port "6379"
51 * Timeout 2
52 * Password "foobar"
53 * </Node>
54 * </Plugin>
55 */
57 struct redis_query_s;
58 typedef struct redis_query_s redis_query_t;
59 struct redis_query_s
60 {
61 char query[MAX_REDIS_QUERY];
62 char type[DATA_MAX_NAME_LEN];
63 char instance[DATA_MAX_NAME_LEN];
64 redis_query_t *next;
65 };
67 struct redis_node_s;
68 typedef struct redis_node_s redis_node_t;
69 struct redis_node_s
70 {
71 char name[MAX_REDIS_NODE_NAME];
72 char host[HOST_NAME_MAX];
73 char passwd[MAX_REDIS_PASSWD_LENGTH];
74 int port;
75 struct timeval timeout;
76 redis_query_t *queries;
78 redis_node_t *next;
79 };
81 static redis_node_t *nodes_head = NULL;
83 static int redis_node_add (const redis_node_t *rn) /* {{{ */
84 {
85 redis_node_t *rn_copy;
86 redis_node_t *rn_ptr;
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)
91 break;
93 if (rn_ptr != NULL)
94 {
95 ERROR ("redis plugin: A node with the name `%s' already exists.",
96 rn->name);
97 return (-1);
98 }
100 rn_copy = malloc (sizeof (*rn_copy));
101 if (rn_copy == NULL)
102 {
103 ERROR ("redis plugin: malloc failed adding redis_node to the tree.");
104 return (-1);
105 }
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;
114 else
115 {
116 rn_ptr = nodes_head;
117 while (rn_ptr->next != NULL)
118 rn_ptr = rn_ptr->next;
119 rn_ptr->next = rn_copy;
120 }
122 return (0);
123 } /* }}} */
125 static redis_query_t *redis_config_query (oconfig_item_t *ci) /* {{{ */
126 {
127 redis_query_t *rc;
128 int status;
129 int i;
131 rc = calloc(1, sizeof(*rc));
132 if (rc == NULL) {
133 ERROR("redis plugin: calloca failed adding redis_query.");
134 return NULL;
135 }
136 status = cf_util_get_string_buffer(ci, rc->type, sizeof(rc->type));
137 if (status != 0)
138 goto err;
140 for (i = 0; i < ci->children_num; i++) {
141 oconfig_item_t *option = ci->children + i;
143 if (strcasecmp("Exec", option->key) == 0) {
144 status = cf_util_get_string_buffer(option, rc->query, sizeof(rc->query));
145 } else if (strcasecmp("Instance", option->key) == 0) {
146 status = cf_util_get_string_buffer(option, rc->instance, sizeof(rc->instance));
147 }
148 if (status != 0)
149 goto err;
150 }
151 if (strlen(rc->query) == 0) {
152 WARNING("redis plugin: invalid query definition for: %s", rc->type);
153 goto err;
154 }
155 return rc;
156 err:
157 free(rc);
158 return NULL;
159 } /* }}} */
161 static int redis_config_node (oconfig_item_t *ci) /* {{{ */
162 {
163 redis_node_t rn;
164 redis_query_t *rc;
165 int i;
166 int status;
167 int timeout;
169 memset (&rn, 0, sizeof (rn));
170 sstrncpy (rn.host, REDIS_DEF_HOST, sizeof (rn.host));
171 rn.port = REDIS_DEF_PORT;
172 rn.timeout.tv_usec = REDIS_DEF_TIMEOUT;
173 rn.queries = NULL;
175 status = cf_util_get_string_buffer (ci, rn.name, sizeof (rn.name));
176 if (status != 0)
177 return (status);
179 for (i = 0; i < ci->children_num; i++)
180 {
181 oconfig_item_t *option = ci->children + i;
183 if (strcasecmp ("Host", option->key) == 0)
184 status = cf_util_get_string_buffer (option, rn.host, sizeof (rn.host));
185 else if (strcasecmp ("Port", option->key) == 0)
186 {
187 status = cf_util_get_port_number (option);
188 if (status > 0)
189 {
190 rn.port = status;
191 status = 0;
192 }
193 }
194 else if (strcasecmp ("Query", option->key) == 0)
195 {
196 rc = redis_config_query(option);
197 if (rc == NULL) {
198 status =1;
199 } else {
200 rc->next = rn.queries;
201 rn.queries = rc;
202 }
203 }
204 else if (strcasecmp ("Timeout", option->key) == 0)
205 {
206 status = cf_util_get_int (option, &timeout);
207 if (status == 0) rn.timeout.tv_usec = timeout;
208 }
209 else if (strcasecmp ("Password", option->key) == 0)
210 status = cf_util_get_string_buffer (option, rn.passwd, sizeof (rn.passwd));
211 else
212 WARNING ("redis plugin: Option `%s' not allowed inside a `Node' "
213 "block. I'll ignore this option.", option->key);
215 if (status != 0)
216 break;
217 }
219 if (status != 0)
220 return (status);
222 return (redis_node_add (&rn));
223 } /* }}} int redis_config_node */
225 static int redis_config (oconfig_item_t *ci) /* {{{ */
226 {
227 int i;
229 for (i = 0; i < ci->children_num; i++)
230 {
231 oconfig_item_t *option = ci->children + i;
233 if (strcasecmp ("Node", option->key) == 0)
234 redis_config_node (option);
235 else
236 WARNING ("redis plugin: Option `%s' not allowed in redis"
237 " configuration. It will be ignored.", option->key);
238 }
240 if (nodes_head == NULL)
241 {
242 ERROR ("redis plugin: No valid node configuration could be found.");
243 return (ENOENT);
244 }
246 return (0);
247 } /* }}} */
249 __attribute__ ((nonnull(2)))
250 static void redis_submit (char *plugin_instance,
251 const char *type, const char *type_instance,
252 value_t value) /* {{{ */
253 {
254 value_t values[1];
255 value_list_t vl = VALUE_LIST_INIT;
257 values[0] = value;
259 vl.values = values;
260 vl.values_len = 1;
261 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
262 sstrncpy (vl.plugin, "redis", sizeof (vl.plugin));
263 if (plugin_instance != NULL)
264 sstrncpy (vl.plugin_instance, plugin_instance,
265 sizeof (vl.plugin_instance));
266 sstrncpy (vl.type, type, sizeof (vl.type));
267 if (type_instance != NULL)
268 sstrncpy (vl.type_instance, type_instance,
269 sizeof (vl.type_instance));
271 plugin_dispatch_values (&vl);
272 } /* }}} */
274 static int redis_init (void) /* {{{ */
275 {
276 redis_node_t rn = {
277 .name = "default",
278 .host = REDIS_DEF_HOST,
279 .port = REDIS_DEF_PORT,
280 .timeout.tv_sec = 0,
281 .timeout.tv_usec = REDIS_DEF_TIMEOUT,
282 .next = NULL
283 };
285 if (nodes_head == NULL)
286 redis_node_add (&rn);
288 return (0);
289 } /* }}} int redis_init */
291 int redis_handle_info (char *node, char const *info_line, char const *type, char const *type_instance, char const *field_name, int ds_type) /* {{{ */
292 {
293 char *str = strstr (info_line, field_name);
294 static char buf[MAX_REDIS_VAL_SIZE];
295 value_t val;
296 if (str)
297 {
298 int i;
300 str += strlen (field_name) + 1; /* also skip the ':' */
301 for(i=0;(*str && (isdigit(*str) || *str == '.'));i++,str++)
302 buf[i] = *str;
303 buf[i] ='\0';
305 if(parse_value (buf, &val, ds_type) == -1)
306 {
307 WARNING ("redis plugin: Unable to parse field `%s'.", field_name);
308 return (-1);
309 }
311 redis_submit (node, type, type_instance, val);
312 return (0);
313 }
314 return (-1);
316 } /* }}} int redis_handle_info */
318 int redis_handle_query (redisContext *rh, redis_node_t *rn, redis_query_t *rc) /* {{{ */
319 {
320 redisReply *rr;
321 const data_set_t *ds;
322 value_t val;
324 ds = plugin_get_ds (rc->type);
325 if (!ds) {
326 ERROR ("redis plugin: DataSet `%s' not defined.", rc->type);
327 return (-1);
328 }
330 if (ds->ds_num != 1) {
331 ERROR ("redis plugin: DS `%s' has too many types.", rc->type);
332 return (-1);
333 }
335 if ((rr = redisCommand(rh, rc->query)) == NULL) {
336 WARNING("redis plugin: unable to carry out query `%s'.", rc->query);
337 return (-1);
338 }
340 switch (rr->type) {
341 case REDIS_REPLY_INTEGER:
342 switch (ds->ds[0].type) {
343 case DS_TYPE_COUNTER:
344 val.counter = (counter_t)rr->integer;
345 break;
346 case DS_TYPE_GAUGE:
347 val.gauge = (gauge_t)rr->integer;
348 break;
349 case DS_TYPE_DERIVE:
350 val.gauge = (derive_t)rr->integer;
351 break;
352 case DS_TYPE_ABSOLUTE:
353 val.gauge = (absolute_t)rr->integer;
354 break;
355 }
356 break;
357 case REDIS_REPLY_STRING:
358 if (parse_value (rr->str, &val, ds->ds[0].type) == -1) {
359 WARNING("redis plugin: Unable to parse field `%s'.", rc->type);
360 freeReplyObject (rr);
361 return (-1);
362 }
363 break;
364 default:
365 WARNING("redis plugin: Cannot coerce redis type.");
366 freeReplyObject(rr);
367 return (-1);
368 }
370 redis_submit(rn->name, rc->type, (strlen(rc->instance) >0)?rc->instance:NULL, val);
371 freeReplyObject (rr);
372 return 0;
373 } /* }}} int redis_handle_info */
375 static int redis_read (void) /* {{{ */
376 {
377 redis_node_t *rn;
378 redis_query_t *rc;
380 for (rn = nodes_head; rn != NULL; rn = rn->next)
381 {
382 redisContext *rh;
383 redisReply *rr;
385 DEBUG ("redis plugin: querying info from node `%s' (%s:%d).", rn->name, rn->host, rn->port);
387 rh = redisConnectWithTimeout ((char *)rn->host, rn->port, rn->timeout);
388 if (rh == NULL)
389 {
390 ERROR ("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, rn->host, rn->port);
391 continue;
392 }
394 if (strlen (rn->passwd) > 0)
395 {
396 DEBUG ("redis plugin: authenticanting node `%s' passwd(%s).", rn->name, rn->passwd);
397 rr = redisCommand (rh, "AUTH %s", rn->passwd);
399 if (rr == NULL || rr->type != REDIS_REPLY_STATUS)
400 {
401 WARNING ("redis plugin: unable to authenticate on node `%s'.", rn->name);
402 if (rr != NULL)
403 freeReplyObject (rr);
405 redisFree (rh);
406 continue;
407 }
408 }
410 if ((rr = redisCommand(rh, "INFO")) == NULL)
411 {
412 WARNING ("redis plugin: unable to connect to node `%s'.", rn->name);
413 redisFree (rh);
414 continue;
415 }
417 redis_handle_info (rn->name, rr->str, "uptime", NULL, "uptime_in_seconds", DS_TYPE_GAUGE);
418 redis_handle_info (rn->name, rr->str, "current_connections", "clients", "connected_clients", DS_TYPE_GAUGE);
419 redis_handle_info (rn->name, rr->str, "blocked_clients", NULL, "blocked_clients", DS_TYPE_GAUGE);
420 redis_handle_info (rn->name, rr->str, "memory", NULL, "used_memory", DS_TYPE_GAUGE);
421 redis_handle_info (rn->name, rr->str, "memory_lua", NULL, "used_memory_lua", DS_TYPE_GAUGE);
422 /* changes_since_last_save: Deprecated in redis version 2.6 and above */
423 redis_handle_info (rn->name, rr->str, "volatile_changes", NULL, "changes_since_last_save", DS_TYPE_GAUGE);
424 redis_handle_info (rn->name, rr->str, "total_connections", NULL, "total_connections_received", DS_TYPE_DERIVE);
425 redis_handle_info (rn->name, rr->str, "total_operations", NULL, "total_commands_processed", DS_TYPE_DERIVE);
426 redis_handle_info (rn->name, rr->str, "expired_keys", NULL, "expired_keys", DS_TYPE_GAUGE);
427 redis_handle_info (rn->name, rr->str, "pubsub", "channels", "pubsub_channels", DS_TYPE_GAUGE);
428 redis_handle_info (rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns", DS_TYPE_GAUGE);
429 redis_handle_info (rn->name, rr->str, "current_connections", "slaves", "connected_slaves", DS_TYPE_GAUGE);
431 freeReplyObject (rr);
433 for (rc = rn->queries; rc != NULL; rc = rc->next)
434 redis_handle_query(rh, rn, rc);
436 redisFree (rh);
437 }
439 return 0;
440 }
441 /* }}} */
443 void module_register (void) /* {{{ */
444 {
445 plugin_register_complex_config ("redis", redis_config);
446 plugin_register_init ("redis", redis_init);
447 plugin_register_read ("redis", redis_read);
448 /* TODO: plugin_register_write: one redis list per value id with
449 * X elements */
450 }
451 /* }}} */
453 /* vim: set sw=2 sts=2 et fdm=marker : */