Update

As of haproxy-1.6-dev1 it is now possible to send email alerts directly from HAProxy thanks to the excellent work done for us by Simon Horman.
To view more details on how to configure this please see section 3.6 on the HAProxy v1.6 documentation.

The following blog entry was an early option for getting email alerts from HAProxy... Ben also wrote about another 3 work options for email alerts.

In this guide I show a very simple solution to get HAProxy email alerts configured using Logwatch. While the first part is aimed at users of our V7 appliance I think anyone wanting to get email alerts for HAProxy will also find this a good example.

First from the WUI :

  1. Set the external relay(Smart Host) under Edit Configuration > Physical - Advanced Configuration.
    phys-adv-shot
  2. Enable HAProxy Logging under Edit Configuration > Layer 7 - Advanced Configuration.
    l7-adv

Then from the CLI :

  1. Install the logwatch package using yum like so :
[root@lbmaster ~]# yum --disableexcludes=all install logwatch
  1. Create the following file and set your To/From email addresses : /etc/logwatch/conf/logwatch.conf
MailTo = myemailaddress@example.com
MailFrom = LBMaster@example.com

At this point you'll have a standard Logwatch install which will send an email once per day (you may want to disable this).

*To disable the daily logwatch email execute:

chmod -x /etc/cron.daily/0logwatch
  1. Create a custom Layer 7 check

a. Create the following file adding the contents below: /etc/logwatch/conf/logfiles/layer7.conf

LogFile = /var/log/haproxy.log
*OnlyHost
*ApplyStdDate

b. Next create the script: /etc/logwatch/scripts/services/layer7

use strict;
my $find = "is UP|is DOWN";
my @lines = <STDIN>;
for (@lines) {
if ($_ =~ /$find/) {
print "$_\n";
}
}

c. Finally create the following file: /etc/logwatch/conf/services/layer7.conf

Title = "Layer 7 Errors"
LogFile = layer7</code>
  1. Enable the Logwatch job to run every minute with Cron
    a. Edit crontab with the following command:
[root@lbmaster ~]# crontab -e

b. Add the following new lines to root's crontab:

PATH=/usr/sbin/:/bin
*/01 * * * * /bin/nice -n 19 /usr/sbin/logwatch --service layer7 --range '-1 minutes for that minute'

Once this is complete you'll now receive an email in the event of a real server failure. The way this works is that logwatch will run every minute and search the previous minutes log entries for servers that are taken down or brought up during that time.