Sub topic 13
Rotating and sizing the central store
This chapter assumes you already have a working rsyslog receiver writing per-host files under /var/log/remote/. You should also know the basic logrotate directives. Here you will rotate that store safely and estimate how much disk it needs.
Why the central store needs its own plan
A client's logs come from one machine. The central store collects every machine's logs, so three things change.
- Volume. Disk use grows with the number of hosts times the rate per host.
- File count. The dynamic file template creates one file per host and program. A hundred hosts with ten programs each means about a thousand open files.
- Single point of failure. If the log partition fills, the receiver stops writing and you lose logs from every host at once.
The rotation stanza
The receiver writes to /var/log/remote/<host>/<program>.log. One logrotate rule with a two-level wildcard covers all of them:
# /etc/logrotate.d/remote on the central server
/var/log/remote/*/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
sharedscripts
postrotate
/usr/bin/systemctl kill -s HUP rsyslog.service
endscript
}Each directive does a specific job:
| Directive | Effect on the central store |
|---|---|
| `daily` | Rotates each matching file once per day. |
| `rotate 30` | Keeps 30 old generations, so about 30 days of history. |
| `compress` | Compresses old generations with gzip. |
| `delaycompress` | Leaves the newest rotated file uncompressed for one more cycle. |
| `missingok` | Ignores patterns that match nothing, such as a host that was removed. |
| `notifempty` | Skips files that received no messages. |
| `sharedscripts` | Runs `postrotate` once for the whole pattern, not once per file. |
delaycompress and the HUP signal work together. After logrotate renames a file, rsyslog still holds the old file open and keeps writing to it until it receives the signal. The signal makes rsyslog close its files and reopen them by name, which creates fresh files. Delaying compression by one cycle protects against any write that lands in the renamed file just before the reopen.
Without sharedscripts, the HUP would run once per matched file. With a thousand files, that is a thousand restarts of rsyslog's output files in a row.
How rotation flows
The diagram shows the order of events during one rotation.
Estimating the disk you need
Rotation limits history by count, not by bytes. You must work out the size yourself. Use this formula:
disk = hosts × daily_volume × (raw_days + compressed_days ÷ ratio) × headroomraw_daysis 2 withdelaycompress: today's file plus yesterday's uncompressed file.compressed_daysis yourrotatevalue minus 1.ratiois the gzip compression ratio. Syslog text usually compresses between 8:1 and 15:1. Use 8 for a cautious estimate.headroomof 1.5 absorbs spikes and growth.
For example, take 40 hosts that each produce 200 MB per day:
- Daily total: 40 × 200 MB = 8 GB.
- Raw days: 2 × 8 GB = 16 GB.
- Compressed days: 29 × 8 GB ÷ 8 = 29 GB.
- Sum: 45 GB. With 1.5 headroom, plan for about 68 GB.
Do not guess the daily volume. Measure it from real files after a typical day:
# Total size of today's raw files, and the ten largest hosts
sudo du -sh /var/log/remote
sudo du -s /var/log/remote/* | sort -n | tail -10A few hosts usually produce most of the volume. A debug-level application or a failing service in a restart loop can emit gigabytes a day. Finding those hosts early is cheaper than buying disk.
Protecting the filesystem
Put /var/log/remote on its own filesystem or volume. A flood of logs then fills only that volume, not the root filesystem. Also consider these points:
- Size-based rotation. Adding
maxsize 1Grotates a file as soon as it exceeds 1 GB, even before the daily boundary. This works only when logrotate runs. The packaged timer usually runs once a day, so a runaway host can still fill the disk between runs. To catch it sooner, add an hourly timer drop-in forlogrotate.timeror use a cron entry that runs logrotate hourly. - Alerting. Alert on filesystem usage at around 80%. Rotation settings cannot fix a store that has outgrown its disk.
- Different retention per class. Security logs may need a year, while debug logs need a week. Use a second logrotate stanza on a separate path pattern. Make sure the two patterns do not overlap, because logrotate rejects the same file listed twice.
Housekeeping for removed hosts
When you decommission a server, its directory stays. Logrotate eventually deletes the old generations, but the empty directory remains. Prune empty directories now and then:
# Preview first, then delete
sudo find /var/log/remote -mindepth 1 -type d -empty
sudo find /var/log/remote -mindepth 1 -type d -empty -deleteThe -mindepth 1 option keeps the top-level /var/log/remote directory itself.
Checking that rotation works
Run logrotate in debug mode first. It prints what it would do and changes nothing:
sudo logrotate -d /etc/logrotate.d/remoteRead the output for lines such as rotating pattern: /var/log/remote/*/*.log after 1 days (30 rotations). If the pattern matches no files, check the path and the template output. Then force one rotation and confirm that rsyslog is still writing to fresh files:
sudo logrotate -f /etc/logrotate.d/remote
sudo ls -l /var/log/remote/*/ | head
logger -t demo "after rotation $(date +%s)"
sudo tail -n 1 /var/log/remote/*/demo.logYou should see new, small .log files and rotated .log.1 files. If new messages appear in .log.1, the HUP did not reach rsyslog. Check that the unit name in postrotate matches systemctl status rsyslog.
Common failures
- Disk fills despite
rotate 30. Compare actual daily volume with your estimate. Also look for a single file that grows past the whole day's budget. - Logs keep appearing in the rotated file. The HUP is missing, failed, or sent to the wrong unit. Run
sudo journalctl -u logrotate.serviceto see script errors. - "Duplicate log entry" error. Two stanzas match the same file. Narrow one of the patterns.
- Rotation never happens. Check that
logrotate.timeris active withsystemctl list-timers logrotate.timer. Also read/var/lib/logrotate/status, which records when each file was last rotated.
Exercise
A central server receives 25 hosts at about 120 MB per day each. You use daily, rotate 14, compress and delaycompress, and you expect an 8:1 compression ratio. Estimate the disk needed with 1.5 headroom, and name the one directive that makes the newest rotated file stay uncompressed.
Show answer
Daily total is 25 × 120 MB = 3 GB. Raw days are 2 × 3 GB = 6 GB. Compressed days are 13 × 3 GB ÷ 8 ≈ 4.9 GB. The sum is about 10.9 GB, and with 1.5 headroom you need about 16.4 GB, so plan for roughly 17 to 20 GB. The directive is delaycompress.