Splunk Like Grep And Alert Email
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.
$ sudo /usr/sbin/td-agent-gem install fluent-plugin-grepcounter
Next, please install
$ sudo /usr/sbin/td-agent-gem install fluent-plugin-mail
Note: If you installed Fluentd using ruby gems, use
gemcommand instead of
Below shows the full configuration example. You can copy the following content and edit it to suit your needs.
path /var/log/apache2/access.log # Set the location of your log file
count_interval 3 # The time window for counting errors (in secs)
input_key code # The field to apply the regular expression
regexp ^5\d\d$ # The regular expression to be applied
threshold 1 # The minimum number of erros to trigger an alert
add_tag_prefix error_5xx # Generate tags like "error_5xx.apache.access"
@type stdout # Print to stdout for debugging
host smtp.gmail.com # Change this to your SMTP server host
port 587 # Normally 25/587/465 are used for submission
user USERNAME # Use your username to log in
password PASSWORD # Use your login password
enable_starttls_auto true # Use this option to enable STARTTLS
from [email protected] # Set the sender address
to [email protected] # Set the recipient address
subject 'HTTP SERVER ERROR'
message Total 5xx error count: %s\n\nPlease check your Apache webserver ASAP
message_out_keys count # Use the "count" field to replace "%s" above
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 serverand a proper recipient address to run this example.
- The access log file has a proper file permission. You need to makethe file readable to the td-agent/Fluentd daemon.
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
In this way, fluentd now works as an email alerting system that monitors the web service for you.
After saving the configuration, restart the td-agent process:
$ sudo /etc/init.d/td-agent restart
If you installed the standalone version of Fluentd, launch the fluentd process manually:
$ fluentd -c alert-email.conf
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".
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