#!/usr/local/bin/perl
#========================================================================#
# relay_denied - This program will scan the previous weeks maillog and   #
#                extract any address that had relaying denied.  This is  #
#                so we don't have to rely on production to give us the   #
#                new ip's that are running through pitbull.              #
#------------------------------------------------------------------------#
# 20021101 - Chad Kerner - Initial Coding                                #
# 20021209 - Chad Kerner - Replaced grep with pattern match.             # 
# 20030123 - Chad Kerner - Replaced system calls with native perl calls. #
#                          Also removed the counter used to determine if #
#                          the header should be printed.  Used the scalar#
#                          variable instead.                             # 
#========================================================================#

$date = `date +"%b %e"`;
if( $ARGV[0] eq "" ) { 
    open(INFILE,"/var/log/maillog") || die("Unable To Open: /var/log/maillog\n"); 
    @maillog=<INFILE>; 
    close(INFILE); 
    @maillog = grep /$date/, @maillog; }
else { 
    open(INFILE," gunzip -c $ARGV[0] |") || die("Unable To Decompress: $ARGV[0]\n"); 
    @maillog=<INFILE>; 
    close(INFILE); }

foreach(@maillog) { 
    if( m/Relaying denied/ ) { 
        if( m/ relay\=\[(\S+)\]/ ) { $addresses{$1} = $addresses{$1} + 1; } } }

if( scalar (keys %addresses) >= 1 ) {
    printf("%-15s %12s\n","IP Address","Failures");
    foreach (sort keys %addresses) { printf("%-15s %12s\n",$_,$addresses{$_}); } }

