X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fredis.c;h=ca17e2994a0d8e4fecbca3978c864272b3c0d588;hb=251897b1633940104c95945d3dd29981e43a0cbf;hp=05225b5bfb2e78ee0aab6ab14feb6e9dd3c36148;hpb=47c86ace348a1d7a5352a83d10935209f89aa4f5;p=collectd.git diff --git a/src/redis.c b/src/redis.c index 05225b5b..ca17e299 100644 --- a/src/redis.c +++ b/src/redis.c @@ -35,8 +35,8 @@ #define REDIS_DEF_HOST "localhost" #define REDIS_DEF_PASSWD "" #define REDIS_DEF_PORT 6379 -#define REDIS_DEF_TIMEOUT 2000 -#define REDIS_DEF_DB_COUNT 16 +#define REDIS_DEF_TIMEOUT_SEC 2 +#define REDIS_DEF_DB_COUNT 256 #define MAX_REDIS_NODE_NAME 64 #define MAX_REDIS_PASSWD_LENGTH 512 #define MAX_REDIS_VAL_SIZE 256 @@ -60,6 +60,8 @@ struct redis_query_s { char query[MAX_REDIS_QUERY]; char type[DATA_MAX_NAME_LEN]; char instance[DATA_MAX_NAME_LEN]; + int database; + redis_query_t *next; }; @@ -76,7 +78,7 @@ struct redis_node_s { redis_node_t *next; }; -static redis_node_t *nodes_head = NULL; +static redis_node_t *nodes_head; static int redis_node_add(const redis_node_t *rn) /* {{{ */ { @@ -137,6 +139,8 @@ static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */ (void)sstrncpy(rq->instance, rq->query, sizeof(rq->instance)); replace_special(rq->instance, sizeof(rq->instance)); + rq->database = 0; + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *option = ci->children + i; @@ -145,6 +149,13 @@ static redis_query_t *redis_config_query(oconfig_item_t *ci) /* {{{ */ } else if (strcasecmp("Instance", option->key) == 0) { status = cf_util_get_string_buffer(option, rq->instance, sizeof(rq->instance)); + } else if (strcasecmp("Database", option->key) == 0) { + status = cf_util_get_int(option, &rq->database); + if (rq->database < 0) { + WARNING("redis plugin: The \"Database\" option must be positive " + "integer or zero"); + status = -1; + } } else { WARNING("redis plugin: unknown configuration option: %s", option->key); status = -1; @@ -165,7 +176,7 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */ int timeout; redis_node_t rn = {.port = REDIS_DEF_PORT, - .timeout.tv_usec = REDIS_DEF_TIMEOUT}; + .timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC}; sstrncpy(rn.host, REDIS_DEF_HOST, sizeof(rn.host)); @@ -194,8 +205,11 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */ } } else if (strcasecmp("Timeout", option->key) == 0) { status = cf_util_get_int(option, &timeout); - if (status == 0) - rn.timeout.tv_usec = timeout; + if (status == 0) { + rn.timeout.tv_usec = timeout * 1000; + rn.timeout.tv_sec = rn.timeout.tv_usec / 1000000L; + rn.timeout.tv_usec %= 1000000L; + } } else if (strcasecmp("Password", option->key) == 0) status = cf_util_get_string_buffer(option, rn.passwd, sizeof(rn.passwd)); else @@ -257,8 +271,7 @@ static int redis_init(void) /* {{{ */ redis_node_t rn = {.name = "default", .host = REDIS_DEF_HOST, .port = REDIS_DEF_PORT, - .timeout.tv_sec = 0, - .timeout.tv_usec = REDIS_DEF_TIMEOUT, + .timeout.tv_sec = REDIS_DEF_TIMEOUT_SEC, .next = NULL}; if (nodes_head == NULL) @@ -304,12 +317,20 @@ static int redis_handle_query(redisContext *rh, redis_node_t *rn, ds = plugin_get_ds(rq->type); if (!ds) { - ERROR("redis plugin: DataSet `%s' not defined.", rq->type); + ERROR("redis plugin: DS type `%s' not defined.", rq->type); return -1; } if (ds->ds_num != 1) { - ERROR("redis plugin: DS `%s' has too many types.", rq->type); + ERROR("redis plugin: DS type `%s' has too many datasources. This is not " + "supported currently.", + rq->type); + return -1; + } + + if ((rr = redisCommand(rh, "SELECT %d", rq->database)) == NULL) { + WARNING("redis plugin: unable to switch to database `%d' on node `%s'.", + rq->database, rn->name); return -1; } @@ -337,13 +358,24 @@ static int redis_handle_query(redisContext *rh, redis_node_t *rn, break; case REDIS_REPLY_STRING: if (parse_value(rr->str, &val, ds->ds[0].type) == -1) { - WARNING("redis plugin: Unable to parse field `%s'.", rq->type); + WARNING("redis plugin: Query `%s': Unable to parse value.", rq->query); freeReplyObject(rr); return -1; } break; + case REDIS_REPLY_ERROR: + WARNING("redis plugin: Query `%s' failed: %s.", rq->query, rr->str); + freeReplyObject(rr); + return -1; + case REDIS_REPLY_ARRAY: + WARNING("redis plugin: Query `%s' should return string or integer. Arrays " + "are not supported.", + rq->query); + freeReplyObject(rr); + return -1; default: - WARNING("redis plugin: Cannot coerce redis type."); + WARNING("redis plugin: Query `%s': Cannot coerce redis type (%i).", + rq->query, rr->type); freeReplyObject(rr); return -1; } @@ -364,13 +396,13 @@ static int redis_db_stats(char *node, char const *info_line) /* {{{ */ 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]; + static char field_name[12]; + static char db_id[4]; value_t val; char *str; int i; - ssnprintf(field_name, sizeof(field_name), "db%d:keys=", db); + snprintf(field_name, sizeof(field_name), "db%d:keys=", db); str = strstr(info_line, field_name); if (!str) @@ -386,7 +418,7 @@ static int redis_db_stats(char *node, char const *info_line) /* {{{ */ return -1; } - ssnprintf(db_id, sizeof(db_id), "%d", db); + snprintf(db_id, sizeof(db_id), "%d", db); redis_submit(node, "records", db_id, val); } return 0; @@ -404,8 +436,13 @@ static int redis_read(void) /* {{{ */ rh = redisConnectWithTimeout((char *)rn->host, rn->port, rn->timeout); if (rh == NULL) { - ERROR("redis plugin: unable to connect to node `%s' (%s:%d).", rn->name, - rn->host, rn->port); + ERROR("redis plugin: can't allocate redis context"); + continue; + } + if (rh->err) { + ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", + rn->name, rn->host, rn->port, rh->errstr); + redisFree(rh); continue; }