Code:
#!/usr/bin/perl
no warnings "uninitialized";
use File::Copy;
use File::stat;
use Time::Local;
use IO::Handle;
use DateTime;
use Getopt::Long;
use File::Glob ':glob';
my $Summary;
my $Individual;
my $Diagnostics;
my $All;
my $help;
main();
sub main
{
$result = GetOptions ("LogDir=s" => \$LogDir,
"Summary" => \$Summary,
"Individual=s" => \$Individual ,
"Diagnostics=s" => \$Diagnostics,
"Prefix=s" => \$Prefix,
"All" => \$All,
"help" => \$help);
usage_help() if($help);
usage_help() unless($Prefix);
usgage_help() unless($LogDir);
if($LogDir)
{
printf "LOG DIR = $LogDir\n";
die "Log dir '$LogDir' doesn't exist" unless -d $LogDir;
}
$glob_path = "$LogDir/${Prefix}*";
@log_paths = glob $glob_path or die "No files found in '$glob_path'";
#printf "log files = \n";
#print @log_paths;
#printf "\n";
if($Summary)
{
process_summaryreport (@log_paths);
}
# This function tells the usage of the script.
sub usage_help
{
printf <<EOF
Script usage is
Scriptname --Summary
Scriptname --Indiviual=Arcotid/QNA/OTP/Password/All/Arcotid Revoke/Expire/All/QNA number
Scriptname --Diagnostics=username/system
Scriptname --ALL
printf "Example:-> perl WFLogStat.pl --LogDir = <DirName if current then put '.'> --Prefix = <arcotwebfort> --Summary";
printf "Example:-> perl WFLogStat.pl --LogDir = <DirName if current then put '.'> --Prefix = <arcotwebfort> --Individual=ArcotID/QnA/OTP/UP/ALL/ArcotID_Expired/ArcotID_Reset/ArcotID_Revoke/Total/UserQnA";
printf "Example:-> perl WFLogStat.pl --LogDir = <DirName if current then put '.'> --Prefix = <arcotwebfort> --Diagnostics=User_Name/System ";
EOF
}
# This function in turn calls many functions which give us the summary about the AOK webfort logs.
sub process_summaryreport
{
printf "in process summary\n";
Avg_Time_Taken_Per_Transaction (@log_paths);
}
# This function gives the Average time taken for a transaction to complete. The transaction can be for ArcotID
# or QnA or OTP or User Password.
sub Avg_Time_Taken_Per_Transaction
{
avg_time_taken_in_UP(@log_paths);
avg_time_taken_in_QnA(@log_paths);
avg_time_taken_in_OTP(@log_paths);
}
sub avg_time_taken_in_QnA
{
die if not @_;
my @log_files = @_;
open(AVG_OUT,">avg_time") or die "cannot create file avg_time for writing";
my %mon2num = qw(
Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6
Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12
);
my ( $start_dt, $end_dt );
for my $log_file (@log_files)
{
open my $log, '<', $log_file or die "Can't open '$log_file' for reading.";
printf AVG_OUT "Processing file $log_file...\n";
while( <$log> )
{
s{\s+\z}{};
#m/^\s*\w+\s+(\w+)\s+(\d+)\s+(\d\d:\d\d:\d\d\.\d\d\d)\s+(\d+)\s+..*pid\s+(\d+)\s+tid\s+(\d+):..*RiskServer : Read.*/i)
if (/(?:Entering|Exiting) QnAModule::authenticate/)
{
$end_dt = '';
my @dtA = split;
my @time = split /[:.]/, $dtA[3];
( /Exiting/ ? $end_dt : $start_dt ) = DateTime->new
(
year => $dtA[4],
month => $mon2num{ $dtA[1] },
day => $dtA[2],
hour => $time[0],
minute => $time[1],
second => $time[2],
nanosecond => $time[3],
);
if ($end_dt && $start_dt)
{
print AVG_OUT "start: ", $start_dt, "\n";
print AVG_OUT "end: ", $end_dt, "\n";
my $e = $end_dt->subtract_datetime($start_dt);
printf AVG_OUT "elapsed time for QnA Module: %s year(s), %s month(s), %s week(s), %s day(s), %s hour(s), %s min, %s sec, %s ms\n",
$e->years, $e->months, $e->weeks, $e->days, $e->hours,
$e->minutes, $e->seconds, $e->nanoseconds;
}
}
}
close $log or warn;
}
close (AVG_OUT);
}
sub avg_time_taken_in_OTP
{
die if not @_;
my @log_files = @_;
open(AVG_OUT,">>avg_time") or die "cannot create file avg_time for writing";
my %mon2num = qw(
Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6
Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12
);
my ( $start_dt, $end_dt );
for my $log_file (@log_files)
{
open my $log, '<', $log_file or die "Can't open '$log_file' for reading.";
printf AVG_OUT "Processing file $log_file...\n";
while( <$log> )
{
s{\s+\z}{};
if (/(?:Entering|Exiting) OTPAuthModule::authenticate/)
{
$end_dt = '';
my @dtA = split;
my @time = split /[:.]/, $dtA[3];
( /Exiting/ ? $end_dt : $start_dt ) = DateTime->new
(
year => $dtA[4],
month => $mon2num{ $dtA[1] },
day => $dtA[2],
hour => $time[0],
minute => $time[1],
second => $time[2],
nanosecond => $time[3],
);
if ($end_dt && $start_dt)
{
print AVG_OUT "start: ", $start_dt, "\n";
print AVG_OUT "end: ", $end_dt, "\n";
my $e = $end_dt->subtract_datetime($start_dt);
printf AVG_OUT "elapsed time for OTP Module: %s year(s), %s month(s), %s week(s), %s day(s), %s hour(s), %s min, %s sec, %s ms\n",
$e->years, $e->months, $e->weeks, $e->days, $e->hours,
$e->minutes, $e->seconds, $e->nanoseconds;
}
}
}
close $log or warn;
}
close (AVG_OUT);
}
sub avg_time_taken_in_UP
{
die if not @_;
my @log_files = @_;
open(AVG_OUT,">>avg_time") or die "cannot create file avg_time for writing";
my %mon2num = qw(
Jan 1 Feb 2 Mar 3 Apr 4 May 5 Jun 6
Jul 7 Aug 8 Sep 9 Oct 10 Nov 11 Dec 12
);
my ( $start_dt, $end_dt );
for my $log_file (@log_files)
{
open my $log, '<', $log_file or die "Can't open '$log_file' for reading.";
printf AVG_OUT "Processing file $log_file...\n";
while( <$log> )
{
s{\s+\z}{};
if (/(?:Entering|Exiting) UPAuthModule::authenticate/)
{
$end_dt = '';
my @dtA = split;
my @time = split /[:.]/, $dtA[3];
( /Exiting/ ? $end_dt : $start_dt ) = DateTime->new
(
year => $dtA[4],
month => $mon2num{ $dtA[1] },
day => $dtA[2],
hour => $time[0],
minute => $time[1],
second => $time[2],
nanosecond => $time[3],
);
if ($end_dt && $start_dt)
{
print AVG_OUT "start: ", $start_dt, "\n";
print AVG_OUT "end: ", $end_dt, "\n";
my $e = $end_dt->subtract_datetime($start_dt);
printf AVG_OUT "elapsed time for User Password Module: %s year(s), %s month(s), %s week(s), %s day(s), %s hour(s), %s min, %s sec, %s ms\n",
$e->years, $e->months, $e->weeks, $e->days, $e->hours,
$e->minutes, $e->seconds, $e->nanoseconds;
}
}
}
close $log or warn;
}
close (AVG_OUT);
}