Skip to content

Commit 0e80e26

Browse files
committed
Introduce custom SYSLOG_MSGID and SYSLOG_STRUCTURED_DATA
sd_journal_send( "MESSAGE=%s", "Message to process", "PRIORITY=%s", "4", "SYSLOG_FACILITY=%s", "1", "SYSLOG_MSGID=%s", "1011", "SYSLOG_STRUCTURED_DATA=%s", R"([exampleSDID@32473 iut="3" eventSource="Application"])", NULL );
1 parent 055f66f commit 0e80e26

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

src/netlog/netlog-gperf.gperf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ struct ConfigPerfItem;
1515
%struct-type
1616
%includes
1717
%%
18-
Network.Address, config_parse_netlog_remote_address, 0, 0
19-
Network.Protocol, config_parse_protocol, 0, offsetof(Manager, protocol)
20-
Network.LogFormat, config_parse_log_format, 0, offsetof(Manager, log_format)
21-
Network.StructuredData, config_parse_string, 0, offsetof(Manager, structured_data)
18+
Network.Address, config_parse_netlog_remote_address, 0, 0
19+
Network.Protocol, config_parse_protocol, 0, offsetof(Manager, protocol)
20+
Network.LogFormat, config_parse_log_format, 0, offsetof(Manager, log_format)
21+
Network.StructuredData, config_parse_string, 0, offsetof(Manager, structured_data)
22+
Network.UseSysLogStructuredData, config_parse_bool, 0, offsetof(Manager, syslog_structured_data)
23+
Network.UseSysLogMsgId, config_parse_bool, 0, offsetof(Manager, syslog_msgid)

src/netlog/netlog-manager.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ static int parse_field(const void *data, size_t length, const char *field, char
7171
}
7272

7373
static int manager_read_journal_input(Manager *m) {
74-
_cleanup_free_ char *facility = NULL, *identifier = NULL,
75-
*priority = NULL, *message = NULL, *pid = NULL,
76-
*hostname = NULL;
74+
_cleanup_free_ char *facility = NULL, *identifier = NULL, *priority = NULL, *message = NULL, *pid = NULL,
75+
*hostname = NULL, *structured_data = NULL, *msgid = NULL;
7776
unsigned sev = JOURNAL_DEFAULT_SEVERITY;
7877
unsigned fac = JOURNAL_DEFAULT_FACILITY;
7978
struct timeval tv;
@@ -120,6 +119,16 @@ static int manager_read_journal_input(Manager *m) {
120119
r = parse_field(data, length, "MESSAGE=", &message);
121120
if (r < 0)
122121
return r;
122+
123+
r = parse_field(data, length, "SYSLOG_STRUCTURED_DATA=", &structured_data);
124+
if (r < 0)
125+
return r;
126+
else if (r > 0)
127+
continue;
128+
129+
r = parse_field(data, length, "SYSLOG_MSGID=", &msgid);
130+
if (r < 0)
131+
return r;
123132
}
124133

125134
r = sd_journal_get_realtime_usec(m->journal, &realtime);
@@ -148,8 +157,15 @@ static int manager_read_journal_input(Manager *m) {
148157
sev = JOURNAL_DEFAULT_SEVERITY;
149158
}
150159

151-
return manager_push_to_network(m, sev, fac, identifier,
152-
message, hostname, pid, r >= 0 ? &tv : NULL);
160+
return manager_push_to_network(m,
161+
sev,
162+
fac,
163+
identifier,
164+
message, hostname,
165+
pid,
166+
r >= 0 ? &tv : NULL,
167+
structured_data,
168+
msgid);
153169
}
154170

155171
static int update_cursor_state(Manager *m) {

src/netlog/netlog-manager.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ struct Manager {
4444
sd_journal *journal;
4545

4646
char *state_file;
47-
4847
char *last_cursor, *current_cursor;
4948
char *structured_data;
49+
5050
SysLogTransmissionProtocol protocol;
5151
SysLogTransmissionLogFormat log_format;
52+
53+
bool syslog_structured_data;
54+
bool syslog_msgid;
5255
};
5356

5457
int manager_new(const char *state_file, const char *cursor, Manager **ret);
@@ -62,10 +65,16 @@ void manager_disconnect(Manager *m);
6265
void manager_close_network_socket(Manager *m);
6366
int manager_open_network_socket(Manager *m);
6467

65-
int manager_push_to_network(Manager *m, int severity, int facility,
66-
const char *identifier, const char *message,
67-
const char *hostname, const char *pid,
68-
const struct timeval *tv);
68+
int manager_push_to_network(Manager *m,
69+
int severity,
70+
int facility,
71+
const char *identifier,
72+
const char *message,
73+
const char *hostname,
74+
const char *pid,
75+
const struct timeval *tv,
76+
const char *syslog_structured_data,
77+
const char *syslog_msgid);
6978

7079
const char *protocol_to_string(int v) _const_;
7180
int protocol_from_string(const char *s) _pure_;

src/netlog/netlog-network.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ static int format_rfc5424(Manager *m,
9494
const char *message,
9595
const char *hostname,
9696
const char *pid,
97-
const struct timeval *tv) {
97+
const struct timeval *tv,
98+
const char *syslog_structured_data,
99+
const char *syslog_msgid) {
98100

99101
char header_time[FORMAT_TIMESTAMP_MAX];
100102
char header_priority[sizeof("< >1 ")];
@@ -140,12 +142,18 @@ static int format_rfc5424(Manager *m,
140142
IOVEC_SET_STRING(iov[n++], " ");
141143

142144
/* Seventh: msgid */
143-
IOVEC_SET_STRING(iov[n++], RFC_5424_NILVALUE);
145+
if (syslog_msgid)
146+
IOVEC_SET_STRING(iov[n++], syslog_msgid);
147+
else
148+
IOVEC_SET_STRING(iov[n++], RFC_5424_NILVALUE);
149+
144150
IOVEC_SET_STRING(iov[n++], " ");
145151

146152
/* Eighth: [structured-data] */
147153
if (m->structured_data)
148154
IOVEC_SET_STRING(iov[n++], m->structured_data);
155+
else if (syslog_structured_data)
156+
IOVEC_SET_STRING(iov[n++], syslog_structured_data);
149157
else
150158
IOVEC_SET_STRING(iov[n++], RFC_5424_NILVALUE);
151159

@@ -238,7 +246,9 @@ int manager_push_to_network(Manager *m,
238246
const char *message,
239247
const char *hostname,
240248
const char *pid,
241-
const struct timeval *tv) {
249+
const struct timeval *tv,
250+
const char *syslog_structured_data,
251+
const char *syslog_msgid) {
242252

243253
int r;
244254

@@ -248,7 +258,7 @@ int manager_push_to_network(Manager *m,
248258
return 0;
249259

250260
if (m->log_format == SYSLOG_TRANSMISSION_LOG_FORMAT_RFC_5424)
251-
r = format_rfc5424(m, severity, facility, identifier, message, hostname, pid, tv);
261+
r = format_rfc5424(m, severity, facility, identifier, message, hostname, pid, tv, syslog_structured_data, syslog_msgid);
252262
else
253263
r = format_rfc3339(m, severity, facility, identifier, message, hostname, pid, tv);
254264

0 commit comments

Comments
 (0)