Solved

sendmail log script

Posted on 2002-07-26
5
371 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now