kdmurphy001 has asked for the wisdom of the Perl Monks concerning the following question:
I once again must beseech you for your aid and ask that you bestow upon me a small fraction of your boundless knowledge. I have partaken on a humble quest but have stumbled near the end.
I am seeking to calculate the length of sessions by scanning a logFile (logFile.txt). It's an apache log file and I have used other versions of this crude script successfully in the past. The one big change here being that I must now look through the file a second time for the last occurrence of the session ID.
To this end I have posted my writing below. Currently the script works only on compressed files (with slight modification) or if I remove/comment out the FILELEN file handle. If I run it as shown below it will simply spin (running from a Jenkins env) with no errors and no output. Please forgive me for showing such ugliness to your divine eyes. It is only out of great necessity that I do so.
I thank you again for your blessings.#!/usr/bin/perl use strict; use warnings; use DBD::mysql; use File::Find; use File::Find::Rule; use POSIX qw(strftime); use PerlIO::gzip; use DateTime (); use Date::Parse qw(str2time); #Get yesterdays date my $epoc = time() - 86400; # one day before of current date. my $yesterday = strftime "%F", localtime($epoc); #REGEX Patterns my $pattern = '(\d+-\d+-\d+ \d+:\d+:\d+).*login:(\S+).*UID:(\S+).*sess +ion:(\S{32}).*type:(\d).*InstID:(\S+)'; my $pattern_session = '(\d+-\d+-\d+).*'; my $output_file= 'FileList_with_Length.txt'; open OUTPUT, ">$output_file"; #Locate Files my $dir = $ARGV[0]; my $ffr_obj = File::Find::Rule->file() ->name( "logFile.txt" ) ->maxdepth( 5 ) ->start ( $ARGV[0] ); while (my $file = $ffr_obj->match() ) { if($file =~ /\/net\/bard2\/home\/bca\/logs\/ilrn-(east|west|sjc|cv +g)(\d+)\/logFile.txt/) { my $node = $2; my $pool = $1; print "In File: $file\n"; open FILE, "< $file" or die $!; while(<FILE>) { if ($_ =~ /$pattern/) { my $currentline = $. ; my $currentsession = $4 ; my $starttime = $1 ; #Sets date and time when first +session ID found my $endtime = 0; #Will hold date and time when +last session ID found my $duration = 0; #Get Sesion Length my $lastseen = 0; #If > 100000 will assume session +has ended open FILELEN, "< $file" or die $!; #open FILELEN, "<:gzip", "$file" or die $!; while (<FILELEN>) { #Skip all lines up to current line (where session +ID was first seen) next if (1..$currentline); if ($_ =~ /(\d+-\d+-\d+ \d+:\d+:\d+).*$currentsess +ion/) { $lastseen = 0; $endtime = $1; #Sets endtime to last date a +nd time seen } else { if($lastseen < 100000) { $lastseen++; } if($lastseen >= 100000) { #Exit if 100,000 +lines were searched with out seeing any further next; #of the session i +d } } } close FILELEN; $duration = str2time($endtime) - str2time($starttime); + #convert to epoch time and subtract to get length in seconds if($duration > 5) { print OUTPUT "$1\t$2\t$3\t$4\t$ +5\t$6\t$duration\n" ; print "$1\t$2\t$3\t$4\t$5\t$6\t$duration\n" } + } } close FILE; } } close OUTPUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Suspect FH issues
by Corion (Patriarch) on Oct 23, 2014 at 07:59 UTC | |
|
Re: Suspect FH issues
by McA (Priest) on Oct 23, 2014 at 07:55 UTC | |
|
Re: Suspect FH issues
by Discipulus (Canon) on Oct 23, 2014 at 07:55 UTC |