Perl Notes/processing time.pl

From Federal Burro of Information
Revision as of 19:34, 11 January 2012 by David (talk | contribs) (Created page with "<pre> #!/usr/bin/perl -w use strict; use Time::Local; my $DEBUG = 0; # This script will tell you how long each message took to process so that you can figure out what's taking ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#!/usr/bin/perl -w

use strict;
use Time::Local;

my $DEBUG = 0;
# This script will tell you how long each message took to process so that you can figure out what's taking so long.

# Mar 18 04:02:33 amazon postfix/smtpd[6001]: D675E1E4184: client=esa-annu.mail.uoguelph.ca[131.104.91.36]
# Mar 18 04:02:33 amazon postfix/cleanup[29358]: D675E1E4184: message-id=<702030269448694818992@LENOVO-3C887B0D>
# Mar 18 04:02:33 amazon postfix/qmgr[17756]: D675E1E4184: from=<mail@prophecy.info>, size=22995, nrcpt=1 (queue active)
# Mar 18 04:02:36 amazon postfix/smtp[9802]: D675E1E4184: to=<jonesth@uoguelph.ca>, orig_to=<thomas.jones@guelphhumber.ca>, relay=127.0.0.1[127.0.0.1]:10024, delay=2.7, delays=0.06/0/0.07/2.5, dsn=2.0.0, status=sent (250 2.0.0 Ok, id=08738-03, from MTA([127.0.0.1]:10025): 250 2.0.0 Ok: queued as 6FEE71E418E)
# Mar 18 04:02:36 amazon postfix/qmgr[17756]: D675E1E4184: removed

my %month = (   'Jan' => '0', 'Feb' => '1', 'Mar' => '2', 'Apr' => '3', 'May' => '4', 'Jun' => '5', 'Jul' => '6', 'Aug' => '7', 'Sep' => '8', 'Oct' => '9', 'Nov' => '10', 'Dec' => '11' );

# Woo dirty!
my $year = "2011";

my %messages ;
my $total = 0;
my $failed = 0 ;
my $parsed = 0 ;

while (<>) {
        chop;
        $total++;

        if ( /(\w+)\s{1}(\d{2})\s{1}(\d{2}):(\d{2}):(\d{2})\s{1}(\w+)\s{1}postfix\/(\w+)\[\d+\]:\s{1}(\w+):(.+)/ ){
        #     Month     day         hh:     mm:     ss          host               process           messid mess
                # A log line!
                my $mon  = $1; my $day     = $2;
                my $hour = $3; my $min     = $4; my $sec    = $5;
                my $host = $6; my $process = $7; my $messid = $8; my $mess = $9;

                $parsed++;
                $DEBUG && print "Mon $mon Day $day Hour $hour Min $min Sec $sec Host $host Process $process ID $messid Mess $mess\n";
                my $epoch = timelocal($sec,$min,$hour,$day,$month{$mon},$year);
                $DEBUG && print "Epoch $epoch Time ".localtime($epoch)."\n";

                if ( $messages{$messid} && ( $mess =~ /removed/ ) ) {
                        $messages{$messid} = $epoch - $messages{$messid};
                } elsif ( ! $messages{$messid} ) {
                        $messages{$messid} = $epoch;
                } else {
                        $DEBUG && print "Mid message message\n";
                }

        } elsif ( /(\w+)\s{1}(\d{2})\s{1}(\d{2}):(\d{2}):(\d{2})\s{1}(\w+)\s{1}postfix\/(\w+)\[\d+\]:\s{1}(.+)/ ){
                # Mar 18 04:02:14 amazon postfix/smtpd[10247]: connect from localhost.localdomain[127.0.0.1]
                $DEBUG && print "CONNECT: ".$_ ."\n";
        } else {
                $failed++;
                $DEBUG && print "FAILED: ". $_."\n"
        }

}

print "Total $total\nparsed $parsed\nFailed $failed\n";
print "Value Message\n";
foreach my $value (sort {$messages{$a} <=> $messages{$b} } keys %messages)
{
     print "$value $messages{$value}\n";
}