GoAccess with rotating compressed web logs

GoAccess is a great tool for real-time web logs, but I recently ran into a bit of snag when trying to use it on a server with custom log rotation. I wanted GoAccess to process all of my web server logs, but continue to have the logs rotate on a daily basis, with compression on logs older than 1 day:

To accomplish this, I created a systemd service for the process, under /etc/systemd/system/goaccess.service:

[Unit]
Description=Goaccess Web log report.
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
User=root
Group=root
Restart=always
RestartSec=1800
ExecStart=/usr/local/bin/goaccessreport
StandardInput=tty
StandardOutput=tty
StandardError=tty
TTYPath=/dev/tty7

[Install]
WantedBy=multi-user.target

And my /usr/local/bin/goaccessreport:

#!/bin/bash
zcat -f /srv/$DOMAIN/public/logs/*access.log.* | goaccess /srv/$DOMAIN/public/logs/*access.log - -o /srv/logs.$DOMAIN/public/htdocs/index.html --exclude-ip=$SERVER_IPV4 --exclude-ip=$SERVER_IPV6 --ignore-crawlers --log-format=COMBINED --real-time-html

Log rotation runs out of /etc/cron.daily at around 07:00. Each time it runs, GoAccess will cease processing abruptly, hence the use of RestartSec=1800 in the service file above. This stops us hitting the 5 restart attempt threshold in systemd as it provides ample time for log rotation to complete.

We end up with log processing that’ll continue to update itself, no matter the state of the underlying logs; compressed or uncompressed, as well as throughout rotation.

Leave a Reply

Your email address will not be published. Required fields are marked *