Skip to content

Commit 5d0aa3a

Browse files
committed
Add option to skip message with certain syslog facilities
Closes: #120
1 parent 9e20b54 commit 5d0aa3a

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ Optional settings
103103
A boolean. Specifies whether to extract SYSLOG_STRUCTURED_DATA= from journal. Defaults to false.
104104

105105
UseSysLogMsgId=
106-
A boolean. Specifies whether to extract SYSLOG_MSGID= from journal. Defaults to false.
106+
A boolean. Specifies whether to extract SYSLOG_MSGID= from journal. Defaults to false.
107+
108+
ExcludeSyslogFacility=
109+
A list of strings. Specifies the syslog facilities to skip forwarding.
107110

108111
**EXAMPLE**
109112

@@ -116,13 +119,14 @@ Address=239.0.0.1:6000
116119
#LogFormat=rfc5424
117120
```
118121

119-
Example 2.UDP
122+
Example 2.UDP and skipping AUTH and AUTHPRIV messages
120123

121124
``` toml
122125
[Network]
123126
Address=192.168.8.101:514
124127
#Protocol=udp
125128
LogFormat=rfc3339
129+
ExcludeSyslogFacility=auth authpriv
126130
```
127131

128132
Example 3. Structured data

conf/netlogd.conf.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
#KeepAliveProbes=
1717
#NoDelay=no
1818
#SendBuffer=
19+
#ExcludeSyslogFacility=

src/netlog/netlog-conf.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "conf-parser.h"
66
#include "def.h"
7+
#include "extract-word.h"
78
#include "in-addr-util.h"
89
#include "netlog-conf.h"
910
#include "parse-util.h"
@@ -197,6 +198,48 @@ int config_parse_namespace(const char *unit,
197198
return 0;
198199
}
199200

201+
int config_parse_syslog_facility(const char *unit,
202+
const char *filename,
203+
unsigned line,
204+
const char *section,
205+
unsigned section_line,
206+
const char *lvalue,
207+
int ltype,
208+
const char *rvalue,
209+
void *data,
210+
void *userdata) {
211+
Manager *m = userdata;
212+
uint32_t val = 0;
213+
int r;
214+
215+
assert(filename);
216+
assert(lvalue);
217+
assert(rvalue);
218+
assert(data);
219+
assert(m);
220+
221+
for (const char *p = rvalue;;) {
222+
_cleanup_free_ char *word = NULL;
223+
224+
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
225+
if (r < 0) {
226+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
227+
return 0;
228+
}
229+
if (r == 0)
230+
break;
231+
232+
r = syslog_facility_from_string(word);
233+
if (r < 0) {
234+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syslog facility '%s', ignoring: %m", word);
235+
} else
236+
val |= UINT32_C(1) << r;
237+
}
238+
239+
m->excluded_syslog_facilities = val;
240+
return 0;
241+
}
242+
200243
int manager_parse_config_file(Manager *m) {
201244
int r;
202245

src/netlog/netlog-conf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ int config_parse_namespace(const char *unit,
6262
void *data,
6363
void *userdata);
6464

65+
int config_parse_syslog_facility(const char *unit,
66+
const char *filename,
67+
unsigned line,
68+
const char *section,
69+
unsigned section_line,
70+
const char *lvalue,
71+
int ltype,
72+
const char *rvalue,
73+
void *data,
74+
void *userdata);
75+
6576
int manager_parse_config_file(Manager *m);

src/netlog/netlog-gperf.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ Network.KeepAliveIntervalSec, config_parse_sec, 0, off
3232
Network.KeepAliveProbes, config_parse_unsigned, 0, offsetof(Manager, keep_alive_cnt)
3333
Network.NoDelay, config_parse_bool, 0, offsetof(Manager, no_delay)
3434
Network.SendBuffer, config_parse_iec_size, 0, offsetof(Manager, send_buffer)
35+
Network.ExcludeSyslogFacility, config_parse_syslog_facility, 0, offsetof(Manager, excluded_syslog_facilities)

src/netlog/netlog-manager.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ static const char *const log_format_table[_SYSLOG_TRANSMISSION_LOG_FORMAT_MAX] =
4444

4545
DEFINE_STRING_TABLE_LOOKUP(log_format, int);
4646

47+
static const char *const syslog_facility_table[_SYSLOG_FACILITY_MAX] = {
48+
[SYSLOG_FACILITY_KERN] = "kern",
49+
[SYSLOG_FACILITY_USER] = "user",
50+
[SYSLOG_FACILITY_MAIL] = "mail",
51+
[SYSLOG_FACILITY_DAEMON] = "daemon",
52+
[SYSLOG_FACILITY_AUTH] = "auth",
53+
[SYSLOG_FACILITY_SYSLOG] = "syslog",
54+
[SYSLOG_FACILITY_LPR] = "lpr",
55+
[SYSLOG_FACILITY_NEWS] = "news",
56+
[SYSLOG_FACILITY_UUCP] = "uucp",
57+
[SYSLOG_FACILITY_CRON] = "cron",
58+
[SYSLOG_FACILITY_AUTHPRIV] = "authpriv",
59+
[SYSLOG_FACILITY_FTP] = "ftp",
60+
[SYSLOG_FACILITY_NTP] = "ntp",
61+
[SYSLOG_FACILITY_SECURITY] = "security",
62+
[SYSLOG_FACILITY_CONSOLE] = "console",
63+
[SYSLOG_FACILITY_SOLARIS_CRON] = "solaris-cron",
64+
[SYSLOG_FACILITY_LOCAL0] = "local0",
65+
[SYSLOG_FACILITY_LOCAL1] = "local1",
66+
[SYSLOG_FACILITY_LOCAL2] = "local2",
67+
[SYSLOG_FACILITY_LOCAL3] = "local3",
68+
[SYSLOG_FACILITY_LOCAL4] = "local4",
69+
[SYSLOG_FACILITY_LOCAL5] = "local5",
70+
[SYSLOG_FACILITY_LOCAL6] = "local6",
71+
[SYSLOG_FACILITY_LOCAL7] = "local7",
72+
};
73+
74+
DEFINE_STRING_TABLE_LOOKUP(syslog_facility, int);
75+
4776
typedef struct ParseFieldVec {
4877
const char *field;
4978
size_t field_len;
@@ -180,6 +209,10 @@ static int manager_read_journal_input(Manager *m) {
180209
r = safe_atou(facility, &fac);
181210
if (r < 0)
182211
log_debug("Failed to parse syslog facility: %s", facility);
212+
else if (fac < _SYSLOG_FACILITY_MAX && ((UINT32_C(1) << fac) & m->excluded_syslog_facilities)) {
213+
log_debug("Skipping message with excluded facility %s.", syslog_facility_to_string(fac));
214+
return 0;
215+
}
183216

184217
if (fac >= LOG_NFACILITIES)
185218
fac = JOURNAL_DEFAULT_FACILITY;

src/netlog/netlog-manager.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ typedef enum SysLogTransmissionLogFormat {
2929
_SYSLOG_TRANSMISSION_LOG_FORMAT_INVALID = -EINVAL,
3030
} SysLogTransmissionLogFormat;
3131

32+
/* RFC 5424 Section 6.2.1 */
33+
typedef enum SysLogFacility {
34+
SYSLOG_FACILITY_KERN = 0,
35+
SYSLOG_FACILITY_USER = 1,
36+
SYSLOG_FACILITY_MAIL = 2,
37+
SYSLOG_FACILITY_DAEMON = 3,
38+
SYSLOG_FACILITY_AUTH = 4,
39+
SYSLOG_FACILITY_SYSLOG = 5,
40+
SYSLOG_FACILITY_LPR = 6,
41+
SYSLOG_FACILITY_NEWS = 7,
42+
SYSLOG_FACILITY_UUCP = 8,
43+
SYSLOG_FACILITY_CRON = 9,
44+
SYSLOG_FACILITY_AUTHPRIV = 10,
45+
SYSLOG_FACILITY_FTP = 11,
46+
SYSLOG_FACILITY_NTP = 12,
47+
SYSLOG_FACILITY_SECURITY = 13,
48+
SYSLOG_FACILITY_CONSOLE = 14,
49+
SYSLOG_FACILITY_SOLARIS_CRON = 15,
50+
SYSLOG_FACILITY_LOCAL0 = 16,
51+
SYSLOG_FACILITY_LOCAL1 = 17,
52+
SYSLOG_FACILITY_LOCAL2 = 18,
53+
SYSLOG_FACILITY_LOCAL3 = 19,
54+
SYSLOG_FACILITY_LOCAL4 = 20,
55+
SYSLOG_FACILITY_LOCAL5 = 21,
56+
SYSLOG_FACILITY_LOCAL6 = 22,
57+
SYSLOG_FACILITY_LOCAL7 = 23,
58+
_SYSLOG_FACILITY_MAX,
59+
} SysLogFacility;
60+
3261
typedef struct Manager Manager;
3362

3463
struct Manager {
@@ -58,6 +87,8 @@ struct Manager {
5887

5988
char *server_name;
6089

90+
uint32_t excluded_syslog_facilities;
91+
6192
/* journal */
6293
int journal_watch_fd;
6394
int namespace_flags;
@@ -124,3 +155,6 @@ int protocol_from_string(const char *s) _pure_;
124155

125156
const char *log_format_to_string(int v) _const_;
126157
int log_format_from_string(const char *s) _pure_;
158+
159+
const char *syslog_facility_to_string(int v) _const_;
160+
int syslog_facility_from_string(const char *s) _pure_;

0 commit comments

Comments
 (0)