From a8333f82b5cd9e8c0f1d2bf3353fd9599a59bc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Andersen?= Date: Fri, 18 Aug 2017 09:33:52 +0200 Subject: [PATCH] Add the possiblilty for configuring structured-data, so the systemd.netlog can be used agenst Cloud Based syslog services, such as Loggly --- README.md | 12 ++++++++++++ man/systemd-netlogd.conf.xml | 19 ++++++++++++++++++ src/conf-parser.c | 38 ++++++++++++++++++++++++++++++++++++ src/conf-parser.h | 3 +++ src/netlog-gperf.gperf | 1 + src/netlog-manager.h | 1 + src/netlog-network.c | 6 +++++- 7 files changed, 79 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d7c73ec..b52f017 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ Create a user **systemd-journal-netlog** The the address string format is similar to socket units. See systemd.socket(1) + Optional settings + + StructuredData= + Meta information about the syslog message, which can be used for Cloud Based + syslog servers, such as Loggly + **EXAMPLE** Example 1. /etc/systemd/systemd-netlogd.conf @@ -38,3 +44,9 @@ Example 2. /etc/systemd/systemd-netlogd.conf [Network] Address=192.168.8.101:514 + +Example 3. /etc/systemd/systemd-netlogd.conf + + [Network] + Address=192.168.8.101:514 + StructuredData=[1ab456b6-90bb-6578-abcd-5b734584aaaa@41058] diff --git a/man/systemd-netlogd.conf.xml b/man/systemd-netlogd.conf.xml index 7000085..31bf13e 100644 --- a/man/systemd-netlogd.conf.xml +++ b/man/systemd-netlogd.conf.xml @@ -80,6 +80,14 @@ + + StructuredData= + Meta information about the syslog message, + which can be used for Cloud Based syslog servers, such as Loggly + + The StructureData is only an optional setting + + @@ -103,6 +111,17 @@ Address=192.168.8.101:514 + + Example + + /etc/systemd/systemd-netlogd.conf + [Network] +Address=192.168.8.101:514 +StructuredData=[1ab456b6-90bb-6578-abcd-5b734584aaaa@41058] + + + + See Also diff --git a/src/conf-parser.c b/src/conf-parser.c index 7589d18..0201fd2 100644 --- a/src/conf-parser.c +++ b/src/conf-parser.c @@ -458,3 +458,41 @@ int config_parse_many(const char *conf_file, return 0; \ } \ struct __useless_struct_to_allow_trailing_semicolon__ + +int config_parse_string( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + char **s = data, *n; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if (!utf8_is_valid(rvalue)) { + log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue); + return 0; + } + + if (isempty(rvalue)) + n = NULL; + else { + n = strdup(rvalue); + if (!n) + return log_oom(); + } + + free(*s); + *s = n; + + return 0; +} diff --git a/src/conf-parser.h b/src/conf-parser.h index 107f16e..f40836d 100644 --- a/src/conf-parser.h +++ b/src/conf-parser.h @@ -103,6 +103,9 @@ int config_parse_many(const char *conf_file, /* possibly NULL */ bool relaxed, void *userdata); +/* Generic parsers */ +int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); + #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \ int function(const char *unit, \ const char *filename, \ diff --git a/src/netlog-gperf.gperf b/src/netlog-gperf.gperf index 3c2ce40..221771c 100644 --- a/src/netlog-gperf.gperf +++ b/src/netlog-gperf.gperf @@ -16,3 +16,4 @@ struct ConfigPerfItem; %includes %% Network.Address, config_parse_netlog_remote_address, 0, 0 +Network.StructuredData, config_parse_string, 0, offsetof(Manager, structured_data) diff --git a/src/netlog-manager.h b/src/netlog-manager.h index cf7a5e8..742f51f 100644 --- a/src/netlog-manager.h +++ b/src/netlog-manager.h @@ -52,6 +52,7 @@ struct Manager { char *state_file; char *last_cursor, *current_cursor; + char *structured_data; }; int manager_new(Manager **ret, const char *state_file, const char *cursor); diff --git a/src/netlog-network.c b/src/netlog-network.c index 4d8b817..f7c0f65 100644 --- a/src/netlog-network.c +++ b/src/netlog-network.c @@ -161,7 +161,11 @@ int manager_push_to_network(Manager *m, IOVEC_SET_STRING(iov[n++], " "); /* Eighth: [structured-data] */ - IOVEC_SET_STRING(iov[n++], RFC_5424_NILVALUE); + if (m->structured_data) + IOVEC_SET_STRING(iov[n++], m->structured_data); + else + IOVEC_SET_STRING(iov[n++], RFC_5424_NILVALUE); + IOVEC_SET_STRING(iov[n++], " "); /* Ninth: message */