projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
redis plugin: [new feature] ability to select database for query
[collectd.git]
/
src
/
redis.c
diff --git
a/src/redis.c
b/src/redis.c
index
05225b5
..
47fa28d
100644
(file)
--- 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_PASSWD ""
#define REDIS_DEF_PORT 6379
#define REDIS_DEF_TIMEOUT 2000
-#define REDIS_DEF_DB_COUNT
1
6
+#define REDIS_DEF_DB_COUNT
25
6
#define MAX_REDIS_NODE_NAME 64
#define MAX_REDIS_PASSWD_LENGTH 512
#define MAX_REDIS_VAL_SIZE 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];
char query[MAX_REDIS_QUERY];
char type[DATA_MAX_NAME_LEN];
char instance[DATA_MAX_NAME_LEN];
+ int database;
+
redis_query_t *next;
};
redis_query_t *next;
};
@@
-76,7
+78,7
@@
struct redis_node_s {
redis_node_t *next;
};
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) /* {{{ */
{
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));
(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;
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("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;
} 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;
}
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;
if ((rr = redisCommand(rh, rq->query)) == NULL) {
WARNING("redis plugin: unable to carry out query `%s'.", rq->query);
return -1;
@@
-370,7
+387,7
@@
static int redis_db_stats(char *node, char const *info_line) /* {{{ */
char *str;
int i;
char *str;
int i;
- s
s
nprintf(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)
str = strstr(info_line, field_name);
if (!str)
@@
-386,7
+403,7
@@
static int redis_db_stats(char *node, char const *info_line) /* {{{ */
return -1;
}
return -1;
}
- s
s
nprintf(db_id, sizeof(db_id), "%d", db);
+ snprintf(db_id, sizeof(db_id), "%d", db);
redis_submit(node, "records", db_id, val);
}
return 0;
redis_submit(node, "records", db_id, val);
}
return 0;