Script to get the number of events from the logs.

2010-04-11 2 min read Learning Linux perl

<a id="aptureLink_4P0PJ73NaG" style="margin: 0pt auto; padding: 0px 6px; text-align: center; display: block;" href="http://en.wikipedia.org/wiki/Log%20analysis"><img style="border: 0px none;" title="Log analysis" src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/ph/360x320_WikipediaArticle" alt="" width="360px" height="320px" />

I was trying to do some <a class="zem_slink freebase/en/log_analysis" title="Log analysis" rel="wikipedia" href="http://en.wikipedia.org/wiki/Log_analysis">log analysis and finding the events in the logs. For this the logs had the Events logged as &#8221;|+Event name|&#8221; 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 <span style="color: #ff0000;">bold red and make sure that all <a class="zem_slink freebase/en/character_encoding" title="Character encoding" rel="wikipedia" href="http://en.wikipedia.org/wiki/Character_encoding">special characters like | or + need to be escaped with &#8221;&#8221;. So here&#8217;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:  <a class="zem_slink" title="Amit Agarwal" rel="me" href="http://amit-agarwal.co.in">Amit Agarwal (amit.agarwal@amit-agarwal.co.in),
#      COMPANY:
#      VERSION:  1.0
#      CREATED:  01/15/2010 11:27:49 AM
#     REVISION:  —
#===============================================================================
open (FILE, “<$ARGV[0]“);

@names = `<a class="zem_slink freebase/en/grep" title="Grep" rel="wikipedia" href="http://en.wikipedia.org/wiki/Grep">grep ‘<span style="color: #ff0000;">|+’ $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.<h6 class="zemanta-related-title">Related articles by Zemanta <ul class="zemanta-article-ul"> <li class="zemanta-article-ul-li"><a href="http://blog.amit-agarwal.co.in/2010/01/19/bash-one-liner-to-changeremove-test-pattern-from-config-file-or-text-file/">bash one liner to change/remove test pattern from config file or text file. (amit-agarwal.co.in) <li class="zemanta-article-ul-li"><a href="http://blog.amit-agarwal.co.in/2010/01/20/gnome-disk-manager-the-easier-way-to-manage-disks-in-gnome-with-palimpsest/">gnome-disk-manager – the easier way to manage disks in Gnome with Palimpsest (amit-agarwal.co.in) <li class="zemanta-article-ul-li"><a href="http://www.xaprb.com/blog/2009/11/01/catching-erroneous-queries-without-mysql-proxy/">Catching erroneous queries, without MySQL proxy (xaprb.com) <li class="zemanta-article-ul-li"><a href="http://www.downloadsquad.com/2009/09/22/twitter-continues-quest-for-global-domination-adds-media-sharin/">Twitter continues quest for global domination, adds media sharing to Bit.ly (downloadsquad.com) <li class="zemanta-article-ul-li"><a href="http://www.ezrasf.com/wplog/2009/06/19/diff-and-spaces/">Diff and Spaces (ezrasf.com) <div class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/956c31e1-7197-4333-9451-90a8677267df/"><img class="zemanta-pixie-img" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/reblog_b55.png" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related more-info pretty-attribution paragraph-reblog">

comments powered by Disqus