Merge pull request #3339 from jkohen/patch-1
[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 "plugin.h"
26 #include "utils/common/common.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 prev_s {
63   derive_t keyspace_hits;
64   derive_t keyspace_misses;
65 };
66 typedef struct prev_s prev_t;
67
68 struct redis_node_s;
69 typedef struct redis_node_s redis_node_t;
70 struct redis_node_s {
71   char *name;
72   char *host;
73   char *socket;
74   char *passwd;
75   int port;
76   struct timeval timeout;
77   bool report_command_stats;
78   bool report_cpu_usage;
79   redisContext *redisContext;
80   redis_query_t *queries;
81   prev_t prev;
82
83   redis_node_t *next;
84 };
85
86 static bool redis_have_instances;
87 static int redis_read(user_data_t *user_data);
88
89 static void redis_node_free(void *arg) {
90   redis_node_t *rn = arg;
91   if (rn == NULL)
92     return;
93
94   redis_query_t *rq = rn->queries;
95   while (rq != NULL) {
96     redis_query_t *next = rq->next;
97     sfree(rq);
98     rq = next;
99   }
100
101   if (rn->redisContext)
102     redisFree(rn->redisContext);
103   sfree(rn->name);
104   sfree(rn->host);
105   sfree(rn->socket);
106   sfree(rn->passwd);
107   sfree(rn);
108 } /* void redis_node_free */
109
110 static int redis_node_add(redis_node_t *rn) /* {{{ */
111 {
112   DEBUG("redis plugin: Adding node \"%s\".", rn->name);
113
114   /* Disable automatic generation of default instance in the init callback. */
115   redis_have_instances = true;
116
117   char cb_name[sizeof("redis/") + DATA_MAX_NAME_LEN];
118   ssnprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
119
120   return plugin_register_complex_read(
121       /* group = */ "redis",
122       /* name      = */ cb_name,
123       /* callback  = */ redis_read,
124       /* interval  = */ 0,
125       &(user_data_t){
126           .data = rn,
127           .free_func = redis_node_free,
128       });
129 } /* }}} */
130
131 static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */
132 {
133   redis_query_t *rq;
134   int status;
135
136   rq = calloc(1, sizeof(*rq));
137   if (rq == NULL) {
138     ERROR("redis plugin: calloc failed adding redis_query.");
139     return NULL;
140   }
141   status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query));
142   if (status != 0)
143     goto err;
144
145   /*
146    * Default to a gauge type.
147    */
148   (void)strncpy(rq->type, "gauge", sizeof(rq->type));
149   (void)sstrncpy(rq->instance, rq->query, sizeof(rq->instance));
150   replace_special(rq->instance, sizeof(rq->instance));
151
152   rq->database = 0;
153
154   for (int i = 0; i < ci->children_num; i++) {
155     oconfig_item_t *option = ci->children + i;
156
157     if (strcasecmp("Type", option->key) == 0) {
158       status = cf_util_get_string_buffer(option, rq->type, sizeof(rq->type));
159     } else if (strcasecmp("Instance", option->key) == 0) {
160       status =
161           cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance));
162     } else if (strcasecmp("Database", option->key) == 0) {
163       status = cf_util_get_int(option, &rq->database);
164       if (rq->database < 0) {
165         WARNING("redis plugin: The \"Database\" option must be positive "
166                 "integer or zero");
167         status = -1;
168       }
169     } else {
170       WARNING("redis plugin: unknown configuration option: %s", option->key);
171       status = -1;
172     }
173     if (status != 0)
174       goto err;
175   }
176   return rq;
177 err:
178   free(rq);
179   return NULL;
180 } /* }}} */
181
182 static int redis_config_node(oconfig_item_t *ci) /* {{{ */
183 {
184   redis_node_t *rn = calloc(1, sizeof(*rn));
185   if (rn == NULL) {
186     ERROR("redis plugin: calloc failed adding node.");
187     return ENOMEM;
188   }
189
190   rn->port = REDIS_DEF_PORT;
191   rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
192   rn->report_cpu_usage = true;
193
194   rn->host = strdup(REDIS_DEF_HOST);
195   if (rn->host == NULL) {
196     ERROR("redis plugin: strdup failed adding node.");
197     sfree(rn);
198     return ENOMEM;
199   }
200
201   int status = cf_util_get_string(ci, &rn->name);
202   if (status != 0) {
203     sfree(rn->host);
204     sfree(rn);
205     return status;
206   }
207
208   for (int i = 0; i < ci->children_num; i++) {
209     oconfig_item_t *option = ci->children + i;
210
211     if (strcasecmp("Host", option->key) == 0)
212       status = cf_util_get_string(option, &rn->host);
213     else if (strcasecmp("Port", option->key) == 0) {
214       status = cf_util_get_port_number(option);
215       if (status > 0) {
216         rn->port = status;
217         status = 0;
218       }
219     } else if (strcasecmp("Socket", option->key) == 0) {
220       status = cf_util_get_string(option, &rn->socket);
221     } else if (strcasecmp("Query", option->key) == 0) {
222       redis_query_t *rq = redis_config_query(option);
223       if (rq == NULL) {
224         status = 1;
225       } else {
226         rq->next = rn->queries;
227         rn->queries = rq;
228       }
229     } else if (strcasecmp("Timeout", option->key) == 0) {
230       int timeout;
231       status = cf_util_get_int(option, &timeout);
232       if (status == 0) {
233         rn->timeout.tv_usec = timeout * 1000;
234         rn->timeout.tv_sec = rn->timeout.tv_usec / 1000000L;
235         rn->timeout.tv_usec %= 1000000L;
236       }
237     } else if (strcasecmp("Password", option->key) == 0)
238       status = cf_util_get_string(option, &rn->passwd);
239     else if (strcasecmp("ReportCommandStats", option->key) == 0)
240       status = cf_util_get_boolean(option, &rn->report_command_stats);
241     else if (strcasecmp("ReportCpuUsage", option->key) == 0)
242       status = cf_util_get_boolean(option, &rn->report_cpu_usage);
243     else
244       WARNING("redis plugin: Option `%s' not allowed inside a `Node' "
245               "block. I'll ignore this option.",
246               option->key);
247
248     if (status != 0)
249       break;
250   }
251
252   if (status != 0) {
253     redis_node_free(rn);
254     return status;
255   }
256
257   return redis_node_add(rn);
258 } /* }}} int redis_config_node */
259
260 static int redis_config(oconfig_item_t *ci) /* {{{ */
261 {
262   for (int i = 0; i < ci->children_num; i++) {
263     oconfig_item_t *option = ci->children + i;
264
265     if (strcasecmp("Node", option->key) == 0)
266       redis_config_node(option);
267     else
268       WARNING("redis plugin: Option `%s' not allowed in redis"
269               " configuration. It will be ignored.",
270               option->key);
271   }
272
273   return 0;
274 } /* }}} */
275
276 __attribute__((nonnull(2))) static void
277 redis_submit(const char *plugin_instance, const char *type,
278              const char *type_instance, value_t value) /* {{{ */
279 {
280   value_list_t vl = VALUE_LIST_INIT;
281
282   vl.values = &value;
283   vl.values_len = 1;
284   sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
285   if (plugin_instance != NULL)
286     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
287   sstrncpy(vl.type, type, sizeof(vl.type));
288   if (type_instance != NULL)
289     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
290
291   plugin_dispatch_values(&vl);
292 } /* }}} */
293
294 __attribute__((nonnull(2))) static void
295 redis_submit2(const char *plugin_instance, const char *type,
296               const char *type_instance, value_t value0,
297               value_t value1) /* {{{ */
298 {
299   value_list_t vl = VALUE_LIST_INIT;
300   value_t values[] = {value0, value1};
301
302   vl.values = values;
303   vl.values_len = STATIC_ARRAY_SIZE(values);
304
305   sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
306   sstrncpy(vl.type, type, sizeof(vl.type));
307
308   if (plugin_instance != NULL)
309     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
310
311   if (type_instance != NULL)
312     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
313
314   plugin_dispatch_values(&vl);
315 } /* }}} */
316
317 static int redis_init(void) /* {{{ */
318 {
319   if (redis_have_instances)
320     return 0;
321
322   redis_node_t *rn = calloc(1, sizeof(*rn));
323   if (rn == NULL)
324     return ENOMEM;
325
326   rn->port = REDIS_DEF_PORT;
327   rn->timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC;
328
329   rn->name = strdup("default");
330   rn->host = strdup(REDIS_DEF_HOST);
331
332   if (rn->name == NULL || rn->host == NULL) {
333     sfree(rn->name);
334     sfree(rn->host);
335     sfree(rn);
336     return ENOMEM;
337   }
338
339   return redis_node_add(rn);
340 } /* }}} int redis_init */
341
342 static void *c_redisCommand(redis_node_t *rn, const char *format, ...) {
343   redisContext *c = rn->redisContext;
344
345   if (c == NULL)
346     return NULL;
347
348   va_list ap;
349   va_start(ap, format);
350   void *reply = redisvCommand(c, format, ap);
351   va_end(ap);
352
353   if (reply == NULL) {
354     ERROR("redis plugin: Connection error: %s", c->errstr);
355     redisFree(rn->redisContext);
356     rn->redisContext = NULL;
357   }
358
359   return reply;
360 } /* void c_redisCommand */
361
362 static int redis_get_info_value(char const *info_line, char const *field_name,
363                                 int ds_type, value_t *val) {
364   char *str = strstr(info_line, field_name);
365   static char buf[MAX_REDIS_VAL_SIZE];
366   if (str) {
367     int i;
368
369     str += strlen(field_name) + 1; /* also skip the ':' */
370     for (i = 0; (*str && (isdigit((unsigned char)*str) || *str == '.'));
371          i++, str++)
372       buf[i] = *str;
373     buf[i] = '\0';
374
375     if (parse_value(buf, val, ds_type) == -1) {
376       WARNING("redis plugin: Unable to parse field `%s'.", field_name);
377       return -1;
378     }
379
380     return 0;
381   }
382   return -1;
383 } /* int redis_get_info_value */
384
385 static int redis_handle_info(char *node, char const *info_line,
386                              char const *type, char const *type_instance,
387                              char const *field_name, int ds_type) /* {{{ */
388 {
389   value_t val;
390   if (redis_get_info_value(info_line, field_name, ds_type, &val) != 0)
391     return -1;
392
393   redis_submit(node, type, type_instance, val);
394   return 0;
395 } /* }}} int redis_handle_info */
396
397 static int redis_handle_query(redis_node_t *rn, redis_query_t *rq) /* {{{ */
398 {
399   redisReply *rr;
400   const data_set_t *ds;
401   value_t val;
402
403   ds = plugin_get_ds(rq->type);
404   if (!ds) {
405     ERROR("redis plugin: DS type `%s' not defined.", rq->type);
406     return -1;
407   }
408
409   if (ds->ds_num != 1) {
410     ERROR("redis plugin: DS type `%s' has too many datasources. This is not "
411           "supported currently.",
412           rq->type);
413     return -1;
414   }
415
416   if ((rr = c_redisCommand(rn, "SELECT %d", rq->database)) == NULL) {
417     WARNING("redis plugin: unable to switch to database `%d' on node `%s'.",
418             rq->database, rn->name);
419     return -1;
420   }
421
422   if ((rr = c_redisCommand(rn, rq->query)) == NULL) {
423     WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
424     return -1;
425   }
426
427   switch (rr->type) {
428   case REDIS_REPLY_INTEGER:
429     switch (ds->ds[0].type) {
430     case DS_TYPE_COUNTER:
431       val.counter = (counter_t)rr->integer;
432       break;
433     case DS_TYPE_GAUGE:
434       val.gauge = (gauge_t)rr->integer;
435       break;
436     case DS_TYPE_DERIVE:
437       val.gauge = (derive_t)rr->integer;
438       break;
439     case DS_TYPE_ABSOLUTE:
440       val.gauge = (absolute_t)rr->integer;
441       break;
442     }
443     break;
444   case REDIS_REPLY_STRING:
445     if (parse_value(rr->str, &val, ds->ds[0].type) == -1) {
446       WARNING("redis plugin: Query `%s': Unable to parse value.", rq->query);
447       freeReplyObject(rr);
448       return -1;
449     }
450     break;
451   case REDIS_REPLY_ERROR:
452     WARNING("redis plugin: Query `%s' failed: %s.", rq->query, rr->str);
453     freeReplyObject(rr);
454     return -1;
455   case REDIS_REPLY_ARRAY:
456     WARNING("redis plugin: Query `%s' should return string or integer. Arrays "
457             "are not supported.",
458             rq->query);
459     freeReplyObject(rr);
460     return -1;
461   default:
462     WARNING("redis plugin: Query `%s': Cannot coerce redis type (%i).",
463             rq->query, rr->type);
464     freeReplyObject(rr);
465     return -1;
466   }
467
468   redis_submit(rn->name, rq->type,
469                (strlen(rq->instance) > 0) ? rq->instance : NULL, val);
470   freeReplyObject(rr);
471   return 0;
472 } /* }}} int redis_handle_query */
473
474 static int redis_db_stats(const char *node, char const *info_line) /* {{{ */
475 {
476   /* redis_db_stats parses and dispatches Redis database statistics,
477    * currently the number of keys for each database.
478    * info_line needs to have the following format:
479    *   db0:keys=4,expires=0,avg_ttl=0
480    */
481
482   for (int db = 0; db < REDIS_DEF_DB_COUNT; db++) {
483     static char buf[MAX_REDIS_VAL_SIZE];
484     static char field_name[12];
485     static char db_id[4];
486     value_t val;
487     char *str;
488     int i;
489
490     ssnprintf(field_name, sizeof(field_name), "db%d:keys=", db);
491
492     str = strstr(info_line, field_name);
493     if (!str)
494       continue;
495
496     str += strlen(field_name);
497     for (i = 0; (*str && isdigit((int)*str)); i++, str++)
498       buf[i] = *str;
499     buf[i] = '\0';
500
501     if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
502       WARNING("redis plugin: Unable to parse field `%s'.", field_name);
503       return -1;
504     }
505
506     ssnprintf(db_id, sizeof(db_id), "%d", db);
507     redis_submit(node, "records", db_id, val);
508   }
509   return 0;
510
511 } /* }}} int redis_db_stats */
512
513 static void redis_cpu_usage(const char *node, char const *info_line) {
514   while (42) {
515     value_t rusage_user;
516     value_t rusage_syst;
517
518     if (redis_get_info_value(info_line, "used_cpu_user", DS_TYPE_GAUGE,
519                              &rusage_user) != 0)
520       break;
521
522     if (redis_get_info_value(info_line, "used_cpu_sys", DS_TYPE_GAUGE,
523                              &rusage_syst) != 0)
524       break;
525
526     redis_submit2(node, "ps_cputime", "daemon",
527                   (value_t){.derive = rusage_user.gauge * 1000000},
528                   (value_t){.derive = rusage_syst.gauge * 1000000});
529     break;
530   }
531
532   while (42) {
533     value_t rusage_user;
534     value_t rusage_syst;
535
536     if (redis_get_info_value(info_line, "used_cpu_user_children", DS_TYPE_GAUGE,
537                              &rusage_user) != 0)
538       break;
539
540     if (redis_get_info_value(info_line, "used_cpu_sys_children", DS_TYPE_GAUGE,
541                              &rusage_syst) != 0)
542       break;
543
544     redis_submit2(node, "ps_cputime", "children",
545                   (value_t){.derive = rusage_user.gauge * 1000000},
546                   (value_t){.derive = rusage_syst.gauge * 1000000});
547     break;
548   }
549 } /* void redis_cpu_usage */
550
551 static gauge_t calculate_ratio_percent(derive_t part1, derive_t part2,
552                                        derive_t *prev1, derive_t *prev2) {
553   if ((*prev1 == 0) || (*prev2 == 0) || (part1 < *prev1) || (part2 < *prev2)) {
554     *prev1 = part1;
555     *prev2 = part2;
556     return NAN;
557   }
558
559   derive_t num = part1 - *prev1;
560   derive_t denom = part2 - *prev2 + num;
561
562   *prev1 = part1;
563   *prev2 = part2;
564
565   if (denom == 0)
566     return NAN;
567
568   if (num == 0)
569     return 0;
570
571   return 100.0 * (gauge_t)num / (gauge_t)denom;
572 } /* gauge_t calculate_ratio_percent */
573
574 static void redis_keyspace_usage(redis_node_t *rn, char const *info_line) {
575   value_t hits, misses;
576
577   if (redis_get_info_value(info_line, "keyspace_hits", DS_TYPE_DERIVE, &hits) !=
578       0)
579     return;
580
581   if (redis_get_info_value(info_line, "keyspace_misses", DS_TYPE_DERIVE,
582                            &misses) != 0)
583     return;
584
585   redis_submit(rn->name, "cache_result", "hits", hits);
586   redis_submit(rn->name, "cache_result", "misses", misses);
587
588   prev_t *prev = &rn->prev;
589   gauge_t ratio = calculate_ratio_percent(
590       hits.derive, misses.derive, &prev->keyspace_hits, &prev->keyspace_misses);
591   redis_submit(rn->name, "percent", "hitratio", (value_t){.gauge = ratio});
592
593 } /* void redis_keyspace_usage */
594
595 static void redis_check_connection(redis_node_t *rn) {
596   if (rn->redisContext)
597     return;
598
599   redisContext *rh;
600   if (rn->socket != NULL)
601     rh = redisConnectUnixWithTimeout(rn->socket, rn->timeout);
602   else
603     rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
604
605   if (rh == NULL) {
606     ERROR("redis plugin: can't allocate redis context");
607     return;
608   }
609   if (rh->err) {
610     if (rn->socket)
611       ERROR("redis plugin: unable to connect to node `%s' (%s): %s.", rn->name,
612             rn->socket, rh->errstr);
613     else
614       ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.",
615             rn->name, rn->host, rn->port, rh->errstr);
616     redisFree(rh);
617     return;
618   }
619
620   rn->redisContext = rh;
621
622   if (rn->passwd) {
623     redisReply *rr;
624
625     DEBUG("redis plugin: authenticating node `%s' passwd(%s).", rn->name,
626           rn->passwd);
627
628     if ((rr = c_redisCommand(rn, "AUTH %s", rn->passwd)) == NULL) {
629       WARNING("redis plugin: unable to authenticate on node `%s'.", rn->name);
630       return;
631     }
632
633     if (rr->type != REDIS_REPLY_STATUS) {
634       WARNING("redis plugin: invalid authentication on node `%s'.", rn->name);
635       freeReplyObject(rr);
636       redisFree(rn->redisContext);
637       rn->redisContext = NULL;
638       return;
639     }
640
641     freeReplyObject(rr);
642   }
643   return;
644 } /* void redis_check_connection */
645
646 static void redis_read_server_info(redis_node_t *rn) {
647   redisReply *rr;
648
649   if ((rr = c_redisCommand(rn, "INFO")) == NULL) {
650     WARNING("redis plugin: unable to get INFO from node `%s'.", rn->name);
651     return;
652   }
653
654   redis_handle_info(rn->name, rr->str, "uptime", NULL, "uptime_in_seconds",
655                     DS_TYPE_GAUGE);
656   redis_handle_info(rn->name, rr->str, "current_connections", "clients",
657                     "connected_clients", DS_TYPE_GAUGE);
658   redis_handle_info(rn->name, rr->str, "blocked_clients", NULL,
659                     "blocked_clients", DS_TYPE_GAUGE);
660   redis_handle_info(rn->name, rr->str, "memory", NULL, "used_memory",
661                     DS_TYPE_GAUGE);
662   redis_handle_info(rn->name, rr->str, "memory_lua", NULL, "used_memory_lua",
663                     DS_TYPE_GAUGE);
664   /* changes_since_last_save: Deprecated in redis version 2.6 and above */
665   redis_handle_info(rn->name, rr->str, "volatile_changes", NULL,
666                     "changes_since_last_save", DS_TYPE_GAUGE);
667   redis_handle_info(rn->name, rr->str, "total_connections", NULL,
668                     "total_connections_received", DS_TYPE_DERIVE);
669   redis_handle_info(rn->name, rr->str, "total_operations", NULL,
670                     "total_commands_processed", DS_TYPE_DERIVE);
671   redis_handle_info(rn->name, rr->str, "expired_keys", NULL, "expired_keys",
672                     DS_TYPE_DERIVE);
673   redis_handle_info(rn->name, rr->str, "evicted_keys", NULL, "evicted_keys",
674                     DS_TYPE_DERIVE);
675   redis_handle_info(rn->name, rr->str, "pubsub", "channels", "pubsub_channels",
676                     DS_TYPE_GAUGE);
677   redis_handle_info(rn->name, rr->str, "pubsub", "patterns", "pubsub_patterns",
678                     DS_TYPE_GAUGE);
679   redis_handle_info(rn->name, rr->str, "current_connections", "slaves",
680                     "connected_slaves", DS_TYPE_GAUGE);
681   redis_handle_info(rn->name, rr->str, "total_bytes", "input",
682                     "total_net_input_bytes", DS_TYPE_DERIVE);
683   redis_handle_info(rn->name, rr->str, "total_bytes", "output",
684                     "total_net_output_bytes", DS_TYPE_DERIVE);
685
686   redis_keyspace_usage(rn, rr->str);
687
688   redis_db_stats(rn->name, rr->str);
689
690   if (rn->report_cpu_usage)
691     redis_cpu_usage(rn->name, rr->str);
692
693   freeReplyObject(rr);
694 } /* void redis_read_server_info */
695
696 static void redis_read_command_stats(redis_node_t *rn) {
697   redisReply *rr;
698
699   if ((rr = c_redisCommand(rn, "INFO commandstats")) == NULL) {
700     WARNING("redis plugin: node `%s': unable to get `INFO commandstats'.",
701             rn->name);
702     return;
703   }
704
705   if (rr->type != REDIS_REPLY_STRING) {
706     WARNING("redis plugin: node `%s' `INFO commandstats' returned unsupported "
707             "redis type %i.",
708             rn->name, rr->type);
709     freeReplyObject(rr);
710     return;
711   }
712
713   char *command;
714   char *line;
715   char *ptr = rr->str;
716   char *saveptr = NULL;
717   while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) {
718     ptr = NULL;
719
720     if (line[0] == '#')
721       continue;
722
723     /* command name */
724     if (strstr(line, "cmdstat_") != line) {
725       ERROR("redis plugin: not found 'cmdstat_' prefix in line '%s'", line);
726       continue;
727     }
728
729     char *values = strstr(line, ":");
730     if (values == NULL) {
731       ERROR("redis plugin: not found ':' separator in line '%s'", line);
732       continue;
733     }
734
735     /* Null-terminate command token */
736     values[0] = '\0';
737     command = line + strlen("cmdstat_");
738     values++;
739
740     /* parse values */
741     /* cmdstat_publish:calls=20795774,usec=111039258,usec_per_call=5.34 */
742     char *field;
743     char *saveptr_field = NULL;
744     while ((field = strtok_r(values, "=", &saveptr_field)) != NULL) {
745       values = NULL;
746
747       const char *type;
748       /* only these are supported */
749       if (strcmp(field, "calls") == 0)
750         type = "commands";
751       else if (strcmp(field, "usec") == 0)
752         type = "redis_command_cputime";
753       else
754         continue;
755
756       if ((field = strtok_r(NULL, ",", &saveptr_field)) == NULL)
757         continue;
758
759       char *endptr = NULL;
760       errno = 0;
761       derive_t value = strtoll(field, &endptr, 0);
762
763       if ((endptr == field) || (errno != 0))
764         continue;
765
766       redis_submit(rn->name, type, command, (value_t){.derive = value});
767     }
768   }
769   freeReplyObject(rr);
770 } /* void redis_read_command_stats */
771
772 static int redis_read(user_data_t *user_data) /* {{{ */
773 {
774   redis_node_t *rn = user_data->data;
775
776 #if COLLECT_DEBUG
777   if (rn->socket)
778     DEBUG("redis plugin: querying info from node `%s' (%s).", rn->name,
779           rn->socket);
780   else
781     DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
782           rn->host, rn->port);
783 #endif
784
785   redis_check_connection(rn);
786
787   if (!rn->redisContext) /* no connection */
788     return -1;
789
790   redis_read_server_info(rn);
791
792   if (!rn->redisContext) /* connection lost */
793     return -1;
794
795   if (rn->report_command_stats) {
796     redis_read_command_stats(rn);
797
798     if (!rn->redisContext) /* connection lost */
799       return -1;
800   }
801
802   for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next) {
803     redis_handle_query(rn, rq);
804     if (!rn->redisContext) /* connection lost */
805       return -1;
806   }
807
808   return 0;
809 }
810 /* }}} */
811
812 void module_register(void) /* {{{ */
813 {
814   plugin_register_complex_config("redis", redis_config);
815   plugin_register_init("redis", redis_init);
816 }
817 /* }}} */