83cf6f59142665f2f94438c58e6be296bcf18352
[collectd.git] / src / redis.c
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  **/
22
23 #include "collectd.h"
24
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <hiredis/hiredis.h>
29 #include <sys/time.h>
30
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
38
39 /* Redis plugin configuration example:
40  *
41  * <Plugin redis>
42  *   <Node "mynode">
43  *     Host "localhost"
44  *     Port "6379"
45  *     Timeout 2
46  *     Password "foobar"
47  *   </Node>
48  * </Plugin>
49  */
50
51 struct redis_query_s;
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];
57   int database;
58
59   redis_query_t *next;
60 };
61
62 struct redis_node_s;
63 typedef struct redis_node_s redis_node_t;
64 struct redis_node_s {
65   char *name;
66   char *host;
67   char *passwd;
68   int port;
69   struct timeval timeout;
70   redisContext *redisContext;
71   redis_query_t *queries;
72
73   redis_node_t *next;
74 };
75
76 static bool redis_have_instances;
77 static int redis_read(user_data_t *user_data);
78
79 static void redis_node_free(void *arg) {
80   redis_node_t *rn = arg;
81   if (rn == NULL)
82     return;
83
84   redis_query_t *rq = rn->queries;
85   while (rq != NULL) {
86     redis_query_t *next = rq->next;
87     sfree(rq);
88     rq = next;
89   }
90
91   redisFree(rn->redisContext);
92   sfree(rn->name);
93   sfree(rn->host);
94   sfree(rn->passwd);
95   sfree(rn);
96 } /* void redis_node_free */
97
98 static int redis_node_add(redis_node_t *rn) /* {{{ */
99 {
100   DEBUG("redis plugin: Adding node \"%s\".", rn->name);
101
102   /* Disable automatic generation of default instance in the init callback. */
103   redis_have_instances = true;
104
105   char cb_name[sizeof("redis/") + DATA_MAX_NAME_LEN];
106   snprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
107
108   return plugin_register_complex_read(
109       /* group = */ "redis",
110       /* name      = */ cb_name,
111       /* callback  = */ redis_read,
112       /* interval  = */ 0,
113       &(user_data_t){
114           .data = rn, .free_func = redis_node_free,
115       });
116 } /* }}} */
117
118 static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */
119 {
120   redis_query_t *rq;
121   int status;
122
123   rq = calloc(1, sizeof(*rq));
124   if (rq == NULL) {
125     ERROR("redis plugin: calloc failed adding redis_query.");
126     return NULL;
127   }
128   status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
129   if (status != 0)
130     goto err;
131
132   /*
133    * Default to a gauge type.
134    */
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));
138
139   rq->database = 0;
140
141   for (int i = 0; i < ci->children_num; i++) {
142     oconfig_item_t *option = ci->children + i;
143
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) {
147       status =
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 "
153                 "integer or zero");
154         status = -1;
155       }
156     } else {
157       WARNING("redis plugin: unknown configuration option: %s", option->key);
158       status = -1;
159     }
160     if (status != 0)
161       goto err;
162   }
163   return rq;
164 err:
165   free(rq);
166   return NULL;
167 } /* }}} */
168
169 static int redis_config_node(oconfig_item_t *ci) /* {{{ */
170 {
171   redis_node_t *rn = calloc(1, sizeof(*rn));
172   if (rn == NULL) {
173     ERROR("redis plugin: calloc failed adding node.");
174     return ENOMEM;
175   }
176
177   rn->port = REDIS_DEF_PORT;
178   rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
179
180   rn->host = strdup(REDIS_DEF_HOST);
181   if (rn->host == NULL) {
182     ERROR("redis plugin: strdup failed adding node.");
183     sfree(rn);
184     return ENOMEM;
185   }
186
187   int status = cf_util_get_string(ci, &rn->name);
188   if (status != 0) {
189     sfree(rn->host);
190     sfree(rn);
191     return status;
192   }
193
194   for (int i = 0; i < ci->children_num; i++) {
195     oconfig_item_t *option = ci->children + i;
196
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);
201       if (status > 0) {
202         rn->port = status;
203         status = 0;
204       }
205     } else if (strcasecmp("Query", option->key) == 0) {
206       redis_query_t *rq = redis_config_query(option);
207       if (rq == NULL) {
208         status = 1;
209       } else {
210         rq->next = rn->queries;
211         rn->queries = rq;
212       }
213     } else if (strcasecmp("Timeout", option->key) == 0) {
214       int timeout;
215       status = cf_util_get_int(option, &timeout);
216       if (status == 0) {
217         rn->timeout.tv_usec = timeout * 1000;
218         rn->timeout.tv_sec = rn->timeout.tv_usec / 1000000L;
219         rn->timeout.tv_usec %= 1000000L;
220       }
221     } else if (strcasecmp("Password", option->key) == 0)
222       status = cf_util_get_string(option, &rn->passwd);
223     else
224       WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
225               "block. I'll ignore this option.",
226               option->key);
227
228     if (status != 0)
229       break;
230   }
231
232   if (status != 0) {
233     redis_node_free(rn);
234     return status;
235   }
236
237   return redis_node_add(rn);
238 } /* }}} int redis_config_node */
239
240 static int redis_config(oconfig_item_t *ci) /* {{{ */
241 {
242   for (int i = 0; i < ci->children_num; i++) {
243     oconfig_item_t *option = ci->children + i;
244
245     if (strcasecmp("Node", option->key) == 0)
246       redis_config_node(option);
247     else
248       WARNING("redis plugin: Option `%s' not allowed in redis"
249               " configuration. It will be ignored.",
250               option->key);
251   }
252
253   return 0;
254 } /* }}} */
255
256 __attribute__((nonnull(2))) static void
257 redis_submit(char *plugin_instance, const char *type, const char *type_instance,
258              value_t value) /* {{{ */
259 {
260   value_list_t vl = VALUE_LIST_INIT;
261
262   vl.values = &value;
263   vl.values_len = 1;
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));
270
271   plugin_dispatch_values(&vl);
272 } /* }}} */
273
274 static int redis_init(void) /* {{{ */
275 {
276   if (redis_have_instances)
277     return 0;
278
279   redis_node_t *rn = calloc(1, sizeof(*rn));
280   if (rn == NULL)
281     return ENOMEM;
282
283   rn->port = REDIS_DEF_PORT;
284   rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
285
286   rn->name = strdup("default");
287   rn->host = strdup(REDIS_DEF_HOST);
288
289   if (rn->name == NULL || rn->host == NULL)
290     return ENOMEM;
291
292   return redis_node_add(rn);
293 } /* }}} int redis_init */
294
295 static void *c_redisCommand(redis_node_t *rn, const char *format, ...) {
296   redisContext *c = rn->redisContext;
297
298   if (c == NULL)
299     return NULL;
300
301   va_list ap;
302   va_start(ap, format);
303   void *reply = redisvCommand(c, format, ap);
304   va_end(ap);
305
306   if (reply == NULL) {
307     ERROR("redis plugin: Connection error: %s", c->errstr);
308     redisFree(rn->redisContext);
309     rn->redisContext = NULL;
310   }
311
312   return reply;
313 } /* void c_redisCommand */
314
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) /* {{{ */
318 {
319   char *str = strstr(info_line, field_name);
320   static char buf[MAX_REDIS_VAL_SIZE];
321   value_t val;
322   if (str) {
323     int i;
324
325     str += strlen(field_name) + 1; /* also skip the ':' */
326     for (i = 0; (*str && (isdigit((unsigned char)*str) || *str == '.'));
327          i++, str++)
328       buf[i] = *str;
329     buf[i] = '\0';
330
331     if (parse_value(buf, &val, ds_type) == -1) {
332       WARNING("redis plugin: Unable to parse field `%s'.", field_name);
333       return -1;
334     }
335
336     redis_submit(node, type, type_instance, val);
337     return 0;
338   }
339   return -1;
340
341 } /* }}} int redis_handle_info */
342
343 static int redis_handle_query(redis_node_t *rn, redis_query_t *rq) /* {{{ */
344 {
345   redisReply *rr;
346   const data_set_t *ds;
347   value_t val;
348
349   ds = plugin_get_ds(rq->type);
350   if (!ds) {
351     ERROR("redis plugin: DS type `%s' not defined.", rq->type);
352     return -1;
353   }
354
355   if (ds->ds_num != 1) {
356     ERROR("redis plugin: DS type `%s' has too many datasources. This is not "
357           "supported currently.",
358           rq->type);
359     return -1;
360   }
361
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);
365     return -1;
366   }
367
368   if ((rr = c_redisCommand(rn, rq->query)) == NULL) {
369     WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
370     return -1;
371   }
372
373   switch (rr->type) {
374   case REDIS_REPLY_INTEGER:
375     switch (ds->ds[0].type) {
376     case DS_TYPE_COUNTER:
377       val.counter = (counter_t)rr->integer;
378       break;
379     case DS_TYPE_GAUGE:
380       val.gauge = (gauge_t)rr->integer;
381       break;
382     case DS_TYPE_DERIVE:
383       val.gauge = (derive_t)rr->integer;
384       break;
385     case DS_TYPE_ABSOLUTE:
386       val.gauge = (absolute_t)rr->integer;
387       break;
388     }
389     break;
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);
393       freeReplyObject(rr);
394       return -1;
395     }
396     break;
397   case REDIS_REPLY_ERROR:
398     WARNING("redis plugin: Query `%s' failed: %s.", rq->query, rr->str);
399     freeReplyObject(rr);
400     return -1;
401   case REDIS_REPLY_ARRAY:
402     WARNING("redis plugin: Query `%s' should return string or integer. Arrays "
403             "are not supported.",
404             rq->query);
405     freeReplyObject(rr);
406     return -1;
407   default:
408     WARNING("redis plugin: Query `%s': Cannot coerce redis type (%i).",
409             rq->query, rr->type);
410     freeReplyObject(rr);
411     return -1;
412   }
413
414   redis_submit(rn->name, rq->type,
415                (strlen(rq->instance) > 0) ? rq->instance : NULL, val);
416   freeReplyObject(rr);
417   return 0;
418 } /* }}} int redis_handle_query */
419
420 static int redis_db_stats(char *node, char const *info_line) /* {{{ */
421 {
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
426    */
427
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];
432     value_t val;
433     char *str;
434     int i;
435
436     snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
437
438     str = strstr(info_line, field_name);
439     if (!str)
440       continue;
441
442     str += strlen(field_name);
443     for (i = 0; (*str && isdigit((int)*str)); i++, str++)
444       buf[i] = *str;
445     buf[i] = '\0';
446
447     if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
448       WARNING("redis plugin: Unable to parse field `%s'.", field_name);
449       return -1;
450     }
451
452     snprintf(db_id, sizeof(db_id), "%d", db);
453     redis_submit(node, "records", db_id, val);
454   }
455   return 0;
456
457 } /* }}} int redis_db_stats */
458
459 static void redis_check_connection(redis_node_t *rn) {
460   if (rn->redisContext)
461     return;
462
463   redisContext *rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
464
465   if (rh == NULL) {
466     ERROR("redis plugin: can't allocate redis context");
467     return;
468   }
469   if (rh->err) {
470     ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", rn->name,
471           rn->host, rn->port, rh->errstr);
472     redisFree(rh);
473     return;
474   }
475
476   rn->redisContext = rh;
477
478   if (rn->passwd) {
479     redisReply *rr;
480
481     DEBUG("redis plugin: authenticating node `%s' passwd(%s).", rn->name,
482           rn->passwd);
483
484     if ((rr = c_redisCommand(rn, "AUTH %s", rn->passwd)) == NULL) {
485       WARNING("redis plugin: unable to authenticate on node `%s'.", rn->name);
486       return;
487     }
488
489     if (rr->type != REDIS_REPLY_STATUS) {
490       WARNING("redis plugin: invalid authentication on node `%s'.", rn->name);
491       freeReplyObject(rr);
492       redisFree(rn->redisContext);
493       rn->redisContext = NULL;
494       return;
495     }
496
497     freeReplyObject(rr);
498   }
499   return;
500 } /* void redis_check_connection */
501
502 static void redis_read_server_info(redis_node_t *rn) {
503   redisReply *rr;
504
505   if ((rr = c_redisCommand(rn, "INFO")) == NULL) {
506     WARNING("redis plugin: unable to get INFO from node `%s'.", rn->name);
507     return;
508   }
509
510   redis_handle_info(rn->name, rr->str, "uptime", NULL, "uptime_in_seconds",
511                     DS_TYPE_GAUGE);
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",
517                     DS_TYPE_GAUGE);
518   redis_handle_info(rn->name, rr->str, "memory_lua", NULL, "used_memory_lua",
519                     DS_TYPE_GAUGE);
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",
530                     DS_TYPE_DERIVE);
531   redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
532                     DS_TYPE_DERIVE);
533   redis_handle_info(rn->name, rr->str, "pubsub", "channels", "pubsub_channels",
534                     DS_TYPE_GAUGE);
535   redis_handle_info(rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns",
536                     DS_TYPE_GAUGE);
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",
540                     DS_TYPE_DERIVE);
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);
547
548   redis_db_stats(rn->name, rr->str);
549
550   freeReplyObject(rr);
551 } /* void redis_read_server_info */
552
553 static int redis_read(user_data_t *user_data) /* {{{ */
554 {
555   redis_node_t *rn = user_data->data;
556
557   DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
558         rn->host, rn->port);
559
560   redis_check_connection(rn);
561
562   if (!rn->redisContext) /* no connection */
563     return -1;
564
565   redis_read_server_info(rn);
566
567   if (!rn->redisContext) /* connection lost */
568     return -1;
569
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 */
573       return -1;
574   }
575
576   return 0;
577 }
578 /* }}} */
579
580 void module_register(void) /* {{{ */
581 {
582   plugin_register_complex_config("redis", redis_config);
583   plugin_register_init("redis", redis_init);
584 }
585 /* }}} */