So far my overnight test has worked exactly how I wanted. The process I have set up does this;
-
emonhub.log
is written to/var/log/
- The
emonhub.log
file is monitored bymonit
and when it is over a specified size, monit calls logrotate (not a forced rotate, as the logrotate directives will cause it to do the rotation anyway). - Logrotate is configured to rotate the
emonhub.log
file at the same specified size as above. - A postrotate directive firstly moves the rotated file to the
data
folder on disk. The moved file is then concatenated with any existingemonhub.log
file and deleted. - The
emonhub.log
file grows over a 24hr period. - On a daily basis, the
emonhub.log
file in thedata
folder is rotated and compressed by the daily run of logrotate.
Over a period of time, a number of days of logs for emonhub can be stored for debugging purposes.
Notes:
- This setup is independednt of how the
emonhub.log
file is generated (in this case by modifying the behaviour ofrsyslog
). - This setup is independent of where
/var/log/
is mounted and any other management of that folder. -
monit
is a standard apt package (really useful!). - This setup does not require any changes to the standard setup of
logrotate
ormonit
other than the addition of a configuration file for each. - The length of time logs are retained for can be set in terms of days.
- Setting the size directive reasonably low will minimise data lost on a system crash / reboot, but there are other mechanisms that could mitigate that.
- It could easily be replicated for other logs.
This has been a 36hr test so far. Needs to be left a bit longer. It was built by removing the emoncms changes to logrotate on a standard EmonSD card (plus modifications to the generation of the emonhub.log file
).
Configuration files (currently).
File /etc/monit/conf-enabled/emonhub-monitrc
check file emonhub.log with path /var/log/emonhub.log
if size > 2 MB then exec "/usr/sbin/logrotate -v /etc/logrotate.conf"
File /etc/logrotate.d/emonhub
/var/log/emonhub.log {
rotate 6
daily
size 2M
maxsize 2M
missingok
# create 640 pi pi
copytruncate
notifempty
postrotate
mv /var/log/emonhub.log.1 /home/pi/data/emonhub-log/
cat /home/pi/data/emonhub-log/emonhub.log.1 >> /home/pi/data/emonhub-log/emonhub.log
rm /home/pi/data/emonhub-log/emonhub.log.1
endscript
}
/home/pi/data/emonhub-log/emonhub.log {
rotate 10
daily
compress
missingok
notifempty
}
[Edit 2] Checking out this item re the use of CopyRotate rsyslog with logrotate: reload rsyslog vs copytruncate - Server Fault