#!/usr/bin/perl -w use strict; use Date::Calc qw(Delta_YMDHMS); # needed to calc offset of date ############################################################################# # User config section. # You might have to alter some of these. my $qidPattern = "[A-F0-9]+"; # system-dependent based on file inode; I've seen 9-12 digits. my $minutesPast = 45; # Number of minutes in the past the first QID will be looked for. my $relayAddress = "\[127\.0\.0\.1\]"; # Defaults to localhost. my $pflogsumm = "/usr/local/bin/pflogsumm"; # Name + path of pflogsumm #my $errorFile = "error_log"; my $tempFile = "temp_mail_file"; # Name of new mail log file to run pflogsumm on. # # ###################################################################### my %qid; my %monthNums = qw( Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6 Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12); # Reads in log file, writes out new file with only the word 'postfix' in it, and makes a note of the # original message ids and the date/time. my $inputLog = pop @ARGV; open IN, "<$inputLog" or die "Can't open file!\n"; #open STDERR, ">$errorFile" or die "Can't open error log!\n"; #select STDERR; my @tempArr; my ($month, $date, $hour, $min, $sec, $queueNo); my $yearnow = (localtime)[5] + 1900; # reads last arg on command line. while() { # Remove all lines that don't have postfix in them. if(/postfix/ || /vmailer/) { if(/^(...) +(\d{1,2}) (..):(..):(..) .* ($qidPattern):.*relay=\S+$relayAddress.*queued as ($qidPattern)/) { # store first QID as key, requeue QID + date as value #print "RELAY LINE: $_"; if(exists $qid{$6}) { # Add second date only if requeing QID is separate -- one email going to multiples. @tempArr = split /,/, $qid{$6}; if(shift (@tempArr) ne $7) { $qid{$6} .= "," . "$yearnow $monthNums{$1} $2 $3 $4 $5"; } } else { $qid{$6}="$7,$yearnow $monthNums{$1} $2 $3 $4 $5"; } } # print TEMPLOG "$_"; } } close IN; #foreach $key (keys %qid) { # print "ID $key links to $qid{$key}\n"; #} #exit 0; open IN, "<$inputLog" or die "Can't open input file\n"; open OUT, ">$tempFile" or die "Can't open file!\n"; my $skipline; while() { #if(/postfix\/cleanup/) { # print OUT "$_"; # next; # don't need to alter cleanup lines. #} unless(/postfix/ || /vmailer/) { next; } if(($month, $date, $hour, $min, $sec, $queueNo) = /^(...) +(\d{1,2}) (\d\d):(\d\d):(\d\d).* ($qidPattern): /) { if(exists $qid{$queueNo}) { @tempArr = split /,/, $qid{$queueNo}; shift (@tempArr); # dumps reque QID. my $timeline; foreach $timeline (@tempArr) { if(&qidWithinTimeLimit($monthNums{$month}, $date, $hour, $min, $sec, $timeline)) { $skipline = 1; last; # if date is in valid range, skip printing this line. } else { #print STDERR "NOT SKIPPING LINE FOR TIME REASONS! $_"; } } # foreach } } if($skipline) { $skipline = 0; } else { print OUT $_; } } # while close IN; close OUT; #$| = 1; #select STDOUT; print `$pflogsumm @ARGV $tempFile`; # delete files when finished... unlink "$tempFile"; # END. sub qidWithinTimeLimit { my ($month, $date, $hour, $min, $sec, $relayDate) = @_; #print "Month: $month Date: $date Hour: $hour Min: $min Sec: $sec QID: $qidDate\n"; my @DateArray = split / /, $relayDate; #print "year in qidWithinTimeLimit is @DateArray\n"; my ($D_y, $D_m, $D_d, $D_h, $D_min, $D_s) = Delta_YMDHMS( $yearnow, $month, $date, $hour, $min, $sec, shift (@DateArray), shift (@DateArray), shift (@DateArray), shift (@DateArray), shift (@DateArray), shift (@DateArray), ); # return true if requeing within $minutesPast if($D_y < 1 and $D_m < 1 and $D_d < 1 and $D_min < $minutesPast) { 1; } else { #print STDERR "NO SKIP!\n"; #print STDERR "Delta Year: $D_y Month: $D_m Date: $D_d Hour: $D_h Min: $D_min Sec: $D_s\n"; 0; } }