Skip to content

Commit b5f5f12

Browse files
committed
in_syslog: enable charset decoding
Enable charset deocding with "from_encoding"/"encoding" with syslog module. Useful syslog is not using UTF-8 (example: latin1 / CP1252) Requires: FLB_ICONV Signed-off-by: Jukka Pihl <[email protected]>
1 parent b85a9f9 commit b5f5f12

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

plugins/in_syslog/syslog.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include <fluent-bit/flb_info.h>
2525
#include <fluent-bit/flb_input.h>
2626

27+
#ifndef FLB_HAVE_ICONV
28+
#include <fluent-bit/flb_iconv.h>
29+
#endif
30+
2731
/* Syslog modes */
2832
#define FLB_SYSLOG_UNIX_TCP 1
2933
#define FLB_SYSLOG_UNIX_UDP 2
@@ -62,6 +66,10 @@ struct flb_syslog {
6266
struct mk_list connections;
6367
struct mk_event_loop *evl;
6468
struct flb_input_instance *i_ins;
69+
70+
#ifdef FLB_HAVE_ICONV
71+
struct flb_iconv *iconvert;
72+
#endif
6573
};
6674

6775
#endif

plugins/in_syslog/syslog_conf.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <fluent-bit/flb_log.h>
2626
#include <fluent-bit/flb_parser.h>
2727
#include <fluent-bit/flb_utils.h>
28+
#ifdef FLB_HAVE_ICONV
29+
#include <fluent-bit/flb_iconv.h>
30+
#endif
2831

2932
#include "syslog.h"
3033
#include "syslog_server.h"
@@ -36,6 +39,9 @@ struct flb_syslog *syslog_conf_create(struct flb_input_instance *i_ins,
3639
const char *tmp;
3740
char port[16];
3841
struct flb_syslog *ctx;
42+
#ifdef FLB_HAVE_ICONV
43+
char *tmp2;
44+
#endif
3945

4046
ctx = flb_calloc(1, sizeof(struct flb_syslog));
4147
if (!ctx) {
@@ -144,6 +150,27 @@ struct flb_syslog *syslog_conf_create(struct flb_input_instance *i_ins,
144150
return NULL;
145151
}
146152

153+
#ifdef FLB_HAVE_ICONV
154+
tmp = flb_input_get_property("from_encoding", i_ins);
155+
tmp2 = flb_input_get_property("encoding", i_ins);
156+
if(tmp) {
157+
if(!tmp2) {
158+
tmp2 = "UTF8";
159+
} else if(!strcasecmp(tmp2,"default")) {
160+
tmp2 = "";
161+
}
162+
if(!strcasecmp(tmp,"default")) {
163+
tmp = "";
164+
}
165+
ctx->iconvert = flb_iconv_open(tmp2,tmp);
166+
if(ctx->iconvert == NULL) {
167+
flb_error("[in_tail] cannot init iconv: '%s'=>'%s'", tmp, tmp2);
168+
}
169+
} else {
170+
ctx->iconvert = NULL;
171+
}
172+
#endif
173+
147174
return ctx;
148175
}
149176

plugins/in_syslog/syslog_prot.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <fluent-bit/flb_info.h>
2424
#include <fluent-bit/flb_parser.h>
2525
#include <fluent-bit/flb_time.h>
26+
#ifdef FLB_HAVE_ICONV
27+
#include <fluent-bit/flb_iconv.h>
28+
#endif
2629

2730
#include "syslog.h"
2831
#include "syslog_conn.h"
@@ -63,6 +66,12 @@ int syslog_prot_process(struct syslog_conn *conn)
6366
size_t out_size;
6467
struct flb_time out_time;
6568
struct flb_syslog *ctx = conn->ctx;
69+
char *line;
70+
size_t line_len;
71+
#ifdef FLB_HAVE_ICONV
72+
char *iconv_data;
73+
size_t iconv_len;
74+
#endif
6675

6776
eof = p = conn->buf_data;
6877
end = conn->buf_data + conn->buf_len;
@@ -97,8 +106,24 @@ int syslog_prot_process(struct syslog_conn *conn)
97106
continue;
98107
}
99108

109+
#ifdef FLB_HAVE_ICONV
110+
iconv_data = NULL;
111+
line = p;
112+
line_len = len;
113+
if(ctx->iconvert) {
114+
ret = flb_iconv_execute(ctx->iconvert, line, line_len, &iconv_data, &iconv_len, FLB_ICONV_ACCEPT_NOT_CHANGED);
115+
if(ret == FLB_ICONV_SUCCESS) {
116+
line = iconv_data;
117+
line_len = iconv_len;
118+
}
119+
}
120+
#else
121+
line = p;
122+
line_len = len;
123+
#endif
124+
100125
/* Process the string */
101-
ret = flb_parser_do(ctx->parser, p, len,
126+
ret = flb_parser_do(ctx->parser, line, line_len,
102127
&out_buf, &out_size, &out_time);
103128
if (ret >= 0) {
104129
pack_line(ctx, &out_time, out_buf, out_size);
@@ -107,7 +132,12 @@ int syslog_prot_process(struct syslog_conn *conn)
107132
else {
108133
flb_warn("[in_syslog] error parsing log message");
109134
}
110-
135+
#ifdef FLB_HAVE_ICONV
136+
if(iconv_data) {
137+
flb_free(iconv_data);
138+
iconv_data = NULL;
139+
}
140+
#endif
111141
conn->buf_parsed += len + 1;
112142
end = conn->buf_data + conn->buf_len;
113143
eof = p = conn->buf_data + conn->buf_parsed;

0 commit comments

Comments
 (0)