Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I don't know if there are any other Exim (www.exim.org) users here, but I am trying to develop a script which will summarise the ammount of emails sent/recieved in a day/week/month etc..

In the exim logs the entries are like so:
2002-01-05 07:56:49 16MqNV-0001Ai-00 <= localuser@server.com U=localus +er P=local S=524 2002-01-05 07:56:55 16MqNV-0001Ai-00 => remote.user@domain.com R=looku +phost T=remote_smtp H=mx1.mail.domain.com [xx.xx.xx.xx] 2002-01-05 07:56:55 16MqNV-0001Ai-00 Completed
As you can see, the above email was sent from a local user, what would really like to do is monitor these and produce some statistics - I'm really unsure how to approach this and whether it would require a running daemon

Any thoughts on whether this is possible would be appreciated

Replies are listed 'Best First'.
Re: Log Monitor
by Masem (Monsignor) on Jan 05, 2002 at 18:53 UTC
    A user of exim myself, one nice thing is that all messages will carry a unique ID (the 16MqNV.. above), and this can be used to parse through the log once and then spit out stats.

    The best way to do this , in this case is similar to the following, in which you split each line, look for the unique ID, and push the rest into a hash of arrays.

    my %mailhash; while (<LOG>) { my( $date, $time, $message, @rest ) = split /\s+\; if ( $message ~= /^\w{6}\-\w{6}\-\w{2}$/ ) { $mailhash{ $message } ||= []; push @{ $mailhash{ $message } }, join(" ", @rest); } }
    Note that you can also collect statistics at this point, but because it sounds like to want to do this while the log is active, you ought to read through the log as fast as possible, then proccess after that step. Doing the stats while reading may be problematic.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      you might want to throw the date and time back in there, as well

      push @{ $mailhash{ $message } }, join(" ", $date, $time, @rest);
      excellent point about separating the reading from the processing.

      ~Particle