Skip to content

Commit 0613583

Browse files
committed
in_kmsg: add a field for syslog timestamp
It would be convenient if this plugin also printed syslog format timestamp of the log message, so add a new field with this value. Signed-off-by: Mohd Husaam Mehdi <[email protected]>
1 parent 1ae8ed9 commit 0613583

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

plugins/in_kmsg/in_kmsg.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <sys/stat.h>
3737
#include <sys/time.h>
3838
#include <inttypes.h>
39+
#include <time.h>
3940

4041
#include "in_kmsg.h"
4142

@@ -164,6 +165,15 @@ static inline int process_line(const char *line,
164165

165166
flb_time_set(&ts, ctx->boot_time.tv_sec + tv.tv_sec, tv.tv_usec * 1000);
166167

168+
/* Format syslog timestamp: "Jul 03 10:31:53" */
169+
time_t real_time = ctx->boot_time.tv_sec + tv.tv_sec;
170+
struct tm tm_info;
171+
char syslog_ts[32];
172+
173+
localtime_r(&real_time, &tm_info);
174+
strftime(syslog_ts, sizeof(syslog_ts), "%b %d %H:%M:%S", &tm_info);
175+
int syslog_ts_len = strlen(syslog_ts);
176+
167177
/* Now process the human readable message */
168178
p = strchr(p, ';');
169179
if (!p) {
@@ -197,7 +207,10 @@ static inline int process_line(const char *line,
197207
FLB_LOG_EVENT_UINT64_VALUE(tv.tv_usec),
198208

199209
FLB_LOG_EVENT_CSTRING_VALUE("msg"),
200-
FLB_LOG_EVENT_STRING_VALUE((char *) p, line_len - 1));
210+
FLB_LOG_EVENT_STRING_VALUE((char *) p, line_len - 1),
211+
212+
FLB_LOG_EVENT_CSTRING_VALUE("syslog_ts"),
213+
FLB_LOG_EVENT_STRING_VALUE(syslog_ts, syslog_ts_len));
201214
}
202215

203216
if (ret == FLB_EVENT_ENCODER_SUCCESS) {

0 commit comments

Comments
 (0)