]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util-logopenfile: introduce SCConfLogOpenRedis
authorEric Leblond <eric@regit.org>
Sun, 24 May 2015 16:07:20 +0000 (18:07 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 22 Oct 2015 08:01:05 +0000 (10:01 +0200)
Introduce a function to realize the parsing and config file and
opening of connection to the database. Only used by output-json
for now it will be usable by other logging modules.

src/output-json.c
src/util-logopenfile.c
src/util-logopenfile.h

index 4ac09b30098c75485ea3325709fbdcba7dea80e9..99032a5481cb736179b3ca1ee71ac770026273cb 100644 (file)
@@ -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
 
index 05dad32b08735c1cb2f1f564241f0acfe0d957e3..519e4f7735fb2077924d2d7e5a72b6a5214a5c5f 100644 (file)
@@ -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
  *  */
index 0c6c392bf2237183bed02aa96868c6141b7da3a6..82ea4eefdd35605b62e6df81a231eb860d817303 100644 (file)
@@ -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__ */