#!/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+).*session:(\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|cvg)(\d+)\/logFile.txt/) { my $node = $2; my $pool = $1; print "In File: $file\n"; open FILE, "< $file" or die $!; while() { 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 () { #Skip all lines up to current line (where session ID was first seen) next if (1..$currentline); if ($_ =~ /(\d+-\d+-\d+ \d+:\d+:\d+).*$currentsession/) { $lastseen = 0; $endtime = $1; #Sets endtime to last date and 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 id } } } 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;