logrotate is an utility specially designed for ease rotation of large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Normally, logrotate is run as a daily cron job. By default it lacks the configuration file for Asterisk logfiles. Here I’ll suggest an example but you may customize it based on your needs.
Open /etc/logrotate.d/asterisk file with your favorite text editor:
1 2 |
touch /etc/logrotate.d/asterisk vi /etc/logrotate.d/asterisk |
and paste the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/var/log/asterisk/queue_log { daily missingok # log retention time is 7 days rotate 7 notifempty sharedscripts } #/var/spool/mail/asterisk /var/log/asterisk/messages { daily missingok rotate 90 #compress log files to save disk space compress notifempty sharedscripts postrotate /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null endscript } /var/log/asterisk/security { daily missingok rotate 90 #compress log files to save disk space compress notifempty sharedscripts postrotate /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null endscript } #/var/log/asterisk/*_log /var/log/asterisk/full /var/log/asterisk/dtmf { daily missingok rotate 7 #compress log files to save disk space compress notifempty sharedscripts postrotate /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null endscript } /var/log/asterisk/cdr-csv/*csv { missingok rotate 5 monthly } |
Now you can check if it’s working:
1 |
/usr/sbin/logrotate -f /etc/logrotate.conf |
Now check /var/log/asterisk and ensure logfiles have been rotated.
In case you run FreePBX the owner of /var/log/asterisk directory is usually asterisk.asterisk . In this case you will get logrotate error:
error: skipping “/var/log/asterisk/full” because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation.
In this case add su
directive as follows:
1 2 3 4 5 6 |
/var/log/asterisk/dtmf { su asterisk asterisk daily missingok ... } |
Run:
1 |
man logrotate |
to get details about configuration file directives.