redis: fix parser and comments
[collectd.git] / src / redis.c
index 5b77484..40a311d 100644 (file)
@@ -36,6 +36,7 @@
 #define REDIS_DEF_PASSWD ""
 #define REDIS_DEF_PORT 6379
 #define REDIS_DEF_TIMEOUT 2000
+#define REDIS_DEF_DB_COUNT 16
 #define MAX_REDIS_NODE_NAME 64
 #define MAX_REDIS_PASSWD_LENGTH 512
 #define MAX_REDIS_VAL_SIZE 256
@@ -237,14 +238,10 @@ __attribute__((nonnull(2))) static void
 redis_submit(char *plugin_instance, const char *type, const char *type_instance,
              value_t value) /* {{{ */
 {
-  value_t values[1];
   value_list_t vl = VALUE_LIST_INIT;
 
-  values[0] = value;
-
-  vl.values = values;
+  vl.values = &value;
   vl.values_len = 1;
-  sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin, "redis", sizeof(vl.plugin));
   if (plugin_instance != NULL)
     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
@@ -357,6 +354,45 @@ static int redis_handle_query(redisContext *rh, redis_node_t *rn,
   return 0;
 } /* }}} int redis_handle_query */
 
+static int redis_db_stats(char *node, char const *info_line) /* {{{ */
+{
+  /* redis_db_stats parses and dispatches Redis database statistics,
+   * currently the number of keys for each database.
+   * info_line needs to have the following format:
+   *   db0:keys=4,expires=0,avg_ttl=0
+   */
+
+  for (int db = 0; db < REDIS_DEF_DB_COUNT; db++) {
+    static char buf[MAX_REDIS_VAL_SIZE];
+    static char field_name[11];
+    static char db_id[3];
+    value_t val;
+    char *str;
+    int i;
+
+    ssnprintf(field_name, sizeof(field_name), "db%d:keys=", db);
+
+    str = strstr(info_line, field_name);
+    if (!str)
+      continue;
+
+    str += strlen(field_name);
+    for (i = 0; (*str && isdigit((int)*str)); i++, str++)
+      buf[i] = *str;
+    buf[i] = '\0';
+
+    if (parse_value(buf, &val, DS_TYPE_GAUGE) != 0) {
+      WARNING("redis plugin: Unable to parse field `%s'.", field_name);
+      return (-1);
+    }
+
+    ssnprintf(db_id, sizeof(db_id), "%d", db);
+    redis_submit(node, "records", db_id, val);
+  }
+  return (0);
+
+} /* }}} int redis_db_stats */
+
 static int redis_read(void) /* {{{ */
 {
   for (redis_node_t *rn = nodes_head; rn != NULL; rn = rn->next) {
@@ -433,6 +469,8 @@ static int redis_read(void) /* {{{ */
     redis_handle_info(rn->name, rr->str, "total_bytes", "output",
                       "total_net_output_bytes", DS_TYPE_DERIVE);
 
+    redis_db_stats(rn->name, rr->str);
+
     for (redis_query_t *rq = rn->queries; rq != NULL; rq = rq->next)
       redis_handle_query(rh, rn, rq);
 
@@ -455,5 +493,3 @@ void module_register(void) /* {{{ */
    * X elements */
 }
 /* }}} */
-
-/* vim: set sw=2 sts=2 et fdm=marker : */