Amavis TIMING csv

From Federal Burro of Information
Jump to navigationJump to search

This script will take an amavis log , extract TIMING data, and print a csv.

Propably not fit for LOTS and LOTS of data.

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

# Sep 17 15:17:22 amazon amavis[7316]: (07316-01) TIMING [total 25758 ms] - ldap-prepare: 7 (0%)0, SMTP greeting: 4 (0%)0, SMTP EHLO: 2 (0%)0, SMTP pre-MAIL: 2 (0%)0, mkdir tempdir: 1 (0%)0, create email.txt: 1 (0%)0, ldap-connect: 69 (0%)0, lookup_ldap:
2 (0%)0, SMTP pre-DATA-flush: 2 (0%)0, SMTP DATA: 2 (0%)0, check_init: 1 (0%)0, digest_hdr: 2 (0%)0, digest_body_dkim: 1 (0%)0, gen_mail_id: 2 (0%)0, mkdir parts: 2 (0%)0, mime_decode: 18 (0%)1, get-file-type2: 30 (0%)1, decompose_part: 2 (0%)1, decompose
part: 2 (0%)1, parts_decode: 0 (0%)1, check_header: 2 (0%)1, AV-scan-1: 21 (0%)1, spam-wb-list: 3 (0%)1, SA parse: 8 (0%)1, SA check: 25422 (99%)99, update_cache: 7 (0%)100, decide_mail_destiny: 3 (0%)100, fwd-connect: 8 (0%)100, fwd-mail-pip: 8 (0%)100,
wd-rcpt-pip: 0 (0%)100, fwd-data-chkpnt: 0 (0%)100, write-header: 1 (0%)100, fwd-data-contents: 0 (0%)100, fwd-end-chkpnt: 81 (0%)100, prepare-dsn: 1 (0%)100, main_log_entry: 11 (0%)100, SMTP pre-response: 0 (0%)100, SMTP response: 0 (0%)100, unlin...
# Sep 17 15:17:22 amazon amavis[7311]: (07311-02) TIMING [total 30106 ms] - SMTP greeting: 4 (0%)0, SMTP EHLO: 3 (0%)0, SMTP pre-MAIL: 4 (0%)0, lookup_ldap: 22 (0%)0, lookup_ldap: 9 (0%)0, lookup_ldap: 18 (0%)0, SMTP pre-DATA-flush: 1 (0%)0, SMTP DATA: 2
0%)0, check_init: 0 (0%)0, digest_hdr: 4 (0%)0, digest_body_dkim: 1 (0%)0, gen_mail_id: 1 (0%)0, mime_decode: 23 (0%)0, get-file-type2: 25 (0%)0, parts_decode: 0 (0%)0, check_header: 3 (0%)0, AV-scan-1: 5 (0%)0, spam-wb-list: 7 (0%)0, SA parse: 8 (0%)0, S
 check: 29842 (99%)100, update_cache: 8 (0%)100, decide_mail_destiny: 2 (0%)100, fwd-connect: 9 (0%)100, fwd-mail-pip: 7 (0%)100, fwd-rcpt-pip: 0 (0%)100, fwd-data-chkpnt: 0 (0%)100, write-header: 2 (0%)100, fwd-data-contents: 1 (0%)100, fwd-end-chkpnt: 8
 (0%)100, prepare-dsn: 1 (0%)100, main_log_entry: 11 (0%)100, SMTP pre-response: 0 (0%)100, SMTP response: 0 (0%)100, unlink-2-files: 0 (0%)100, rundown: 1 (0%)100
# Sep 17 15:17:24 amazon amavis[7301]: (07301-03-12) TIMING [total 29191 ms] - lookup_ldap: 21 (0%)0, SMTP pre-DATA-flush: 1 (0%)0, SMTP DATA: 21 (0%)0, check_init: 0 (0%)0, digest_hdr: 10 (0%)0, digest_body_dkim: 14498 (50%)50, gen_mail_id: 3 (0%)50, mim
_decode: 13 (0%)50, get-file-type1: 25 (0%)50, parts_decode: 0 (0%)50, check_header: 2 (0%)50, AV-scan-1: 58 (0%)50, spam-wb-list: 2 (0%)50, SA parse: 8 (0%)50, SA check: 14389 (49%)100, update_cache: 9 (0%)100, decide_mail_destiny: 1 (0%)100, fwd-connect
 6 (0%)100, fwd-mail-pip: 4 (0%)100, fwd-rcpt-pip: 0 (0%)100, fwd-data-chkpnt: 0 (0%)100, write-header: 1 (0%)100, fwd-data-contents: 2 (0%)100, fwd-end-chkpnt: 102 (0%)100, prepare-dsn: 1 (0%)100, main_log_entry: 10 (0%)100, SMTP pre-response: 0 (0%)100,
SMTP response: 0 (0%)100, unlink-1-files: 0 (0%)100, rundown: 1 (0%)100

my $data;
my $fields;
my $verbose = 0;

while (<>){
    #       Month     date          hour minute second
    if ( /([\w]{3})\s+([\d]+)\s([\d]{1,2}:[\d]{1,2}:[\d]{1,2})\s+([\w]+)\s+([\w]+)\[(\d+)\]:\s+\(([\d]+\-[\d]{2})\)\sTIMING\s\[total\s(\d+)\sms\] - (.+)$/ ) {
    $verbose && print ;
    chomp();
        my $amavisid = $7;
        # $data->{$amavisid}->{'totaltime'}= $8;
        if ( $verbose ) {
        print "\tMonth ".$1."\n";
        print "\tdate ".$2."\n";
        print "\ttime ".$3."\n";
        print "\thost ".$4."\n";
        print "\tprocess ".$5."\n";
        print "\tpid ".$6."\n";
        print "\tamavisid ".$7."\n";
        print "\ttotaltime ".$8."\n";
        print "\trest ".$9."\n";
        }
        my @parts = split( ', ', $9 ) ;
        for ( @parts ) {
            $verbose && print ;
            if ( /(.+):\s([\d]+)\s/ ) {
                if ( length($1) > 0 ) {
                    $data->{$amavisid}->{$1}= $2;
                    $fields->{$1} = 1;
                }
            }
        }
        # print "Parts ".@parts."\n";
    }
}

print "amavisid,".join "," , sort { $a cmp $b } keys(%$fields);
print "\n";

foreach my $amavisid (keys %{ $data }) {
    print $amavisid.", ";
    foreach my $field ( sort { $a cmp $b } keys(%$fields) ) {
            if ( $data->{$amavisid}->{$field} ) {
                print $data->{$amavisid}->{$field}.",";
            } else {
                print "0,";
            }

    }
    print "\n";
}