Skip to content

Commit 045b372

Browse files
authored
Merge pull request #134 from cgzones/facility
Add option to skip message with certain syslog facilities
2 parents 50887bf + 7e1338c commit 045b372

9 files changed

+257
-22
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ 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. Possible values are: "kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news", "uucp", "cron", "authpriv", "ftp", "ntp", "security", "console", "solaris-cron", "local0", "local1", "local2", "local3", "local4", "local5", "local6" and "local7".
110+
111+
ExcludeSyslogLevel=
112+
A list of strings. Specifies the syslog levels to skip forwarding. Possible values are: "emerg", "alert", "crit", "err", "warning", "notice", "info" and "debug".
107113

108114
**EXAMPLE**
109115

@@ -146,7 +152,18 @@ UseSysLogStructuredData=yes
146152
UseSysLogMsgId=yes
147153
```
148154

149-
Example 5. TLS with certificate authentocation mode
155+
Example 5. Skipping messages with facility AUTH or AUTHPRIV and messages with level DEBUG
156+
157+
``` toml
158+
[Network]
159+
Address=192.168.8.101:514
160+
#Protocol=udp
161+
LogFormat=rfc3339
162+
ExcludeSyslogFacility=auth authpriv
163+
ExcludeSyslogLevel=debug
164+
```
165+
166+
Example 6. TLS with certificate authentocation mode
150167

151168
``` toml
152169
[Network]
@@ -156,7 +173,7 @@ Protocol=tls
156173
TLSCertificateAuthMode=warn
157174
```
158175

159-
Example 6. DTLS with certificate authentocation mode
176+
Example 7. DTLS with certificate authentocation mode
160177

161178
``` toml
162179
[Network]

conf/netlogd.conf.in

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
#KeepAliveProbes=
1717
#NoDelay=no
1818
#SendBuffer=
19+
#ExcludeSyslogFacility=
20+
#ExcludeSyslogLevel=

src/netlog/netlog-conf.c

+85
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,90 @@ 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", word);
235+
} else
236+
val |= UINT32_C(1) << r;
237+
}
238+
239+
m->excluded_syslog_facilities = val;
240+
return 0;
241+
}
242+
243+
int config_parse_syslog_level(const char *unit,
244+
const char *filename,
245+
unsigned line,
246+
const char *section,
247+
unsigned section_line,
248+
const char *lvalue,
249+
int ltype,
250+
const char *rvalue,
251+
void *data,
252+
void *userdata) {
253+
Manager *m = userdata;
254+
uint8_t val = 0;
255+
int r;
256+
257+
assert(filename);
258+
assert(lvalue);
259+
assert(rvalue);
260+
assert(data);
261+
assert(m);
262+
263+
for (const char *p = rvalue;;) {
264+
_cleanup_free_ char *word = NULL;
265+
266+
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
267+
if (r < 0) {
268+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
269+
return 0;
270+
}
271+
if (r == 0)
272+
break;
273+
274+
r = syslog_level_from_string(word);
275+
if (r < 0) {
276+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syslog level '%s', ignoring", word);
277+
} else
278+
val |= UINT8_C(1) << r;
279+
}
280+
281+
m->excluded_syslog_levels = val;
282+
return 0;
283+
}
284+
200285
int manager_parse_config_file(Manager *m) {
201286
int r;
202287

src/netlog/netlog-conf.h

+22
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,26 @@ 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+
76+
int config_parse_syslog_level(const char *unit,
77+
const char *filename,
78+
unsigned line,
79+
const char *section,
80+
unsigned section_line,
81+
const char *lvalue,
82+
int ltype,
83+
const char *rvalue,
84+
void *data,
85+
void *userdata);
86+
6587
int manager_parse_config_file(Manager *m);

src/netlog/netlog-gperf.gperf

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ 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)
36+
Network.ExcludeSyslogLevel, config_parse_syslog_level, 0, offsetof(Manager, excluded_syslog_levels)

src/netlog/netlog-manager.c

+59-6
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,56 @@ static const char *const protocol_table[_SYSLOG_TRANSMISSION_PROTOCOL_MAX] = {
3535
[SYSLOG_TRANSMISSION_PROTOCOL_TLS] = "tls",
3636
};
3737

38-
DEFINE_STRING_TABLE_LOOKUP(protocol, int);
38+
DEFINE_STRING_TABLE_LOOKUP(protocol, SysLogTransmissionProtocol);
3939

4040
static const char *const log_format_table[_SYSLOG_TRANSMISSION_LOG_FORMAT_MAX] = {
4141
[SYSLOG_TRANSMISSION_LOG_FORMAT_RFC_5424] = "rfc5424",
4242
[SYSLOG_TRANSMISSION_LOG_FORMAT_RFC_3339] = "rfc3339",
4343
};
4444

45-
DEFINE_STRING_TABLE_LOOKUP(log_format, int);
45+
DEFINE_STRING_TABLE_LOOKUP(log_format, SysLogTransmissionLogFormat);
46+
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, SysLogFacility);
75+
76+
static const char *const syslog_level_table[_SYSLOG_LEVEL_MAX] = {
77+
[SYSLOG_LEVEL_EMERGENCY] = "emerg",
78+
[SYSLOG_LEVEL_ALERT] = "alert",
79+
[SYSLOG_LEVEL_CRITICAL] = "crit",
80+
[SYSLOG_LEVEL_ERROR] = "err",
81+
[SYSLOG_LEVEL_WARNING] = "warning",
82+
[SYSLOG_LEVEL_NOTICE] = "notice",
83+
[SYSLOG_LEVEL_INFORMATIONAL] = "info",
84+
[SYSLOG_LEVEL_DEBUG] = "debug",
85+
};
86+
87+
DEFINE_STRING_TABLE_LOOKUP(syslog_level, SysLogLevel);
4688

4789
typedef struct ParseFieldVec {
4890
const char *field;
@@ -121,7 +163,7 @@ static int manager_read_journal_input(Manager *m) {
121163
structured_data_len = 0, msgid_len = 0, pid_len = 0;
122164
unsigned sev = JOURNAL_DEFAULT_SEVERITY;
123165
unsigned fac = JOURNAL_DEFAULT_FACILITY;
124-
struct timeval tv;
166+
struct timeval tv, *tvp = NULL;
125167
const void *data;
126168
usec_t realtime;
127169
size_t length;
@@ -169,14 +211,21 @@ static int manager_read_journal_input(Manager *m) {
169211
if (r < 0)
170212
log_warning_errno(r, "Failed to rerieve realtime from journal: %m");
171213
else {
172-
tv.tv_sec = realtime / USEC_PER_SEC;
173-
tv.tv_usec = realtime % USEC_PER_SEC;
214+
tv = (struct timeval) {
215+
.tv_sec = realtime / USEC_PER_SEC,
216+
.tv_usec = realtime % USEC_PER_SEC,
217+
};
218+
tvp = &tv;
174219
}
175220

176221
if (facility) {
177222
r = safe_atou(facility, &fac);
178223
if (r < 0)
179224
log_debug("Failed to parse syslog facility: %s", facility);
225+
else if (fac < _SYSLOG_FACILITY_MAX && ((UINT32_C(1) << fac) & m->excluded_syslog_facilities)) {
226+
log_debug("Skipping message with excluded syslog facility %s.", syslog_facility_to_string(fac));
227+
return 0;
228+
}
180229

181230
if (fac >= LOG_NFACILITIES)
182231
fac = JOURNAL_DEFAULT_FACILITY;
@@ -186,6 +235,10 @@ static int manager_read_journal_input(Manager *m) {
186235
r = safe_atou(priority, &sev);
187236
if (r < 0)
188237
log_debug("Failed to parse syslog priority: %s", priority);
238+
else if (sev < _SYSLOG_LEVEL_MAX && ((UINT8_C(1) << sev) & m->excluded_syslog_levels)) {
239+
log_debug("Skipping message with excluded syslog level %s.", syslog_level_to_string(sev));
240+
return 0;
241+
}
189242

190243
if (sev > LOG_DEBUG)
191244
sev = JOURNAL_DEFAULT_SEVERITY;
@@ -197,7 +250,7 @@ static int manager_read_journal_input(Manager *m) {
197250
identifier,
198251
message, hostname,
199252
pid,
200-
r >= 0 ? &tv : NULL,
253+
tvp,
201254
structured_data,
202255
m->syslog_msgid ? msgid : NULL);
203256
}

src/netlog/netlog-manager.h

+57-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,50 @@ 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+
_SYSLOG_FACILITY_INVALID = -EINVAL,
60+
} SysLogFacility;
61+
62+
/* RFC 5424 Section 6.2.1 */
63+
typedef enum SysLogLevel {
64+
SYSLOG_LEVEL_EMERGENCY = 0,
65+
SYSLOG_LEVEL_ALERT = 1,
66+
SYSLOG_LEVEL_CRITICAL = 2,
67+
SYSLOG_LEVEL_ERROR = 3,
68+
SYSLOG_LEVEL_WARNING = 4,
69+
SYSLOG_LEVEL_NOTICE = 5,
70+
SYSLOG_LEVEL_INFORMATIONAL = 6,
71+
SYSLOG_LEVEL_DEBUG = 7,
72+
_SYSLOG_LEVEL_MAX,
73+
_SYSLOG_LEVEL_INVALID = -EINVAL,
74+
} SysLogLevel;
75+
3276
typedef struct Manager Manager;
3377

3478
struct Manager {
@@ -58,6 +102,9 @@ struct Manager {
58102

59103
char *server_name;
60104

105+
uint32_t excluded_syslog_facilities;
106+
uint8_t excluded_syslog_levels;
107+
61108
/* journal */
62109
int journal_watch_fd;
63110
int namespace_flags;
@@ -119,8 +166,14 @@ int manager_push_to_network(Manager *m,
119166
const char *syslog_structured_data,
120167
const char *syslog_msgid);
121168

122-
const char *protocol_to_string(int v) _const_;
123-
int protocol_from_string(const char *s) _pure_;
169+
const char *protocol_to_string(SysLogTransmissionProtocol v) _const_;
170+
SysLogTransmissionProtocol protocol_from_string(const char *s) _pure_;
171+
172+
const char *log_format_to_string(SysLogTransmissionLogFormat v) _const_;
173+
SysLogTransmissionLogFormat log_format_from_string(const char *s) _pure_;
174+
175+
const char *syslog_facility_to_string(SysLogFacility v) _const_;
176+
SysLogFacility syslog_facility_from_string(const char *s) _pure_;
124177

125-
const char *log_format_to_string(int v) _const_;
126-
int log_format_from_string(const char *s) _pure_;
178+
const char *syslog_level_to_string(SysLogLevel v) _const_;
179+
SysLogLevel syslog_level_from_string(const char *s) _pure_;

src/netlog/netlog-tls.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
#include "netlog-ssl.h"
1919

20-
static const char *const certificate_auth_mode_table[OPEN_SSL_CERTIFICATE_AUTH_MODE_MAX] = {
20+
static const char *const certificate_auth_mode_table[_OPEN_SSL_CERTIFICATE_AUTH_MODE_MAX] = {
2121
[OPEN_SSL_CERTIFICATE_AUTH_MODE_NONE] = "no",
2222
[OPEN_SSL_CERTIFICATE_AUTH_MODE_ALLOW] = "allow",
2323
[OPEN_SSL_CERTIFICATE_AUTH_MODE_DENY] = "deny",
2424
[OPEN_SSL_CERTIFICATE_AUTH_MODE_WARN] = "warn",
2525
};
2626

27-
DEFINE_STRING_TABLE_LOOKUP(certificate_auth_mode, int);
27+
DEFINE_STRING_TABLE_LOOKUP(certificate_auth_mode, OpenSSLCertificateAuthMode);
2828

2929
static int tls_write(TLSManager *m, const char *buf, size_t count) {
3030
int r;

0 commit comments

Comments
 (0)