Skip to content

Instantly share code, notes, and snippets.

@SergeyStorm
Created February 19, 2017 12:30
Show Gist options
  • Save SergeyStorm/244c60759094a5abca8cdd19bb4b3341 to your computer and use it in GitHub Desktop.
Save SergeyStorm/244c60759094a5abca8cdd19bb4b3341 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Script to monitor top senders via your exim server in realtime
use strict;
no warnings;
use Text::Table;
# Default amount of top senders, may be overriden by commandline argument
my $default_top = 40;
# Path to exim main.log
my $logfile = '/var/log/exim/main.log';
my $top;
if ( $ARGV[0] && $ARGV[0] =~ /\d+/ ) {
$top = $ARGV[0];
} else {
$top = $default_top;
}
my $matches = 0;
my %domains;
my %domain_boxes;
my %boxes_sent;
my %boxes;
if ( open LOG, "tail -f $logfile |" ) {
while( my $line = <LOG> ) {
if ( $line =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\S+) <= (\S+) H=(\S+) \[(\d+\.\d+\.\d+\.\d+)\] P=.+ X=.+ S=.+ id=.+ from <(.+)> for (.+)$/ ) {
$boxes{ $3 }{ $7 } += 1;
$boxes_sent{ $3 } += 1;
$domains{ $4 } += 1;
$domain_boxes{ $4 }{ $3 } += 1;
$matches++;
print_top();
}
}
}
sub print_top {
print "\033[2J";
print "\033[0;0H";
my @sorted_boxes = sort { $boxes_sent{ $b } <=> $boxes_sent{ $a } } keys %boxes_sent;
my $boxes = scalar @sorted_boxes;
my $items = $boxes < $top ? $boxes : $top;
my $row = 1;
my $tb = Text::Table->new('Top', 'Sent', 'Box' );
my @rows;
for my $bidx (0..$items-1) {
my $box = $sorted_boxes[$bidx];
push @rows, [ $row, $boxes_sent{ $box }, $box ];
$row++;
}
$tb->load( @rows );
print $tb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment