forked from brablc/postfix-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpflogrep
More file actions
executable file
·83 lines (62 loc) · 1.8 KB
/
pflogrep
File metadata and controls
executable file
·83 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
=head1 NAME
pflogrep -- Postfix log grep complete activity
=head1 SYNOPSIS
pflogrep [-i] PATTERN [FILE]....
=head1 DESCRIPTION
Allows to grep complete activity where PATTERN was found.
Get nice stats of filtered output using pflogsumm:
http://jimsun.linxnet.com/postfix_contrib.html
=head1 AUTHOR
Ondrej Brablc <https://github.com/brablc/>
https://github.com/brablc/postfix-tools
=cut
use Getopt::Long;
($me = $0) =~ s%.*/%%;
$Usage = "
$me [-iv] PATTERN [FILE]....
-i -- ignore case distinctions when matching
-v -- selected lines are those not matching
Examples:
# Get only communication related to one email
pflogrep info\@example.com mail.log | pflogsumm
# Get communication for whole domain - print only from and to lines and color email and status
pflogrep example.com mail.log | grep -e from= -e to= | grep --color -P \\<.*\\>\\|status
";
die $Usage unless &GetOptions( 'i', 'v' ) && (@ARGV >= 1 );
$ptn = shift;
$regex = ( $opt_i ) ? qr/$ptn/io : qr/$ptn/o;
$exstat = 1;
$|++; #turn off buffering for STDOUT
sub handleStream() {
my $fh = shift;
my %P;
my $found = 0;
while (<$fh>) {
next unless /: (([0-9A-Z]{11})|NOQUEUE)/;
$q = $1;
$P{$q} .= $_;
if (/: (removed$|milter-reject:)/ || $q eq 'NOQUEUE') {
$matches = $P{$q} =~ $regex;
if ( ($matches && !$opt_v) || (!$matches && $opt_v)) {
print $P{$q};
$found++;
}
undef $P{$q};
}
}
return $found;
}
if (@ARGV==0) {
$exstat = 0 if &handleStream(\*STDIN);
exit( $exstat );
}
for $f ( @ARGV ) {
unless ( open( INP, '<', $f )) {
warn "Unable to open input file $f: $!\n";
next;
}
$exstat = 0 if &handleStream(\*INP);
close INP;
}
exit( $exstat );