Keeping your server’s log files in check is a task that can’t be overlooked. In this guide, we’ll delve into using logrotate
for log management, and integrate it with Matomo for log analytics, along with managing Nginx service restarts post-rotation.
Understanding logrotate is essential. This tool helps in managing the growth of log files on Linux systems. But we’ll add a twist — forwarding logs to Matomo for analysis before rotation, and ensuring Nginx gets a fresh start with the new log files.
Act 1: The Setup
Make sure logrotate is installed on your system. If not, install it via your package manager. In Arch Linux, it’s a simple command away:
sudo pacman -Syu logrotate
Now, let’s dive into configuring logrotate with Matomo integration and Nginx restarts. We’ll assume you have a log file, access.log
, that you want to manage.
Matomo integration is done via the Matomo Log Analytics script: https://github.com/matomo-org/matomo-log-analytics/#readme
Create a logrotate configuration file:
/etc/logrotate.d/nginx:
/path/to/access.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 640 username groupname
prerotate
# Send log file to Matomo for analysis
python path/to/import_logs.py --url=matomo.example.com /path/to/access.log
endscript
postrotate
# Reload Nginx to apply changes
systemctl reload nginx
endscript
}
Replace /path/to/access.log
, username
, groupname
, and path/to/import_logs.py
with actual paths and credentials.
Act 2: The Automation
Automation is a breeze with logrotate. It’s typically run via a daily cron job. To ensure this is the case on your system:
sudo systemctl enable cronie.service
sudo systemctl start cronie.service
These commands will activate daily checks and rotations as per your configuration.
Act 3: Testing and Execution
Before fully automating, testing your setup is a good practice:
sudo logrotate -d /etc/logrotate.d/myapp
This command runs logrotate in debug mode, showing what would happen without actually rotating the logs.
Once you’re confident in your setup, let it run. Logrotate, along with your custom prerotate and postrotate scripts, will manage your log files, keep Matomo updated with the latest logs, and ensure Nginx is always working with fresh log files.
FIN
Log management, when automated intelligently, can save countless hours and prevent potential issues. Integrating Matomo adds a layer of analytics, turning your logs into insightful data. If you have any further questions, insights, or need clarifications, feel free to reach out.
Keep your logs rotating and your data insightful. Bithive out!
There is no comment system. If you want to contact me about this article, you can do so via e-mail or Mastodon.