diff --git a/plugins/in_systemd/systemd.c b/plugins/in_systemd/systemd.c index 75d638c0f2e..7575ac4e285 100644 --- a/plugins/in_systemd/systemd.c +++ b/plugins/in_systemd/systemd.c @@ -25,6 +25,8 @@ #include "systemd_config.h" #include "systemd_db.h" +#include + /* msgpack helpers to pack unsigned ints (it takes care of endianness */ #define pack_uint16(buf, d) _msgpack_store16(buf, (uint16_t) d) #define pack_uint32(buf, d) _msgpack_store32(buf, (uint32_t) d) @@ -73,6 +75,7 @@ static int in_systemd_collect(struct flb_input_instance *ins, { int ret; int ret_j; + int i; int len; int entries = 0; int skip_entries = 0; @@ -82,10 +85,12 @@ static int in_systemd_collect(struct flb_input_instance *ins, uint8_t h; uint64_t usec; size_t length; + size_t threshold; const char *sep; const char *key; const char *val; char *tmp; + char *buf = NULL; #ifdef FLB_HAVE_SQLDB char *cursor = NULL; #endif @@ -125,6 +130,17 @@ static int in_systemd_collect(struct flb_input_instance *ins, } } + if (ctx->lowercase == FLB_TRUE) { + ret = sd_journal_get_data_threshold(ctx->j, &threshold); + if (ret != 0) { + flb_plg_error(ctx->ins, + "error setting up systemd data. " + "sd_journal_get_data_threshold() return value '%i'", + ret); + return FLB_SYSTEMD_ERROR; + } + } + while ((ret_j = sd_journal_next(ctx->j)) > 0) { /* If the tag is composed dynamically, gather the Systemd Unit name */ if (ctx->dynamic_tag) { @@ -211,14 +227,36 @@ static int in_systemd_collect(struct flb_input_instance *ins, key++; length--; } + sep = strchr(key, '='); if (sep == NULL) { skip_entries++; continue; } + len = (sep - key); msgpack_pack_str(&mp_pck, len); - msgpack_pack_str_body(&mp_pck, key, len); + + if (ctx->lowercase == FLB_TRUE) { + /* + * Ensure buf to have enough space for the key because the libsystemd + * might return larger data than the threshold. + */ + if (buf == NULL) { + buf = flb_sds_create_len(NULL, threshold); + } + if (flb_sds_alloc(buf) < len) { + buf = flb_sds_increase(buf, len - flb_sds_alloc(buf)); + } + for (i = 0; i < len; i++) { + buf[i] = tolower(key[i]); + } + + msgpack_pack_str_body(&mp_pck, buf, len); + } + else { + msgpack_pack_str_body(&mp_pck, key, len); + } val = sep + 1; len = length - (sep - key) - 1; @@ -276,6 +314,8 @@ static int in_systemd_collect(struct flb_input_instance *ins, } } + flb_sds_destroy(buf); + #ifdef FLB_HAVE_SQLDB /* Save cursor */ if (ctx->db) { @@ -493,6 +533,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_systemd_config, read_from_tail), "Read the journal from the end (tail)" }, + { + FLB_CONFIG_MAP_BOOL, "lowercase", "false", + 0, FLB_TRUE, offsetof(struct flb_systemd_config, lowercase), + "Lowercase the fields" + }, { FLB_CONFIG_MAP_BOOL, "strip_underscores", "false", 0, FLB_TRUE, offsetof(struct flb_systemd_config, strip_underscores), diff --git a/plugins/in_systemd/systemd_config.c b/plugins/in_systemd/systemd_config.c index ee81c87106e..5cb25d3f575 100644 --- a/plugins/in_systemd/systemd_config.c +++ b/plugins/in_systemd/systemd_config.c @@ -151,16 +151,19 @@ struct flb_systemd_config *flb_systemd_config_create(struct flb_input_instance * if (ctx->filter_type) { if (strcasecmp(ctx->filter_type, "and") == 0) { journal_filter_is_and = FLB_TRUE; - } else if (strcasecmp(ctx->filter_type, "or") == 0) { + } + else if (strcasecmp(ctx->filter_type, "or") == 0) { journal_filter_is_and = FLB_FALSE; - } else { + } + else { flb_plg_error(ctx->ins, "systemd_filter_type must be 'and' or 'or'. Got %s", ctx->filter_type); flb_free(ctx); return NULL; } - } else { + } + else { journal_filter_is_and = FLB_FALSE; } @@ -172,7 +175,8 @@ struct flb_systemd_config *flb_systemd_config_create(struct flb_input_instance * sd_journal_add_match(ctx->j, mv->val.str, 0); if (journal_filter_is_and) { sd_journal_add_conjunction(ctx->j); - } else { + } + else { sd_journal_add_disjunction(ctx->j); } } diff --git a/plugins/in_systemd/systemd_config.h b/plugins/in_systemd/systemd_config.h index 2716e252335..6b60d65b80d 100644 --- a/plugins/in_systemd/systemd_config.h +++ b/plugins/in_systemd/systemd_config.h @@ -51,6 +51,7 @@ struct flb_systemd_config { struct mk_list *systemd_filters; int pending_records; int read_from_tail; /* read_from_tail option */ + int lowercase; int strip_underscores; /* Internal */