HAProxy Traffic Logs with GoAccess

By chimo on (updated on )
HAProxy is essentially the entry-point for most of the services I run. Its logs are sent to a central rsyslog container on a volume that's accessible by the GoAccess container for parsing. I found most of the configs here.
Architecture
/etc/haproxy.conf:
global
    log         /dev/log local2 # Log to local syslog, which sends a copy to a
                                # remote rsyslog container.
    [...]

frontend main
    [...]
    capture request header Referer len 128
    capture request header User-Agent len 128

    log-format %si:%sp\ %ci\ [%t]\ \"%r\"\ %ST\ %B\ "%hr"
    # %si - your server ip - very useful if you have multiple application
    # %sp - your server port
    # %ci - user ip
    # %t  - datetime in haproxy format
    # %r  - request
    # %ST - status code
    # %B  - data reponse length
    # %hr - captured headers separated by "|" (Referer|User-Agent)
    [...]
rsyslog.conf
# Send haproxy logs to its own file
local2.*    -/var/opt/log/haproxy/haproxy.log

# Receive messages from remote host via UDP
module(load="imudp")
input(
        type="imudp"
        port="514"
)
/etc/goaccess.conf:
time-format %H:%M:%S

date-format %d/%b/%Y

log-format %^ %^ %^ %^ %h [%d:%t.%^] "%r" %s %b "{%R|%u}"

# %^ - skipped token
# %h - user ip
# %d - date-format
# %t - time-format
# %r - request e.g. GET /something
# %s - server status code
# %b - data response length
# %R - referer - very important if you want to know where your users come from
# %u - user agent

# There is so many skipped tokens because my haproxy put some extra information in every line or rsyslog(?)
# Sample line:
#
# Mar 22 09:09:06 server haproxy[PID]: 10.60.10.50:80 1.2.3.4 [22/Mar/2016:09:08:56.989] "POST /UIDL/?v-uiId=0 HTTP/1.1" 200 334 "{https://www.referer.com/|Mozilla/5.0 (Linux; Android 4.4.4; GT-I9060I Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Saf}"

Now I can look at all crawler traffic from the terminal. Yay.

GoAccess Screenshot

I might enable the web version at a later date. Still undecided at the moment.

Recent articles from blogs I follow

The Scunthorpe Problem

I was talking with a friend recently about an email of theirs running afoul (🐔) of another aggressive filter system, because they dared to to talk with someone called Dickson. I know right, they’re the absolute worst. For those unfamiliar, this is the The…

via Rubenerd November 21, 2024

In which Neil is surprised by the lack of an HDMI cable

Some modern technology decisions baffle me. Today, I was sitting in a meeting room. In the room was my friend, with her laptop. Her laptop has an HDMI port. Also in the room was a screen, onto which my friend wished to display her laptop’s desktop. The screen …

via Neil's blog November 19, 2024

Helm: JSON schema generation

Helm charts support the inclusion of a values.schema.json file to validate values.yaml. Documentation: https://helm.sh/docs/topics/charts/#schema-files A JSON schema is akin to defining the structure of and type-annotating a JSON file. It helps to “shift lef…

via not just serendipity November 14, 2024