From: Eric Leblond Date: Sun, 24 May 2015 15:43:51 +0000 (+0200) Subject: util-logopenfile: add write function X-Git-Tag: suricata-3.0RC1~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a13be67b5e6c26c53149b88f9565c58f7fbe3381;p=thirdparty%2Fsuricata.git util-logopenfile: add write function Introduce a function LogFileWrite that will handle the writing with respect of the type defined in the configuration. This is used in this patch to remove the write complexity from output-json. --- diff --git a/src/output-json.c b/src/output-json.c index d45bbb1233..4ac09b3009 100644 --- a/src/output-json.c +++ b/src/output-json.c @@ -119,10 +119,6 @@ void OutputJsonRegisterTests (void) #define OUTPUT_BUFFER_SIZE 65535 -#ifndef OS_WIN32 -static int alert_syslog_level = DEFAULT_ALERT_SYSLOG_LEVEL; -#endif /* OS_WIN32 */ - TmEcode OutputJson (ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *); TmEcode OutputJsonThreadInit(ThreadVars *, void *, void **); TmEcode OutputJsonThreadDeinit(ThreadVars *, void *); @@ -361,56 +357,8 @@ int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer *buffer) if (unlikely(js_s == NULL)) return TM_ECODE_OK; - SCMutexLock(&file_ctx->fp_mutex); - if (file_ctx->type == LOGFILE_TYPE_SYSLOG) - { - if (file_ctx->prefix != NULL) - { - syslog(alert_syslog_level, "%s%s", file_ctx->prefix, js_s); - } - else - { - syslog(alert_syslog_level, "%s", js_s); - } - } - else if (file_ctx->type == LOGFILE_TYPE_FILE || - file_ctx->type == LOGFILE_TYPE_UNIX_DGRAM || - file_ctx->type == LOGFILE_TYPE_UNIX_STREAM) - { - if (file_ctx->prefix != NULL) - { - MemBufferWriteString(buffer, "%s%s\n", file_ctx->prefix, js_s); - } - else - { - MemBufferWriteString(buffer, "%s\n", js_s); - } - file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer), - MEMBUFFER_OFFSET(buffer), file_ctx); - } -#if HAVE_LIBHIREDIS - else if (file_ctx->type == LOGFILE_TYPE_REDIS) { - /* FIXME go async here */ - redisReply *reply = redisCommand(file_ctx->redis, "%s %s %s", - file_ctx->redis_setup.command, - file_ctx->redis_setup.key, - js_s); - switch (reply->type) { - case REDIS_REPLY_ERROR: - SCLogWarning(SC_WARN_NO_UNITTESTS, "Redis error: %s", reply->str); - break; - case REDIS_REPLY_INTEGER: - SCLogDebug("Redis integer %lld", reply->integer); - break; - default: - SCLogError(SC_ERR_INVALID_VALUE, - "Redis default triggered with %d", reply->type); - break; - } - freeReplyObject(reply); - } -#endif - SCMutexUnlock(&file_ctx->fp_mutex); + LogFileWrite(file_ctx, buffer, js_s, strlen(js_s)); + free(js_s); return 0; } @@ -577,7 +525,7 @@ OutputCtx *OutputJsonInitCtx(ConfNode *conf) if (level_s != NULL) { int level = SCMapEnumNameToValue(level_s, SCSyslogGetLogLevelMap()); if (level != -1) { - alert_syslog_level = level; + json_ctx->file_ctx->syslog_setup.alert_syslog_level = level; } } diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index b22988ae84..05dad32b08 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -387,3 +387,43 @@ int LogFileFreeCtx(LogFileCtx *lf_ctx) SCReturnInt(1); } + +int LogFileWrite(LogFileCtx *file_ctx, MemBuffer *buffer, char *string, size_t string_len) +{ + SCMutexLock(&file_ctx->fp_mutex); + if (file_ctx->type == LOGFILE_TYPE_SYSLOG) { + syslog(file_ctx->syslog_setup.alert_syslog_level, "%s", string); + } else if (file_ctx->type == LOGFILE_TYPE_FILE || + file_ctx->type == LOGFILE_TYPE_UNIX_DGRAM || + file_ctx->type == LOGFILE_TYPE_UNIX_STREAM) + { + MemBufferWriteString(buffer, "%s\n", string); + file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer), + MEMBUFFER_OFFSET(buffer), file_ctx); + } +#if HAVE_LIBHIREDIS + else if (file_ctx->type == LOGFILE_TYPE_REDIS) { + /* FIXME go async here */ + redisReply *reply = redisCommand(file_ctx->redis, "%s %s %s", + file_ctx->redis_setup.command, + file_ctx->redis_setup.key, + string); + switch (reply->type) { + case REDIS_REPLY_ERROR: + SCLogWarning(SC_WARN_NO_UNITTESTS, "Redis error: %s", reply->str); + break; + case REDIS_REPLY_INTEGER: + SCLogDebug("Redis integer %lld", reply->integer); + break; + default: + SCLogError(SC_ERR_INVALID_VALUE, + "Redis default triggered with %d", reply->type); + break; + } + freeReplyObject(reply); + } +#endif + SCMutexUnlock(&file_ctx->fp_mutex); + + return 0; +} diff --git a/src/util-logopenfile.h b/src/util-logopenfile.h index 775647a6b3..0c6c392bf2 100644 --- a/src/util-logopenfile.h +++ b/src/util-logopenfile.h @@ -26,6 +26,7 @@ #include "conf.h" /* ConfNode */ #include "tm-modules.h" /* LogFileCtx */ +#include "util-buffer.h" #ifdef HAVE_LIBHIREDIS #include "hiredis/hiredis.h" @@ -41,6 +42,10 @@ enum LogFileType { LOGFILE_TYPE_FILE, LOGFILE_TYPE_UNIX_STREAM, LOGFILE_TYPE_REDIS }; +typedef struct SyslogSetup_ { + int alert_syslog_level; +} SyslogSetup; + #ifdef HAVE_LIBHIREDIS enum RedisMode { REDIS_LIST, REDIS_CHANNEL }; @@ -62,9 +67,12 @@ typedef struct LogFileCtx_ { #endif }; + union { + SyslogSetup syslog_setup; #ifdef HAVE_LIBHIREDIS - RedisSetup redis_setup; + RedisSetup redis_setup; #endif + }; int (*Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp); void (*Close)(struct LogFileCtx_ *fp); @@ -115,6 +123,7 @@ typedef struct LogFileCtx_ { LogFileCtx *LogFileNewCtx(void); 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 SCConfLogReopen(LogFileCtx *);