diff --git a/rct_core.c b/rct_core.c index 84810c8..423db1a 100644 --- a/rct_core.c +++ b/rct_core.c @@ -8,11 +8,11 @@ #define RCT_AE_CONTINUE 0 #define RCT_AE_STOP 1 -unsigned int dictSdsHash(const void *key) { +static unsigned int dictSdsHash(const void *key) { return dictGenHashFunction((unsigned char*)key, sdslen((char*)key)); } -int dictSdsKeyCompare(void *privdata, const void *key1, +static int dictSdsKeyCompare(void *privdata, const void *key1, const void *key2) { int l1,l2; @@ -24,7 +24,7 @@ int dictSdsKeyCompare(void *privdata, const void *key1, return memcmp(key1, key2, l1) == 0; } -void dictSdsDestructor(void *privdata, void *val) +static void dictSdsDestructor(void *privdata, void *val) { DICT_NOTUSED(privdata); @@ -64,6 +64,7 @@ create_context(struct instance *nci) rct_ctx->cc = NULL; rct_ctx->address = NULL; + rct_ctx->auth = NULL; commands = dictCreate(&commandTableDictType,NULL); if(commands == NULL) @@ -78,6 +79,9 @@ create_context(struct instance *nci) if (nci->addr) rct_ctx->address = sdsnew(nci->addr); + if (nci->auth) + rct_ctx->auth = sdsnew(nci->auth); + cmd_parts = sdssplitargs(nci->command, &cmd_parts_count); if(cmd_parts == NULL || cmd_parts_count <= 0) { @@ -1957,7 +1961,7 @@ int async_command_init(async_command *acmd, rctContext *ctx, char *addrs, int fl acmd->ctx = ctx; - acmd->acc = redisClusterAsyncConnect(addrs, HIRCLUSTER_FLAG_ADD_SLAVE); + acmd->acc = redisClusterAsyncConnect(addrs, ctx->auth, HIRCLUSTER_FLAG_ADD_SLAVE); if(acmd->acc == NULL){ log_error("Connect to %s failed.", addrs); goto error; @@ -2169,7 +2173,7 @@ static int do_command_one_node_async(async_command *acmd, struct cluster_node *n char **argv; size_t *argvlen; sds *str; - + if(acmd == NULL || node == NULL){ return RCT_ERROR; } @@ -5945,7 +5949,10 @@ int core_core(rctContext *ctx) flags = HIRCLUSTER_FLAG_ADD_SLAVE; } - cc = redisClusterConnect(ctx->address, flags); + if(ctx->auth) + cc = redisClusterConnectWithAuth(ctx->address, ctx->auth, flags); + else + cc = redisClusterConnect(ctx->address, flags); //cc = redisClusterConnectAllWithTimeout(addr, timeout, flags); if(cc == NULL || cc->err) { diff --git a/rct_core.h b/rct_core.h index 64f5cd3..fa42642 100644 --- a/rct_core.h +++ b/rct_core.h @@ -89,6 +89,7 @@ struct instance { char *conf_filename; /* configuration filename */ int interval; /* stats aggregation interval */ char *addr; /* stats monitoring addr */ + char *auth; /* auth */ char hostname[RCT_MAXHOSTNAMELEN]; /* hostname */ pid_t pid; /* process id */ char *pid_filename; /* pid filename */ @@ -111,6 +112,7 @@ typedef struct rctContext { redisClusterContext *cc; dict *commands; /* Command table */ sds address; + sds auth; char *cmd; uint8_t redis_role; uint8_t simple; diff --git a/rct_option.c b/rct_option.c index 186a16e..f486a98 100644 --- a/rct_option.c +++ b/rct_option.c @@ -19,6 +19,7 @@ #define RCT_OPTION_BUFFER_DEFAULT 1024*1024 +#define RCT_AUTH NULL static struct option long_options[] = { { "help", no_argument, NULL, 'h' }, @@ -36,10 +37,11 @@ static struct option long_options[] = { { "thread", required_argument, NULL, 't' }, { "buffer", required_argument, NULL, 'b' }, { "step", required_argument, NULL, 'S' }, + { "auth", required_argument, NULL, 'A' }, { NULL, 0, NULL, 0 } }; -static char short_options[] = "hVdo:v:c:a:i:p:C:r:st:b:S:"; +static char short_options[] = "hVdo:v:c:a:i:p:C:r:st:b:S:A:"; void rct_show_usage(void) @@ -48,7 +50,7 @@ rct_show_usage(void) "Usage: redis-cluster-tool [-?hVds] [-v verbosity level] [-o output file]" CRLF " [-c conf file] [-a addr] [-i interval]" CRLF " [-p pid file] [-C command] [-r redis role]" CRLF - " [-t thread number] [-b buffer size]" CRLF + " [-t thread number] [-b buffer size] [-A auth]" CRLF ""); log_stderr( "Options:" CRLF @@ -67,6 +69,7 @@ rct_show_usage(void) " -r, --role=S : set the role of the nodes that command to execute on (default: %s, you can input: %s, %s or %s)" CRLF " -t, --thread=N : set how many threads to run the job(default: %d)" CRLF " -b, --buffer=S : set buffer size to run the job (default: %lld byte, unit:G/M/K)" CRLF + " -A, --auth=S : set redis cluster auth (default: %s)" CRLF "", RCT_LOG_DEFAULT, RCT_LOG_MIN, RCT_LOG_MAX, RCT_LOG_PATH != NULL ? RCT_LOG_PATH : "stderr", @@ -80,7 +83,8 @@ rct_show_usage(void) RCT_OPTION_REDIS_ROLE_MASTER, RCT_OPTION_REDIS_ROLE_SLAVE, RCT_OPTION_THREAD_COUNT_DEFAULT, - RCT_OPTION_BUFFER_DEFAULT); + RCT_OPTION_BUFFER_DEFAULT, + RCT_AUTH); rct_show_command_usage(); } @@ -195,7 +199,11 @@ rct_get_options(int argc, char **argv, struct instance *nci) case 'C': nci->command = optarg; break; - + + case 'A': + nci->auth = optarg; + break; + case 'r': nci->role = optarg; if(strcmp(nci->role, RCT_OPTION_REDIS_ROLE_ALL) != 0 @@ -254,6 +262,7 @@ rct_get_options(int argc, char **argv, struct instance *nci) case 'C': case 'r': case 'b': + case 'A': log_stderr("redis-cluster-tool: option -%c requires a string", optopt); break; diff --git a/rct_util.c b/rct_util.c index 9e911c3..f7f7be9 100644 --- a/rct_util.c +++ b/rct_util.c @@ -473,7 +473,7 @@ rct_assert(const char *cond, const char *file, int line, int panic) } } -int +static int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { int n; @@ -501,7 +501,7 @@ _vscnprintf(char *buf, size_t size, const char *fmt, va_list args) return (int)(size - 1); } -int +static int _scnprintf(char *buf, size_t size, const char *fmt, ...) { va_list args; diff --git a/rct_util.h b/rct_util.h index dec5e73..c069f70 100644 --- a/rct_util.h +++ b/rct_util.h @@ -186,8 +186,8 @@ void rct_assert(const char *cond, const char *file, int line, int panic); void rct_stacktrace(int skip_count); void rct_stacktrace_fd(int fd); -int _scnprintf(char *buf, size_t size, const char *fmt, ...); -int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args); +static int _scnprintf(char *buf, size_t size, const char *fmt, ...); +static int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args); int64_t rct_usec_now(void); int64_t rct_msec_now(void);