Sub topic 15
Testing forwarding and rotation
This chapter assumes you have already built the pieces: an rsyslog client that forwards over TCP, a central receiver that writes per-host files, and a logrotate stanza for /var/log/remote. You should be comfortable with systemctl, journalctl and sudo. Here you will prove that each piece works, one stage at a time, before you rely on it.
Test in the order the data flows
A log message crosses several stages. A failure at one stage looks the same from the end: nothing shows up on the central host. Testing from the source forward shows you which stage fails.
The checks are:
- Validate the configuration syntax on both hosts.
- Confirm the receiver is listening.
- Send a tagged message from a client.
- Find it on the central server.
- Test rotation with a dry run, then a forced run.
- Test what happens when the server is down.
Step 1: validate the configuration
Run the syntax check before every restart. A typo in a drop-in file can stop rsyslog from loading the whole rule set.
sudo rsyslogd -N1-N1 checks the configuration and exits without starting the daemon. It prints errors with file names and line numbers. Silence, or a final line saying the check is complete, means the syntax is fine. It does not prove the rules do what you intend, so the later steps still matter.
Then restart the service and check its state:
sudo systemctl restart rsyslog
systemctl status rsyslog --no-pagerStep 2: confirm the receiver is listening
On the central server, check that something is bound to TCP port 514:
sudo ss -tlnp | grep ':514'You should see a line owned by rsyslogd. If there is none, the imtcp module did not load or the input is missing. If it is listening but clients cannot connect, check the firewall next. From a client:
nc -vz loghost.example.com 514A successful connection rules out routing and firewall problems. A timeout points at the network or firewall. A "refused" message means the host is reachable but nothing is listening.
Step 3: send a tagged message
The logger command writes to the local syslog socket. The -t option sets a tag, which rsyslog exposes as programname. The receiver uses that value in the file name.
logger -t demo "test message from $(hostname)"Use a distinctive tag such as demo, so the message is easy to find and does not mix with real application logs. To test with a specific priority, add -p:
logger -t demo -p local4.warning "warning-level test from $(hostname)"Check the client first. If the message is not in the local journal, the problem is local and the network is not involved:
journalctl -t demo -n 5 --no-pagerStep 4: confirm arrival on the central server
The receiver writes to /var/log/remote/<hostname>/<programname>.log, so the tag you chose determines the file.
sudo ls /var/log/remote/
sudo tail /var/log/remote/*/demo.logThe directory name comes from %HOSTNAME% in the message. It is whatever the client put in the message header, not necessarily the name DNS gives that machine. If you see a directory named localhost or an unexpected short name, fix the client's hostname before you build retention rules around these paths.
The first message from a new host can take a moment, because the client opens the TCP connection on the first send.
Step 5: test rotation without risk
Always run logrotate in debug mode first. The -d flag implies verbose output and makes no changes to files or to the state file.
sudo logrotate -d /etc/logrotate.d/remoteRead the output. For each matched file, logrotate reports whether it considers rotation necessary. A file that has not reached its daily interval since the last recorded rotation is reported as not needing rotation. That is expected, so it is not an error. Look for these problems:
- A pattern that matches no files. Check the glob against the real directory layout. The
*/*.logpattern needs exactly one directory level. - Permission or ownership warnings, such as insecure parent directory permissions.
- A
postrotatescript that is listed but would fail.
Then force a rotation:
sudo logrotate -f /etc/logrotate.d/remote
sudo ls -l /var/log/remote/*/You should see demo.log.1 next to a fresh demo.log. Because the stanza uses delaycompress, the .1 file stays uncompressed on this first rotation. Compression happens on the next one. Seeing an uncompressed .1 file is correct.
Confirm rsyslog reopened the file
The part people forget is the signal. After rotation, the old file has been renamed, but rsyslog may still hold it open and keep writing to the renamed file. Send another message and see where it lands:
logger -t demo "after rotation"
sudo grep -l "after rotation" /var/log/remote/*/demo.log*The match should be in demo.log, not demo.log.1. If it lands in .1, the HUP did not reach rsyslog. Check that the unit name in the postrotate script is correct and that the script exits successfully.
Step 6: test failure and recovery
Forwarding is only reliable if the queue works. Test it on purpose, in a maintenance window or on a staging pair.
- On the central server, stop the receiver:
sudo systemctl stop rsyslog. - On the client, send several messages:
for i in 1 2 3; do logger -t demo "queued $i"; done. - On the client, look at the rsyslog work directory, usually
/var/spool/rsyslog, for files starting withfwd_queue. - Start the receiver again:
sudo systemctl start rsyslog. - On the server, run
sudo grep queued /var/log/remote/*/demo.log.
All three messages should arrive after the server returns, which shows that action.resumeRetryCount="-1" retries indefinitely. If the queue files never appear or the messages are lost, check that the work directory exists and that rsyslog can write to it.
Quick reference
| Symptom | First check | Likely cause | Column |
|---|---|---|---|
| rsyslog fails to start | `rsyslogd -N1` | Syntax error in a drop-in | |
| Nothing on server | `ss -tlnp \ | grep 514` | `imtcp` not loaded or firewall blocks |
| Wrong directory name | `ls /var/log/remote` | Client sends an unexpected hostname | |
| Logs missing, client side fine | `nc -vz` from client | Network path or port closed | |
| New lines go to `.log.1` | `logrotate -d`, unit name | HUP not delivered | |
| No rotation happens | `-d` output, state file | Interval not reached or pattern empty |
The logrotate state file records the last rotation time for each log. It is /var/lib/logrotate/status on Debian and Ubuntu, and /var/lib/logrotate/logrotate.status on RHEL-family systems. If a file is "not due" and you did not expect that, read it.
Exercise
You force a rotation on the central server and send a new message with logger. It appears in demo.log.1 and demo.log stays empty. What went wrong, and how do you check and fix it?
Show answer
rsyslog still has the renamed file open, so the HUP in postrotate did not take effect. Check that the stanza calls systemctl kill -s HUP rsyslog.service with the right unit name, and run logrotate -d to see whether the script is listed. Check also that the script exits successfully. After fixing it, send a HUP by hand with sudo systemctl kill -s HUP rsyslog.service, send another test message, and confirm it lands in demo.log.
Exercise
Why does the first forced rotation leave demo.log.1 uncompressed even though the stanza has compress?
Show answer
The stanza also has delaycompress, which postpones compression by one cycle. This lets a process that still writes to the old file finish before it is compressed. The .1 file is compressed to .2.gz on the next rotation.