Search code examples
linuxlog-rotation

How to get notified by email when logrotate crashes?


For a couple of months I didn't receive all the wanted log files by email upon the weekly log rotation and finally found the cause: two configs inside /etc/logrotate.d were broken, invalid. This on my Ubuntu 15.04 server.

That caused the logrotate service to crash in the middle.

I found that out myself by doing manual log rotations with

$ sudo logrotate -v -f /etc/logrotate.conf

I wish I were notified by email about that. Or to see some log entries about the failed logrotation in /var/log but no, nothing happened.

Do you have an idea how I can configure email alerts when logrotations failed themselves?? Or have these at least logged into something like /var/log/logrotation.log and /var/log/logrotation.err?

Thanks heaps


Solution

  • Setup a daily cron job to run logrotate using '-d'. If echo output is not zero then send an alert email using mailx or whatever you fancy. Sample:

    #!/bin/sh
    
    LOGROTATE_CONFPATH=/etc/logrotate.d
    
    cd $LOGROTATE_CONFPATH
    for i in `ls *`; do
      if ! logrotate -d $i &> /dev/null; then
          mail -s "ERROR IN LOGROTATE FILE:  $i" [email protected] < /dev/null
      fi
    done