From 3571a648ae0e2620ba9fcfec6033a6d604219824 Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Thu, 23 Aug 2018 14:35:19 +0700 Subject: [PATCH] redis plugin: Added option for connecting via UNIX socket --- src/collectd.conf.in | 1 + src/collectd.conf.pod | 6 ++++++ src/redis.c | 28 +++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index fb1471f1..af652145 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1276,6 +1276,7 @@ # # Host "redis.example.com" # Port "6379" +# #Socket "/var/run/redis/redis.sock" # Timeout 2000 # # #Database 0 diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 1134541a..6e6d6eaf 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7359,6 +7359,7 @@ parameters and set of user-defined queries for this node. Host "localhost" Port "6379" + #Socket "/var/run/redis/redis.sock" Timeout 2000 ReportCommandStats false ReportCpuUsage true @@ -7392,6 +7393,11 @@ The B option is the TCP port on which the Redis instance accepts connections. Either a service name of a port number may be given. Please note that numerical port numbers must be given as a string, too. +=item B I + +Connect to Redis using the UNIX domain socket at I. If this +setting is given, the B and B settings are ignored. + =item B I Use I to authenticate when connecting to I. diff --git a/src/redis.c b/src/redis.c index dd4deb17..e24abd54 100644 --- a/src/redis.c +++ b/src/redis.c @@ -70,6 +70,7 @@ typedef struct redis_node_s redis_node_t; struct redis_node_s { char *name; char *host; + char *socket; char *passwd; int port; struct timeval timeout; @@ -101,6 +102,7 @@ static void redis_node_free(void *arg) { redisFree(rn->redisContext); sfree(rn->name); sfree(rn->host); + sfree(rn->socket); sfree(rn->passwd); sfree(rn); } /* void redis_node_free */ @@ -213,6 +215,8 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */ rn->port = status; status = 0; } + } else if (strcasecmp("Socket", option->key) == 0) { + status = cf_util_get_string(option, &rn->socket); } else if (strcasecmp("Query", option->key) == 0) { redis_query_t *rq = redis_config_query(option); if (rq == NULL) { @@ -591,15 +595,23 @@ static void redis_check_connection(redis_node_t *rn) { if (rn->redisContext) return; - redisContext *rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout); + redisContext *rh; + if (rn->socket != NULL) + rh = redisConnectUnixWithTimeout(rn->socket, rn->timeout); + else + rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout); if (rh == NULL) { ERROR("redis plugin: can't allocate redis context"); return; } if (rh->err) { - ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", rn->name, - rn->host, rn->port, rh->errstr); + if (rn->socket) + ERROR("redis plugin: unable to connect to node `%s' (%s): %s.", rn->name, + rn->socket, rh->errstr); + else + ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", + rn->name, rn->host, rn->port, rh->errstr); redisFree(rh); return; } @@ -760,8 +772,14 @@ static int redis_read(user_data_t *user_data) /* {{{ */ { redis_node_t *rn = user_data->data; - DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name, - rn->host, rn->port); +#if COLLECT_DEBUG + if (rn->socket) + DEBUG("redis plugin: querying info from node `%s' (%s).", rn->name, + rn->socket); + else + DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name, + rn->host, rn->port); +#endif redis_check_connection(rn); -- 2.11.0