diff --git a/src/output-json.c b/src/output-json.c index 4ac09b3009..99032a5481 100644 --- a/src/output-json.c +++ b/src/output-json.c @@ -540,11 +540,6 @@ OutputCtx *OutputJsonInitCtx(ConfNode *conf) else if (json_ctx->json_out == LOGFILE_TYPE_REDIS) { ConfNode *redis_node = ConfNodeLookupChild(conf, "redis"); const char *sensor_name = ConfNodeLookupChildValue(conf, "sensor-name"); - const char *redis_server = NULL; - const char *redis_port = NULL; - const char *redis_mode = NULL; - const char *redis_key = NULL; - if (!sensor_name) { char hostname[1024]; gethostname(hostname, 1023); @@ -552,49 +547,12 @@ OutputCtx *OutputJsonInitCtx(ConfNode *conf) } json_ctx->file_ctx->redis_setup.sensor_name = SCStrdup(sensor_name); - - if (redis_node) { - redis_server = ConfNodeLookupChildValue(redis_node, "server"); - redis_port = ConfNodeLookupChildValue(redis_node, "port"); - redis_mode = ConfNodeLookupChildValue(redis_node, "mode"); - redis_key = ConfNodeLookupChildValue(redis_node, "key"); - } - if (!redis_server) { - redis_server = "127.0.0.1"; - SCLogInfo("Using default redis server (127.0.0.1)"); - } - if (!redis_port) - redis_port = "6379"; - if (!redis_mode) - redis_mode = "list"; - if (!redis_key) - redis_key = "suricata"; - json_ctx->file_ctx->redis_setup.key = SCStrdup(redis_key); - - if (!json_ctx->file_ctx->redis_setup.key) { - SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key name"); - exit(EXIT_FAILURE); - } - - if (!strcmp(redis_mode, "list")) { - json_ctx->file_ctx->redis_setup.command = SCStrdup("LPUSH"); - if (!json_ctx->file_ctx->redis_setup.command) { - SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key command"); - exit(EXIT_FAILURE); - } - } else { - json_ctx->file_ctx->redis_setup.command = SCStrdup("PUBLISH"); - if (!json_ctx->file_ctx->redis_setup.command) { - SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key command"); - exit(EXIT_FAILURE); - } - } - redisContext *c = redisConnect(redis_server, atoi(redis_port)); - if (c != NULL && c->err) { - SCLogError(SC_ERR_SOCKET, "Error connecting to redis server: %s\n", c->errstr); - exit(EXIT_FAILURE); + if (SCConfLogOpenRedis(redis_node, json_ctx->file_ctx) < 0) { + LogFileFreeCtx(json_ctx->file_ctx); + SCFree(json_ctx); + SCFree(output_ctx); + return NULL; } - json_ctx->file_ctx->redis = c; } #endif diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index 05dad32b08..519e4f7735 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -330,6 +330,62 @@ int SCConfLogReopen(LogFileCtx *log_ctx) return 0; } + +#if HAVE_LIBHIREDIS +int SCConfLogOpenRedis(ConfNode *redis_node, LogFileCtx *log_ctx) +{ + const char *redis_server = NULL; + const char *redis_port = NULL; + const char *redis_mode = NULL; + const char *redis_key = NULL; + + if (redis_node) { + redis_server = ConfNodeLookupChildValue(redis_node, "server"); + redis_port = ConfNodeLookupChildValue(redis_node, "port"); + redis_mode = ConfNodeLookupChildValue(redis_node, "mode"); + redis_key = ConfNodeLookupChildValue(redis_node, "key"); + } + if (!redis_server) { + redis_server = "127.0.0.1"; + SCLogInfo("Using default redis server (127.0.0.1)"); + } + if (!redis_port) + redis_port = "6379"; + if (!redis_mode) + redis_mode = "list"; + if (!redis_key) + redis_key = "suricata"; + log_ctx->redis_setup.key = SCStrdup(redis_key); + + if (!log_ctx->redis_setup.key) { + SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key name"); + exit(EXIT_FAILURE); + } + + if (!strcmp(redis_mode, "list")) { + log_ctx->redis_setup.command = SCStrdup("LPUSH"); + if (!log_ctx->redis_setup.command) { + SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key command"); + exit(EXIT_FAILURE); + } + } else { + log_ctx->redis_setup.command = SCStrdup("PUBLISH"); + if (!log_ctx->redis_setup.command) { + SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate redis key command"); + exit(EXIT_FAILURE); + } + } + redisContext *c = redisConnect(redis_server, atoi(redis_port)); + if (c != NULL && c->err) { + SCLogError(SC_ERR_SOCKET, "Error connecting to redis server: %s", c->errstr); + exit(EXIT_FAILURE); + } + log_ctx->redis = c; + + return 0; +} +#endif + /** \brief LogFileNewCtx() Get a new LogFileCtx * \retval LogFileCtx * pointer if succesful, NULL if error * */ diff --git a/src/util-logopenfile.h b/src/util-logopenfile.h index 0c6c392bf2..82ea4eefdd 100644 --- a/src/util-logopenfile.h +++ b/src/util-logopenfile.h @@ -126,6 +126,7 @@ int LogFileFreeCtx(LogFileCtx *); int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer, char *string, size_t string_len); int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *, const char *, int); +int SCConfLogOpenRedis(ConfNode *conf, LogFileCtx *log_ctx); int SCConfLogReopen(LogFileCtx *); #endif /* __UTIL_LOGOPENFILE_H__ */