CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: script to count per hour success/error from logs: Edit Log



jeffersno1
Novice

Nov 12, 2012, 1:14 PM


Views: 3508
script to count per hour success/error from logs

Hi All,

I need some help! i know when I've been beaten! I help support a pass booking system and I need to generate some stats on a daily basis showing pass booking attempts (success and failure) every hour.

I'm trying to write a script that does the following:
1 - Script will run from cron daily against yesterdays files and store results. For now i'm using a testdata file
2 - search for pass booking success and failures
3 - Print to screen how many pass success and errors per hour by pass type eg:

Expected results should look like:

Time, Pass Name, Success, Failures
2012-11-08_06:00:00,passA,1,0
2012-11-08_07:00:00,passA,0,1
2012-11-08_07:00:00,passB,1,0
2012-11-08_07:00:00,passC,1,0
2012-11-08_07:00:00,passF,0,3

example date

2012-11-08_06:13:39:094,BOOK,444771234567,,admin@sys_3501,passA,,success,1341
2012-11-08_07:00:22:967,BOOK,442345678999,,admin@sys_3744,passB,,success,591
2012-11-08_07:02:10:128,BOOK,444444444444,,admin@sys_3672,passC,,success,792
2012-11-08_07:02:10:128,BOOK,444466666777,,admin@sys_3672,passA,,internal_server_error,92
2012-11-08_07:02:11:128,BOOK,444444444455,,admin@sys_3672,passF,,internal_server_error,12
2012-11-08_07:02:12:128,BOOK,444466666666,,admin@sys_3673,passF,,internal_server_error,23
2012-11-08_07:02:13:128,BOOK,444477777777,,admin@sys_3675,passF,,internal_server_error,11

I've tried to do this by incrementing the hour time which I've pulled out from the linearray.

The script works but it doesn't print the first hours worth of data, and now I've realised I don't know how to get the errors calculated, should i be using hashes ? They've always confused me!!

Any advice is much appreciated.


Code
#!/usr/bin/perl -w 

use POSIX;

my $filename = "/home/sys/testdata";
open (LOGFILE, $filename ) or die ("Could not open log file.");

$success_time="";
#$connclosed_time=0;
#$timedout_time=0;

for $line (<LOGFILE>)
{
chomp($line);
if (length($line) > 0)
{
@lineArray=split(",",$line);
if ( $lineArray[1] =~ /BOOK/ )
{
$lineArray[0] =~ s/_/:/g;
($date,$hh,$mm,$ss,$ms)=split(":",$lineArray[0]);
$time="$hh";
#$time="$date:$hh";
$key = $lineArray[5];
if (! defined $resultsHash{ $key })
{
$resultsHash{ $key } = [0,0]; ### there can 2 outcomes success or failure
}
my $temp = $resultsHash{ $key };
if ( defined $lineArray[7] && $lineArray[7] =~ /success/ )
{
if ($success_time eq "")
{
print "Time Changed (FIRST) $time\n";
$success_time=$time;
}
if ( $success_time eq $time )
{
# Same time
@$temp[0]++; # Increase Success Counter for this key
}
else
{
# Time Changed
print "Time Changed from $success_time to $time\n";
# Need to reset all Key counters not just this one.
$total=0;
foreach (sort keys %resultsHash)
{
$reset=$resultsHash{$_};
print " $date\_$success_time:$mm,$_,@$reset[0]\n";
$total=$total+@$reset[0];
@$reset[0]=0;
}
print " Total - $total\n";
# Increase Success Counter for this key only
@$temp[0]++;
$success_time=$time;
}
}
}
}
}
close LOGFILE;



(This post was edited by jeffersno1 on Nov 13, 2012, 12:15 AM)


Edit Log:
Post edited by jeffersno1 (Novice) on Nov 13, 2012, 12:01 AM
Post edited by jeffersno1 (Novice) on Nov 13, 2012, 12:15 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives