Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sendmail log script

Posted on 2002-07-26
5
Medium Priority
?
384 Views
Last Modified: 2010-04-22
I wanna do the following task with shell script(may be using awk or grep)

I have a huge sendmail log files(weekly rotated). So I want the shell  program which will serach the sendmail logs based on toaddress, fromaddress ,  stat=Deferred or qued.. And also the discription like Connection refused or relay denied.. If that script have options to choose the sendmail log file or all files.

Basicly I want to know how many messages(numbers and full messages also) failed/deferred/refused/relay denied thru sendmail.
0
Comment
Question by:arvind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:DVB
ID: 7185394
Basically, are you looking for something like pflogsumm.pl (this is for postfix, not sendmail, but should be easy to adapt)?
0
 
LVL 5

Author Comment

by:arvind
ID: 7190362
I'd tried but its not working with sendmail
0
 
LVL 1

Expert Comment

by:joesp
ID: 7723485
modify this script a little:

[joe@www mail_parser]$ more parse_maillog_cv.pl
#!/usr/bin/perl
# reads sendmail log files tells how much/from/date/to for all mail users on system
# orders by employee
#  /main/

@employee_names=("","","","","","", "", "");
# employees in maillog are dick, salesman, jimk,goblin, patron
my @employee_names_copy = @employee_names;
my $first_object = make_hash->new("maillog.5");
my $array_ref = $first_object->return_array_object;
#  my $list_ref = ${$first_object->return_list_object};
# from
my $from_array_ref = parse_from_hash(\@employee_names, $array_ref);
print "********************from**************************\n";
my @array = @$from_array_ref;
for ($j=0; $j <= $#array; $j++) {
        print "&&&&&&& " . $array[$j]{name} . " sent out $array[$j]{from_count} messages.\n";
        if ($array[$j]{from_count} > 0) {
                print "the recipients were:\n";
                for ($i=0; $i < $array[$j]{from_count}; $i++) {
                        print $i+1 . ")  [" . $array[$j]{array_copy}[$i]{date}  . "]  ";
                        foreach ( @{$array[$j]{array_copy}[$i]{to_list}}) {
                                 print $_ . "  " ;
                        }
                print "\n";
                }
        }
        print "\n\n";
}

# to
my $to_array_ref = parse_to_hash(\@employee_names, $array_ref);
print "********************to**************************\n";
@array = @$to_array_ref;
for ($j=0; $j <= $#array; $j++) {
        print "&&&&&&&   $array[$j]{name}   received $array[$j]{to_count} messages at the following dates from these addresses:  \n";
        for ($i=1; $i <= $array[$j]{to_count}; $i++) {
                print $i. ")  [" . $array[$j]{array_copy}[$i-1]{date} . "]  ";
                print $array[$j]{array_copy}[$i-1]{from} . " \n ";
        }
        print "\n\n";
}

# description of hash resulting from parse_hash
# array of hashes with two of the four hash keys being arrays of hashes
# @employee_name[i] as one hash key ...
# -> name  ... $employee_name[] as index
# -> to_count (int) ... number of messages for this person
# -> from_count (int) ... number of messages from this person
# -> -> date # -> -> date # -> -> date # -> -> date
#    -> from #    -> from #    -> from #    -> from
#    -> @to  #    -> @to  #    ->@ to  #    ->@ to
#    -> id   #    -> id   #    -> id   #    -> id      etc.
#  note that the id value is not printed, though it is the major key

#  /end main/

# functions

sub parse_from_hash() {
#       get values off arguments
        my $emp_array_ref = shift;
        my @emp_array = @$emp_array_ref;
        my @emp_array_copy = @emp_array;
        my $hash_array_ref = shift;
        my @hash_array = @$hash_array_ref;
        my @hash_array_copy = @hash_array;
        my @unb;
#       end return value
#       create array hash keys of name and initialize the non-array, int variables
        for ($j=0; $j <= $#emp_array_copy; $j++) {
                $unb[$j]{name}=$emp_array[$j];
                $unb[$j]{from_count}=0;
                $unb[$j]{to_count}=0;
        }
        my @unb_copy = @unb;
        for ($j=0; $j <= $#unb_copy; $j++) {
                for (my $i=0;$i<=$#hash_array_copy;$i++) {
                        if ($hash_array[$i]->{from} =~ /$unb_copy[$j]{name}/i) {
                                $unb[$j]{array_copy}[$nec_counter]{from} = $unb_copy[$j]{name};
                                #  print join(' ', @{$hash_array[$i]{to}}) . "\n";
                                $unb[$j]{array_copy}[$nec_counter]{to_list} = $hash_array[$i]{to};
                                $unb[$j]{array_copy}[$nec_counter]{date} = $hash_array[$i]{date};
                                $unb[$j]{array_copy}[$nec_counter]{id} = $hash_array[$i]{id};
                                $unb[$j]{from_count}++;
                                $nec_counter++;
                        }
                }
                $nec_counter=0;
        }
        return \@unb;
}

sub parse_to_hash() {
#       get values off arguments
        my $emp_array_ref = shift;
        my @emp_array = @$emp_array_ref;
        my @emp_array_copy = @emp_array;
        my $hash_array_ref = shift;
        my @hash_array = @$hash_array_ref;
       my @hash_array_copy = @hash_array;
#       return value
        my @unb;
#       end return value
#       create array hash keys of name and initialize the non-array, int variables
        for ($j=0; $j <= $#emp_array_copy; $j++) {
                $unb[$j]{name}=$emp_array[$j];
                $unb[$j]{from_count}=0;
                $unb[$j]{to_count}=0;
        }
        my @unb_copy = @unb;
        for ($j=0; $j <= $#unb_copy; $j++) {
                for (my $i=0;$i<=$#hash_array_copy;$i++) {
                        foreach (@{$hash_array[$i]->{to}}) {
                                if (/$unb_copy[$j]{name}/i) {
                                        $unb[$j]{array_copy}[$nec_counter]{from} = $hash_array[$i]{from};
                                        $unb[$j]{array_copy}[$nec_counter]{to_list} = $hash_array[$i]{to};
                                        $unb[$j]{array_copy}[$nec_counter]{date} = $hash_array[$i]{date};
                                        $unb[$j]{array_copy}[$nec_counter]{id} = $hash_array[$i]{id};
                                        $unb[$j]{to_count}++;
                                        $nec_counter++;
                                }
                        }
                }
                $nec_counter=0;
        }
        return \@unb;
}
# description of hash resulting from make_hash
# [0]           [1]             [2]             etc.  for each mail in
# ->date       # ->date         # ->date
# ->from       # ->from         # ->from
# ->@to        # ->@to          # ->@to
# ->id         # ->id           # ->id
package make_hash;
use IO::File;
sub new  {
        shift;
        my $file = shift;
        my $domain = 'put in your value';
#       to make this work in the example, put $domain='cars4you';
        my @hash_of_mail;
        my $array_so_far="";
        my $temp_variable=0;
        my $i=0;
        my ($storage1, $storage2);
        open(FILE, $file) || die "can't open $!";
        while ( defined ($_ = <FILE>)){
#  this is the pattern to isolate the unique identifier for each piece of mail
                if (m/^(.*[0-9][0-9]:[0-9][0-9]:[0-9][0-9]) $domain .*]: +(.*): [ft]/) {
                        $storage1=$1;
                        $storage2=$2;
                        $jumper=0;
                        $array_so_far = $array_so_far . " " . $storage2;
                        for (my $loop_index = 0; $loop_index <= $temp_variable ; $loop_index++) {
                                if ($hash_of_mail[$loop_index]->{id}=~ m/$storage2/) {
                                        if (m/\bto=<(.*)>,\b/) {push(@{$hash_of_mail[$loop_index]->{to}},$1);}
                                        elsif (m/\bto=([^, ]*)\b/) {push(@{$hash_of_mail[$loop_index]->{to}},$1);}
                                        $jumper=1;
                                }
                        }
                        if ($jumper==0) {
                                $hash_of_mail[$temp_variable]->{"id"}=$storage2;
                                $hash_of_mail[$temp_variable]->{"date"}=$storage1;
                                if (m/from=<(.*)>[^, ]/) {$hash_of_mail[$temp_variable]->{from}=$1;}
                                elsif (m/from=([^, ]*)/) {$hash_of_mail[$temp_variable]->{from}=$1;}
                                elsif (m/\bfrom=([^, ]*)\b/) {$hash_of_mail[$temp_variable]->{from}=$1;}
                                if (m/\bto=<(.*)>,\b/) {push(@{$hash_of_mail[$loop_index]->{to}},$1);}
                               $temp_variable=$temp_variable+1;
                        }
                }
        }
        close FILE;
        return bless{'array'=>\@hash_of_mail,'ids'=>\$array_so_far};
}

sub return_list_object {return $_[0]->{'ids'};}

sub return_array_object {return $_[0]->{'array'};}

1;

0
 
LVL 5

Author Comment

by:arvind
ID: 7758655
Here is the output of ur script -- changed maillog path to /var/log/maillog.3


********************from**************************
&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


&&&&&&&  sent out 0 messages.


********************to**************************
&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


&&&&&&&      received 0 messages at the following dates from these addresses:  


0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8111849
PAQ'd and all 100 points refunded

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question