How to create a summary of frozen messages from the Exim log

Linux administrators using Exim most probably already know the "Message is frozen" warning in the Exim logs. Messages with failed delivery remain in the message queue until you do something about them. Here's a way for making a semi-colon separated list of some headers of these mails.

On Debian the main Exim logfile can be found at "/var/log/exim/mainlog". You should go to that dir and run the following:
cat mainlog | grep -i 'Message is frozen' | tr -s " " | cut -d\  -f 3 | sort | uniq > frozen_messageids.txt

This will produce a list of ids of the frozen messages in your queue.

Next run this:
input=frozen_messageids.txt
output=frozen_messages.txt

echo "Message-Id;Date;From;To;Subject" > ${output}
for i in `cat ${input}`; do
  echo -n "${i};"
  exim -Mvh ${i} | awk '
/^.*From:/    { f_from = getarg("From:"); }
/^.*Date:/    { f_date = getarg("Date:"); }
/^.*To:/      { f_to = getarg("To:"); }
/^.*Subject:/ { f_subject = getarg("Subject:"); }
END { print f_date ";" f_from ";" f_to ";" f_subject }
function getarg(str) {
  l_str = tolower(str);
  found = 0;
  ret = "";
  for (i = 2; i <= NF; i++) {
    if (found == 1) ret = ret " " $i;
    if (tolower($i) == l_str) found = 1;
  }
  if (length(ret) > 0) ret = substr(ret, 2);
  return ret;
}'
done >> ${output}

This will make a semi-colon separated list containing the message-id, the date, the sender, the recipient and the subject of each frozen mail. You can open this file in most spreadsheet-processors (eg. MS Excel, OpenOffice, etc.) and review the results easily. You can then use the filtering abilities of your spreadsheet program to get the important messages that you should act upon personally.

You can remove a frozen message from the queue by issuing the following command:
exim -Mrm <messageid>

Or you can do a mass-removal by running this command for all ids in the previously collected list (but first you should look it through to make sure nothing important gets deleted):
for i in `cat frozen_messageids.txt`; do exim -Mrm ${i}; done

If you are curious about the complete header (or body) of the messages, then use the following commands:
  • exim -Mvh <messageid> for displaying the headers
  • exim -Mvb <messageid> for displaying the body of the message