How to Parse Syslog Messages
Syslog is a popular protocol that virtually runs on every server. It is used to collect all kinds of logs. The problem with syslog
is that services have a wide range of log formats, and no single parser can parse all syslog
messages effectively.
In this tutorial, we will show how to use Fluentd to filter and parse different syslog
messages robustly.
Prerequisites
A basic understanding of Fluentd
A running instance of
rsyslogd
In this guide, we assume you are running td-agent
on Ubuntu.
Setting Up rsyslogd
rsyslogd
Open /etc/rsyslogd.conf
and append the following line:
Then restart the rsyslogd
service:
This tells rsyslogd
to forward logs to port 5140 to which Fluentd will listen.
Setting up Fluentd
In this section, we will evolve our Fluentd configuration step-by-step.
Step 1: Listening to syslog
Messages
syslog
MessagesFirst, let's configure Fluentd to listen to syslog messages.
Open /etc/td-agent/td-agent.conf
and put the following configuration:
This is the most basic setup: it listens to all syslog messages and outputs them to the standard output.
Now please restart td-agent
:
Let's confirm data is coming in:
Step 2: Extract syslog
Messages from sudo
syslog
Messages from sudo
Now, let's look at a sudo
message like this one:
For security reasons, it is worth knowing which user performed what using sudo
. In order to do so, we need to parse the message field. In other words, we need to extract syslog
messages from sudo
and handle them differently.
For this purpose, we can use the grep
filter plugin. It examines the fields of events, and filter them based on regular expression patterns. In the following example, Fluentd filters out events that come from sudo
and contain command data:
Step 3: Extract Information from Messages
Now let's extract some information from syslog
messages. For this purpose, we use another plugin called filter-parser
. With this plugin, you can parse the content of a field using a regular expression.
Here is the final configuration:
Then restart td-agent
:
Let's execute some comment with sudo
:
Now, you should have a line like this in /var/log/td-agent/td-agent.log
:
There it is, as you can see in the line!
Conclusion
Fluentd makes it easy to ingest syslog
events. You can immediately send data to the output systems like MongoDB and Elasticsearch, but also you can do filtering and further parsing inside Fluentd before passing the processed data onto the output destinations.
If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last updated