Mail logs files as attachments with sendmail

log-fileslogrotatesendmail

I'm using logrotate to rotate my log files.

I've set it up to create new logfiles every week.

However I would like it to first mail me the logfiles as attachments.

The /var/log/httpd file currently contains:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    firstaction
        {I think I need to mail the logfiles here???}
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

Is it possible to do what I want?

If so: what do I need to add to firstaction for it to:

  • mail me the logfiles (/var/log/httpd/*log) as attachment using sendmail
  • add some subject to the mail, e.g.: HTTPD logfiles {week #}

Best Answer

If I understand your question correctly, there's a logrotate option that will do what you want. Simply add mail user@yourdomain.com as one of the configuration options, like this:

/var/log/httpd/*log {
    mail user@yourdomain.com
    ...

This will mail you the log files that are about to be rotated.

Perhaps you've tried this and it didn't quite do what you need. In that case, I think you're on the right track for the most part. However, invoking sendmail directly isn't the right approach when you're trying to send a message with attachments. To quote the sendmail FAQ:

How do I create attachments with sendmail?

You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachments.

If you have the mail user agent mutt (or can install it), it should be able to do what you want. Try something like this:

/var/log/httpd/*log {
    firstaction
       echo | mutt -s "Log files for `date`" user@yourdomain.com -a /var/log/httpd/*log
    endscript
    ...

Note also in your example configuration file you're missing the endscript for your firstaction command.

Related Topic