#!/usr/bin/perl use warnings; use strict; use Time::Local; my $x = getTime(); my $d1; my $d2; my $ID; my $epoch; my %openTime; my %account; my %totalSession; my %dSession; sub getTime { (my $sec,my $min,my $hour,my $mday,my $month,my $year,my $wday,my $yday,my $isdst)=localtime(time); $epoch = timelocal($sec, $min, $hour, $mday, $month, $year); return $epoch; } my @fileTotal = `ls /u/user/myarea/test/file*`; #foreach loop opens each log file foreach my $file (@fileTotal) { chomp($file); open(FILEREAD, "< $file"); open FILEREAD, "< $file" or die "Can't open $file for reading - $?"; while (my $linebuf = ) { chomp($linebuf); # remove at the end my @data = split(/[ ]+/, $linebuf); if( $data[6] eq "session" && $data[7] eq "opened") { $d1 = index($linebuf, "[", 0); $d2 = index($linebuf, "]", ($d1+1)); $ID = substr($linebuf, ($d1+1), ($d2-$d1-1)); $openTime{$ID} = $epoch; $account{$ID} = $data[10]; print "open time= $openTime{$ID}\n"; } elsif( $data[6] eq "session" && $data[7] eq "closed") { # user SSH logoff session $d1 = index($linebuf, "[", 0); $d2 = index($linebuf, "]", ($d1+1)); $ID = substr($linebuf, ($d1+1), ($d2-$d1-1)); my $closeTime = $epoch; print "closetime= $closeTime\n"; if( defined $account{$ID}) { my $userAccount = $account{$ID}; my $duration = $closeTime - $openTime{$ID}; #interval $totalSession{$userAccount}++; $dSession{$userAccount} += $duration; } } } close FILEREAD; } foreach $x (sort(keys %totalSession)) { my $averageSession = $dSession{$x}/$totalSession{$x}; printf "%-10s %10s %10s \t %.2f\n",$x,$totalSession{$x},$dSession{$x}, $averageSession; }