Script to get the number of events from the logs.

\"\"

I was trying to do some log analysis and finding the events in the logs. For this the logs had the Events logged as \”|+Event name|\” or with sending and receiving. So I wrote this little script to take care of my requirements.

First you would need to change the pattern in the bold red and make sure that all special characters like | or + need to be escaped with \”\”. So here\’s the script:

#!/usr/bin/perl
#===============================================================================
#
#         FILE:  log_counters.pl
#
#        USAGE:  ./log_counters.pl log_filename
#
#  DESCRIPTION:  Get all the counters for the messages from the logs.
#
#      OPTIONS:  —
# REQUIREMENTS:  —
#         BUGS:  —
#        NOTES:  —
#       AUTHOR:  Amit Agarwal ([email protected]),
#      COMPANY:
#      VERSION:  1.0
#      CREATED:  01/15/2010 11:27:49 AM
#     REVISION:  —
#===============================================================================
open (FILE, “<$ARGV[0]“);

@names = `grep|+’ $ARGV[0]|sed -e ’s#.*|+##’ -e ’s#|.*##’|sort |uniq`;

%patterns=(’Received :’ => 0, ‘Sending :’ => 0);
for $j (@names) {

chomp($j);
$temp = “\\+$j\\|”;

$patterns{$temp} = 0;
}
@keys = keys %patterns;

while ($line = ) {
chomp($line);
for $i  (keys %patterns)
{

if ($line =~ m/$i/){

$patterns{$i} += 1;

}

}
}
for $j (sort keys %patterns){
printf “%40s ttt==> $patterns{$j}n”, $j;
}
close(FILE);

Sample output:

Received :             ==> 41
Sending :             ==> 39

PS: If you need explanation for any of the above, dont hesitate to contact me via comments or email.

\"Reblog