Logs with Filebeat, Logstash, Elasticsearch and Kibana
Complex systems require monitoring. An important part of this are the log files. The best way to access, search and view multiple log files is the combination of Filebeat, Logstash, Elasticsearch and Kibana.
However, configuring them can be difficult. Here I am demonstrating a possible setup.
First, some words about each of them, in case you don't know them:
Filebeat is a tool, that watches for file system changes and uploads the file contents to a destination (output). Elasticsearch and Logstash are the most commonly used, Kafka and many others are also supported.
Logstash is a tool for beautifying the logs. It is based on the input-filter-output model. It can convert the log files into a different format, it can add and remove fields, etc.
Elasticsearch is a very famous search engine, based on Lucene. Stores documents inside indices.
Kibana is basically the GUI of Elasticsearch. It provides a user interface for searching and displaying data from indices and managing them.
What to log
In this setup I am demonstrating how to handle the system logs (syslog and auth log). The same can be done for other logs, like nginx, mysql, apache2, etc. and application logs.
Elastic search configuration
The "index rollover" topic was covered in a previous post. I will skip the detailed explanation here.
We will upload the logs to the index alias /system
Create the lifecycle police
The lifecycle below will rollover the index after 1 day or when it gets bigger than 500MB. Additionally it will delete it completely one day after the rollover. This way we keep only the last 2 indices.
Create the index template
Create the first index and alias
Here we configure filebeat to upload the system logs to logstash, when they are changed.
Under /etc/filebeat/modules.d/ you can find multiple module configurations.
Rename "system.yml.disabled" to "system.yml" to enable it. Its contents should look like this:
In order to upload them to logstash, in /etc/filebeat/filebeat.yml configure the output logstash and comment out others:
Logstash identifies the logs as system logs, adds some fields and uploads them
to the index alias in ElasticSearch.
Note that logstash processes the configurations based on the filenames in alphabetical order. In order to create priority, we can use numbers in the beginning of the filename.
First, the input - under /etc/logstash/conf.d/01-beats-input.conf
Second, the filter - under /etc/logstash/conf.d/10-syslog-filter.conf
This will convert the log line to JSON and will add a property "log_type" to it with value "system".
Finally, the output - under /etc/logstash/conf.d/20-elasticsearch-output.conf
The following will send the json log line to elasticsearch. The index/alias name is the value of the field "log_type", e.g. /system.
Index pattern: system-**
When viewing the index, you can filter by "fileset.name" (e.g. "auth" for the auth logs).
I also recommend adding the following columns to the view: host.name, system.auth.message
Finally, you can save the search to access it easier later.
It should look like this: