#!/usr/bin/perl use Time::Local; $x = &getTime; sub getTime { ($sec,$min,$hour,$mday,$month,$year,$wday, $yday,$isdst)=localtime(time); $epoch = timelocal($sec, $min, $hour, $mday, $month, $year); return $epoch; } @fileTotal = `ls /var/log/syslog*`; #foreach loop opens each log file foreach $file (@fileTotal) { chomp($file); open(FILEREAD, "< $file"); while ($linebuf = ) { chomp($linebuf); # remove at the end @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]; } 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)); $closeTime = $epoch; if( defined $account{$ID}) { $userAccount = $account{$ID}; $duration = $closeTime - $openTime{$ID}; #interval $totalSession{$userAccount}++; $dSession{$userAccount} += $duration; } } } close FILEREAD; } foreach $x (sort(keys %totalSession)) { $averageSession = $dSession{$x}/$totalSession{$x}; printf "%-10s %10s %10s \t %.2f\n", $x, $totalSession{$x}, $dSession{$x}, $averageSession; }