Adding a hostname to Logrotate email subjects

I have logrotate running on my virtual servers ( both local and Amazon EC2 based ) and configured to send myself all log files by email daily. They are all running virtually the same configuration, and this makes it difficult to work out which log file is tied to which machine. The simplest way to fix this, is to ensure all logrotate emails append the hostname of the machine it is sending from within the subject of the email.

From looking at the logrotate documentation, there doesn’t seem to be any obvious way to do this, fortunately however, you can specify your own mail program when running logrotate. As logrotate runs on the cron, simply add this script somewhere ( i use /usr/local/bin/logrotatemail.sh ):

#! /bin/bash

/bin/mail $1 "`hostname`: $2" $3

Then modify the logrotate cron file ( in /etc/cron.daily/logrotate.cron on Gentoo ) to have the extra “-m” option:

#! /bin/sh

/usr/sbin/logrotate -m /usr/local/bin/logrotatemail.sh /etc/logrotate.conf

Log rotated log files should now appear in your inbox prefixed with the hostname of the server sending it. With this, you can now setup mail filters to organize the mails on a per-server basis.