X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_redis.c;h=135a458785ba55da955ea5c3c206a1467c3f4269;hb=4ad72002cbcabc020226ea1e47a7403872ce4954;hp=5560b4d1fcfcc72fbaf01ec7947bb81314d991c9;hpb=5bf93412e903fb36943088e711031a013495ce11;p=collectd.git diff --git a/src/write_redis.c b/src/write_redis.c index 5560b4d1..135a4587 100644 --- a/src/write_redis.c +++ b/src/write_redis.c @@ -25,11 +25,11 @@ **/ #include "collectd.h" + #include "plugin.h" #include "common.h" #include "configfile.h" -#include #include #include @@ -46,6 +46,8 @@ struct wr_node_s struct timeval timeout; char *prefix; int database; + int max_set_size; + _Bool store_rates; redisContext *conn; pthread_mutex_t lock; @@ -62,7 +64,7 @@ static int wr_write (const data_set_t *ds, /* {{{ */ wr_node_t *node = ud->data; char ident[512]; char key[512]; - char value[512]; + char value[512] = { 0 }; char time[24]; size_t value_size; char *value_ptr; @@ -77,14 +79,14 @@ static int wr_write (const data_set_t *ds, /* {{{ */ ident); ssnprintf (time, sizeof (time), "%.9f", CDTIME_T_TO_DOUBLE(vl->time)); - memset (value, 0, sizeof (value)); value_size = sizeof (value); value_ptr = &value[0]; - status = format_values (value_ptr, value_size, ds, vl, /* store rates = */ 0); - pthread_mutex_lock (&node->lock); + status = format_values (value_ptr, value_size, ds, vl, node->store_rates); if (status != 0) return (status); + pthread_mutex_lock (&node->lock); + if (node->conn == NULL) { node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout); @@ -105,7 +107,7 @@ static int wr_write (const data_set_t *ds, /* {{{ */ pthread_mutex_unlock (&node->lock); return (-1); } - + rr = redisCommand(node->conn, "SELECT %d", node->database); if (rr == NULL) WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr); @@ -119,6 +121,15 @@ static int wr_write (const data_set_t *ds, /* {{{ */ else freeReplyObject (rr); + if (node->max_set_size >= 0) + { + rr = redisCommand (node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0, (-1 * node->max_set_size) - 1); + if (rr == NULL) + WARNING("ZREMRANGEBYRANK command error. key:%s message:%s", key, node->conn->errstr); + else + freeReplyObject (rr); + } + /* TODO(octo): This is more overhead than necessary. Use the cache and * metadata to determine if it is a new metric and call SADD only once for * each metric. */ @@ -157,12 +168,10 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ wr_node_t *node; int timeout; int status; - int i; - node = malloc (sizeof (*node)); + node = calloc (1, sizeof (*node)); if (node == NULL) return (ENOMEM); - memset (node, 0, sizeof (*node)); node->host = NULL; node->port = 0; node->timeout.tv_sec = 0; @@ -170,6 +179,8 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ node->conn = NULL; node->prefix = NULL; node->database = 0; + node->max_set_size = -1; + node->store_rates = 1; pthread_mutex_init (&node->lock, /* attr = */ NULL); status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name)); @@ -179,7 +190,7 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ return (status); } - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -204,6 +215,12 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ else if (strcasecmp ("Database", child->key) == 0) { status = cf_util_get_int (child, &node->database); } + else if (strcasecmp ("MaxSetSize", child->key) == 0) { + status = cf_util_get_int (child, &node->max_set_size); + } + else if (strcasecmp ("StoreRates", child->key) == 0) { + status = cf_util_get_boolean (child, &node->store_rates); + } else WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".", child->key); @@ -233,9 +250,7 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ static int wr_config (oconfig_item_t *ci) /* {{{ */ { - int i; - - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i;