Debian/Ubuntu Tips and Tricks

Debuntu

Debian/Ubuntu Tips and Tricks

How-To: Log HAProxy messages only once

Posted by chantra on January 27th, 2012

When enabling logs with HAProxy on a busy web site, hard disk space can quickly become a scarce resource.

The reason is that, most of the time, HAProxy is set to use local0 facility which tend to write logs to a bunch of files in /var/log such as messages...

Thanks to rsyslog, we will be able to canalize those logs to a more appropriate location and only once, saving a bunch of disk space.

This tutorial will go over the steps required to accomplish this set up.

This how-to is based on Debian Lenny, but I believe settings are pretty much the same on the actual stable: Debian Squeeze.

In our setup, we are going to log HAProxy messages in a dedicated directory in order avoid getting too many files in /var/log. Here, I have chosen /var/log/haproxy.

So let's get started and create that directory:

# mkdir /var/log/haproxy

Then, we tell HAProxy to log info and notice messages. this is done by editing /etc/haproxy/haproxy.cfg as follow:

...
global
     log /dev/log   local0 info
     log /dev/log   local0 notice
....
....

Then, we tell rsyslog to catch those messages and write them in a specific file. Create and edit /etc/rsyslog.d/haproxy.conf woth:

if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~

Finally, we need to tell logrotate how to rotate those logs. This is done by Creating and editing /etc/logrotate.d/haproxy with:

/var/log/haproxy/*.log {
        weekly
        missingok
        rotate 7
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                /etc/init.d/haproxy reload > /dev/null
        endscript
}

Which basically rotate the file every week and keep 7 week worth of copies.

Now, everything should be set up properly, we just have to restart rsyslog and haproxy to take those new changes into account:

/etc/init.d/rsyslog reload
/etc/init.d/haproxy reload

That's it, now, your notice and info message from haproxy should be in separate files and only there, not in 4 or 5 of them in /var/log.

6 Responses to “How-To: Log HAProxy messages only once”

  1. I tried the tutorial and did everything correctly, but my haproxy-info and haproxy-notice files are not there. Perhaps it is my haproxy.cfg file? I appreciate the help.

  2. Yes I did. Here is my haproxy.conf file:

    global
    maxconn 25000 # Total Max Connections. This is dependent on ulimit
    daemon
    nbproc 4 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.
    log /dev/log local0 info
    log /dev/log local0 notice
    # haproxy stats socket is available at /var/run/haproxy.stats
    ## stats socket ./haproxy.stats level admin

    defaults
    mode http
    clitimeout 60000
    srvtimeout 30000
    contimeout 4000
    timeout http-request 5s # added to tackle slow http requests
    option httpclose # Disable Keepalive

    # Split static and dynamic traffic since these requests have different impacts on the servers
    ## use_backend bk_web_static if { path_end .jpg .png .gif .css .js }

    ## default_backend bk_web

    listen http_proxy *:80
    #bind *:80
    balance roundrobin # Load Balancing algorithm
    option httpchk
    option forwardfor # This sets X-Forwarded-For
    use_backend bk_web_static if { path_end .jpg .png .gif .css .js }
    default_backend bk_web ##varnishBackend

    # Dynamic part of the application
    backend bk_web ##backend varnishBackend
    ## Define your servers to balance
    balance roundrobin ## change to URL hashing
    cookie MYSRV insert indirect nocache ## check this NOTE TO SELF
    server server1 0.0.0.0:3002 weight 1 maxconn 512 check
    server server2 0.0.0.0:3003 weight 1 maxconn 512 check
    server server3 0.0.0.0:3004 weight 1 maxconn 512 check

    # Static objects
    backend bk_web_static
    balance roundrobin
    server server1 0.0.0.0:3002 weight 1 maxconn 1000 check
    server server2 0.0.0.0:3006 weight 1 maxconn 1000 check
    server server3 0.0.0.0:3007 weight 1 maxconn 1000 check

    and here is my rsyslog.conf file if anything:

    # /etc/rsyslog.conf Configuration file for rsyslog.
    #
    # For more information see
    # /usr/share/doc/rsyslog-doc/html/rsyslog_conf.html
    #
    # Default logging rules can be found in /etc/rsyslog.d/50-default.conf

    #################
    #### MODULES ####
    #################

    $ModLoad imuxsock # provides support for local system logging
    $ModLoad imklog # provides kernel logging support (previously done by rklogd)
    #$ModLoad immark # provides --MARK-- message capability

    # provides UDP syslog reception
    #$ModLoad imudp
    #$UDPServerRun 514

    # provides TCP syslog reception
    #$ModLoad imtcp
    #$InputTCPServerRun 514

    ###########################
    #### GLOBAL DIRECTIVES ####
    ###########################

    #
    # Use traditional timestamp format.
    # To enable high precision timestamps, comment out the following line.
    #
    $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

    # Filter duplicated messages
    $RepeatedMsgReduction on

    #
    # Set the default permissions for all log files.
    #
    $FileOwner syslog
    $FileGroup adm
    $FileCreateMode 0640
    $DirCreateMode 0755
    $Umask 0022
    $PrivDropToUser syslog
    $PrivDropToGroup syslog

    #
    # Where to place spool files
    #
    $WorkDirectory /var/spool/rsyslog

    #
    # Include all config files in /etc/rsyslog.d/
    #
    $IncludeConfig /etc/rsyslog.d/*.conf

    # Save HA- Proxy logs
    local0.* /var/log/haproxy_0.log
    local1.* /var/log/haproxy_1.log

  3. HAProxy's probably provides the finest level of information available for such a product, which is very important for troubleshooting complex environments.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>