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: Regular Expressions: Re: [1arryb] perl script help: Edit Log



perlisgreat
Novice

Jun 16, 2009, 7:20 AM


Views: 17220
Re: [1arryb] perl script help


In Reply To
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:

Code
... 
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.

Code
... 
# 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


Hi Larry,

Here is the snippet of the code--


Code
#!/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;;
}
}
}


i am running this file as-

C:\Perl Script>perl OTP_Authentication.pl ./arcotwebfort_29May09* >results.txt

but it gives me
usage: OTP_Authentication.pl <logDir> <logPrefix> at OTP_Authentication.pl line
5.

Can you please tell me how to use this.
Am i running the program fine. The log files exists in the same directory.

Thanks
NT


(This post was edited by namishtiwari on Jun 16, 2009, 7:24 AM)


Edit Log:
Post edited by perlisgreat (Novice) on Jun 16, 2009, 7:24 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