Skip to content

Commit

Permalink
in_systemd: add 'lowercase' option (#4908)
Browse files Browse the repository at this point in the history
* in_systemd: add 'lowercase' option

Signed-off-by: Chitoku <[email protected]>
  • Loading branch information
chitoku-k authored Mar 4, 2022
1 parent a9b8753 commit 6ab7380
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
47 changes: 46 additions & 1 deletion plugins/in_systemd/systemd.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "systemd_config.h"
#include "systemd_db.h"

#include <ctype.h>

/* 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)
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 8 additions & 4 deletions plugins/in_systemd/systemd_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions plugins/in_systemd/systemd_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 6ab7380

Please sign in to comment.