I have Machine A, which is a pfsense installation, that sends logs via syslog to a Ubuntu box. The Ubuntu Box will have to rewrite the logs, to replace for instance host names and change format a bit.
The format is generally as follows
Mar 7 00:05:32 hostname service: field1 field2 field3 field4 field5 field6 field7
I would like the possibility of rewriting hostname, service and change order of fields, and filter out messages with a certain value in a certain field, as they are not interesting.
After filtering and handling, the messages should be written to disk in a log file, and sent to another machine via syslog.
Now, the logging part is trivial - simply set up rsyslogd to accept incoming messages, and forward those. However, I'm a bit stuck on the rewriting part. I'm not married to rsyslogd; any syslog-esque daemon will do.
The question is a bit vague but I'll try to propose a possible solution. To rewrite messages
rsyslog
provides a number of modules one of which ismmfields
. It splits an incoming message at a certain character (just one character) into fields and then allows to access these fields. For a message likethe separator would be a blank
and the fields are then accessible as
$!f2
,$!f3
,$!f4
, and$!f5
. Unfortunately, the very first field ($!f1
) is always empty because the message is preceeded by a space and that would be the first field. Thus, for the above message we get$!f1==""
,$!f2=="a=1"
,$!f3=="b=two"
,$!f4=="c=3"
, and$!f5=="d=four"
.rsyslog
ships with other message modification modules as well but in lack of further details I chose this one. Store the following file as/etc/rsyslog.d/10-so.conf
. Change the name according to the desired order of execution but keep the.conf
extension.Restart
rsyslog
(viasudo systemctl restart rsyslog.service
) and try it out:The output of the second
logger
statement will be:To change the hostname, simply replace
in the templates with
To change the syslogtag (what you called service), replace
with
The output will then be:
See here for further message properties.
Note that my approach (with
mmfields
) relies on the fields to always have the same order and does not easily allow for rewriting messages likea=1 b=2
tob=1 a=2
(reorder and change key-value-pairs). For that, another module might be more appropriate.As far as i know this could be achieved by using logstash elasticsearch and Kibana. I am trying to do the same and had been more or less successful by setting up an elk stack. Then using grok filters in logstash to break the syslog message into different fields and use those to match a pattern and send alerts out. Take a look at this guide it might give you some answers on where to begin.
This kind of setup already has built in filters for things like mysql logs or apache or nginx logs to begin with. Here is good overview of the elk stack features and architecture. I hope this helps.