Hi namishitwari,
Easy-peasey. The perl glob() command is what you're looking for. You'll need to pass the log directory and log prefix to your script instead of a file name and add an outer loop to handle the multiple files. Perhaps something like:
...
my $logDir = $ARGV[0];
my $logPrefix = $ARGV[1]
die "usage: $0 <logDir> <logPrefix>" unless $logDir and $logPrefix;
die "Log dir $logDir doesn't exist" unless -d "$logDir";
for my $logFile ( glob("$logDir/${logPrefix}*") ) {
# Process $logFile as before.
...
}
Depending on whether you want per-file or summary statistics, you can keep the output code where it is or move it outside of the loop on $logFile.
UPDATE:
I didn't see the bit about timestamps. Getting the timestamp is easy.
...
# Note: The '(?:...)' regex means "group these regular expressions but don't remember the match".
if ( $line =~ /^(.*) INFO:.*(?:userName [:]|UPAuth SUCCESS for user|OTP SUCCESS for user)(?:\(|\[)(\S*)(?:\)|\])/ ) {
my $timestamp = $1;
my $userName = $2 ? $2 : "NULL";
...
The trick will be if you need to convert the timestamp to some other format. That can be a chore.
UPDATE2:
I'm also not sure what you intend to do with the timestamps. You'll have to change your output to report each session, instead of summarizing sessions per authMethod.
Cheers,
Larry
#!/usr/bin/perl
my $logDir = $ARGV[0];
my $logPrefix = $ARGV[1];
die "usage: $0 <logDir> <logPrefix>" unless $logDir and $logPrefix;
die "Log dir $logDir doesn't exist" unless -d "$logDir";
for my $logFile ( glob("$logDir/${logPrefix}*") )
{
open($log, "<", $logFile) or die "Can't open $logFile for reading.";
open(FP_OUT,">temp1") or die "cannot create file temp1 for writing";
print "Processing file $logFile...\n";
my $Time_Stamp ;
$line_number=0;
my $User_Name;
my $Success;
my $Failure;
my $ErrorCode;
my $ErrorMsg;
while( $line = <$log> )
{
$line_number++;
chomp($line);
if ($line =~ /^(.*)INFO:.*Entering OTPAuthModule::authenticate/)
{
$Time_Stamp = $1;
printf "$line_number: in Entering OTPAuthModule::authenticate\n";
printf FP_OUT "$Time_Stamp,";
die "some error in OTP_Search_For_UserID "if(OTP_Search_For_UserID() == -1);
die "some error in OTP_Search_For_Success_Or_failure "if(OTP_Search_For_Success_Or_failure() == -1);
}
}
}
sub OTP_Search_For_UserID
{
while ( $line = <$log> )
{
$line_number++;
chomp($line);
if ( $line =~ /OTP User DB Query is based on username\[(..*)\] and groupID\[(..*)\]/ )
{
printf "$line_number: in user id = $1 \n";
$User_Name = $1 ? $1 : "NULL" ;
printf FP_OUT "$User_Name,";
return 0;
}
elsif($line =~ /Prepared to Send OK/)
{
printf "$line_number : did not get user id got prepared to send ok \n";
return -1;
}
}
return -1;
}
sub OTP_Search_For_Success_Or_failure
{
while ( $line = <$log> )
{
$line_number++;
chomp($line);
if ($line =~ /OTP SUCCESS.*/) {
printf "$line_number: in sucess\n";
printf FP_OUT "Success,";
$ErrorMsg="null";
while ( $line = <$log> )
{
$line_number++;
if ($line =~ /ArAuthFrameworkImpl::doPostAuth.*Authentication mechanism returned \[(\d+)\]/)
{
printf FP_OUT "${1}, $ErrorMsg\n";
return 0;
}
elsif($line =~ /Prepared to Send OK/)
{
printf "$line_number : END\n";
return 0;
}
}
return -1;
}
elsif($line =~ /Message: OTP FAILED.*Reason : (.*)!/)
{
$ErrorMsg=$1;
printf "$line_number: Failure\n";
printf FP_OUT "Failure,";
while ( $line = <$log> )
{
$line_number++;
if ($line =~ /ArAuthFrameworkImpl::doPostAuth.*Authentication mechanism returned \[(\d+)\]/)
{
printf FP_OUT "${1}, $ErrorMsg\n";
return 0;
}
elsif($line =~ /Sending Invalid credential/)
{
printf "$line_number : END\n";
return 0;
}
}
return -1;;
}
}
}