Fluentd
Search…
0.12
Powered By GitBook
Splunk Like Grep And Alert Email
Splunk is a great tool for searching logs. One of its key features is the ability to "grep" logs and send alert emails when certain conditions are met.
In this little "how to" article, we will show you how to build a similar system using Fluentd. More specifically, we will create a system that sends an alert email when it detects a 5xx HTTP status code in an Apache access log.
If you want a more general introduction to use Fluentd as a free alternative to Splunk, see the article "Free Alternative to Splunk Using Fluentd".

Installing the requisites

Install Fluentd if you haven't yet.
Please install fluent-plugin-grepcounter by running:
1
$ sudo /usr/sbin/td-agent-gem install fluent-plugin-grepcounter
Copied!
Next, please install fluent-plugin-mail by running:
1
$ sudo /usr/sbin/td-agent-gem install fluent-plugin-mail
Copied!
Note: If you installed Fluentd using ruby gems, use gem command instead of td-agent-gem.

Configuration

Full configuration example

Below shows the full configuration example. You can copy the following content and edit it to suit your needs.
1
<source>
2
@type tail
3
path /var/log/apache2/access.log # Set the location of your log file
4
format apache2
5
tag apache.access
6
</source>
7
8
<match apache.access>
9
@type grepcounter
10
count_interval 3 # The time window for counting errors (in secs)
11
input_key code # The field to apply the regular expression
12
regexp ^5\d\d$ # The regular expression to be applied
13
threshold 1 # The minimum number of erros to trigger an alert
14
add_tag_prefix error_5xx # Generate tags like "error_5xx.apache.access"
15
</match>
16
17
<match error_5xx.apache.access>
18
@type copy
19
<store>
20
@type stdout # Print to stdout for debugging
21
</store>
22
<store>
23
@type mail
24
host smtp.gmail.com # Change this to your SMTP server host
25
port 587 # Normally 25/587/465 are used for submission
26
user USERNAME # Use your username to log in
27
password PASSWORD # Use your login password
28
enable_starttls_auto true # Use this option to enable STARTTLS
29
from [email protected] # Set the sender address
30
to [email protected] # Set the recipient address
31
subject 'HTTP SERVER ERROR'
32
message Total 5xx error count: %s\n\nPlease check your Apache webserver ASAP
33
message_out_keys count # Use the "count" field to replace "%s" above
34
</store>
35
</match>
Copied!
Save your settings to /etc/td-agent/td-agent.conf (If you installed Fluentd without td-agent, save the content as 'alert-email.conf' instead).
Before proceeding, please confirm:
    The SMTP configuration is correct. You need a working mail server
    and a proper recipient address to run this example.
    The access log file has a proper file permission. You need to make
    the file readable to the td-agent/Fluentd daemon.

How this configuration works

The configuration above consists of three main parts:
    1.
    The first <source> block sets the httpd log file as an event source for the daemon.
    2.
    The second <match> block tells Fluentd to count the number of 5xx responses per time window (3 seconds). If the number exceeds (or is equal to) the given threshold, Fluentd will emit an event with the tag error_5xx.apache.access.
    3.
    The third <match> block accepts events with the tag error_5xx.apache.access, and send an email to [email protected] per event.
In this way, fluentd now works as an email alerting system that monitors the web service for you.

Test the configuration

After saving the configuration, restart the td-agent process:
1
$ sudo /etc/init.d/td-agent restart
Copied!
If you installed the standalone version of Fluentd, launch the fluentd process manually:
1
$ fluentd -c alert-email.conf
Copied!
Then generate some 5xx errors in the web server. If you do not have a convenient way to accomplish this, appending 5xx lines to the log file manually will produce the same result.
Now you will receive an alert email titled "HTTP SERVER ERROR".

What's next?

Admittedly, this is a contrived example. In reality, you would set the threshold higher. Also, you might be interested in tracking 4xx pages as well. In addition to Apache logs, Fluentd can handle Nginx logs, syslogs, or any single- or multi-lined logs.
You can learn more about Fluentd and its plugins by
If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is a open source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 2yr ago