From 8491c84fd4ed9d1d76318278da01181cfa26c1bd Mon Sep 17 00:00:00 2001 From: skob Date: Mon, 28 May 2018 14:47:48 +0300 Subject: [PATCH] redis plugin: [new feature] ability to select database for query Closes: #2790 --- src/collectd.conf.in | 5 +++++ src/collectd.conf.pod | 6 ++++++ src/redis.c | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 662b4835..715a1864 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1264,6 +1264,11 @@ # Host "redis.example.com" # Port "6379" # Timeout 2000 +# +# Database 0 +# Type "queue_length" +# Instance "myqueue" +# # # diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 7a21ba2c..c097a488 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7276,6 +7276,7 @@ which configures the connection parameters for this node. Port "6379" Timeout 2000 + Database 0 Type "queue_length" Instance "myqueue" @@ -7321,6 +7322,11 @@ than B defined globally. The B block identifies a query to execute against the redis server. There may be an arbitrary number of queries to execute. +=item B I + +This index selects the redis database to use for queries. Defaults +to C<0>. + =item B I Within a query definition, a valid collectd type to use as when submitting diff --git a/src/redis.c b/src/redis.c index 2d33e2dc..47fa28d0 100644 --- a/src/redis.c +++ b/src/redis.c @@ -36,7 +36,7 @@ #define REDIS_DEF_PASSWD "" #define REDIS_DEF_PORT 6379 #define REDIS_DEF_TIMEOUT 2000 -#define REDIS_DEF_DB_COUNT 16 +#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; }; @@ -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; @@ -313,6 +324,12 @@ static int redis_handle_query(redisContext *rh, redis_node_t *rn, 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; + } + if ((rr = redisCommand(rh, rq->query)) == NULL) { WARNING("redis plugin: unable to carry out query `%s'.", rq->query); return -1; -- 2.11.0