#!/usr/bin/perl # # STATS.PL # by Matt Kruse # 7/8/95 # # USAGE: # stats.pl - output statistics for all logins by all users. # # stats.pl username - output statistics for all logins by specific user # # stats.pl username # - output statistics for last # logins for a # specific user. # # This script has nothing to do with the WWW, but is very useful. # It outputs login information about a specific user on your site, based # on the output of the 'last' command and puts it into a useful format. # # It was written for perl 4.036 and runs well on a liunx system. If # you plan to run it on any other system, it will take extensive # modifications to read a different 'last' output. # # This script is 100% free. $USER=$ARGV[0]; $COUNT=$ARGV[1]; if ($USER eq '') { open (LAST,"last |"); } else { open (LAST,"last -$COUNT $USER |"); } @LINE=(); close(LAST); $BEGIN=pop(@LINE); pop(@LINE); chop($BEGIN); foreach $line (@LINE) { $connect=substr($line,10,13); $connect=~s/\s//g; if ($connect=~/ftp/) {$FTP++;} else {$CONNECTION{$connect}++; $TOTAL++;} $_=substr($line,23,17); s/\s//g; if (($_ eq '') && ($connect=~/ttyS/)) {$_='DIAL-IN';} elsif ($_ eq '') {$_='console';} $FROM{$_}++; $DAY{substr($line,40,3)}++; $DATE{substr($line,48,2)}++; $_=substr($line,51,2); s/0(\d)/$1/g; $TIME{$_}++; $LENGTH=(substr($line,67,2)*60) + substr($line,70,2); $TOTAL_TIME=$TOTAL_TIME+$LENGTH; } $AVERAGE_TIME=int($TOTAL_TIME / $TOTAL); $AVERAGE_HOURS=int($AVERAGE_TIME/60); $AVERAGE_MINUTES=$AVERAGE_TIME-(60*$AVERAGE_HOURS); $AVERAGE_TIME=$AVERAGE_HOURS . "h " . $AVERAGE_MINUTES . "m "; $TOTAL_HOURS=int($TOTAL_TIME/60); $TOTAL_MINUTES=$TOTAL_TIME-(60*$TOTAL_HOURS); $TOTAL_TIME=$TOTAL_HOURS . "h " . $TOTAL_MINUTES . "m "; #Print out Beginning of report and Totals #---------------------------------------- print "Statistics for $USER\n"; print "$BEGIN\n"; print "------------------------------\n"; print "TOTALS:\n"; print " Logins to this machine: $TOTAL\n"; print " Total Time Logged in: $TOTAL_TIME\n"; print " Average: $AVERAGE_TIME per login\n"; print " Dial-In's: $FROM{'DIAL-IN'}\n"; print " FTP Accesses: $FTP\n\n"; print "WHERE THEY WERE COMING FROM:\n"; foreach $key (keys %FROM) { print " $key: $FROM{$key}\n"; } #Figure accesses by day #---------------------- $HIGHEST=0; foreach $key (keys %DAY) { if ($DAY{$key} > $HIGHEST) {$HIGHEST=$DAY{$key}; } } $VALUE=int($HIGHEST/30); if ($VALUE == 0) { $VALUE=1; } print "\nACCESSES BY DAYS OF THE WEEK: ( * = $VALUE logins )\n"; print " Sunday: "; print '*' x int($DAY{'Sun'}/$VALUE); print "($DAY{'Sun'})\n"; print " Monday: "; print '*' x int($DAY{'Mon'}/$VALUE); print "($DAY{'Mon'})\n"; print " Tuesday: "; print '*' x int($DAY{'Tue'}/$VALUE); print "($DAY{'Tue'})\n"; print " Wednesday: "; print '*' x int($DAY{'Wed'}/$VALUE); print "($DAY{'Wed'})\n"; print " Thursday: "; print '*' x int($DAY{'Thu'}/$VALUE); print "($DAY{'Thu'})\n"; print " Friday: "; print '*' x int($DAY{'Fri'}/$VALUE); print "($DAY{'Fri'})\n"; print " Saturday: "; print '*' x int($DAY{'Sat'}/$VALUE); print "($DAY{'Sat'})\n"; #Print out accesses by Time #-------------------------- $HIGHEST=0; foreach $key (keys %TIME) { if ($TIME{$key} > $HIGHEST) {$HIGHEST=$TIME{$key}; } } $VALUE=int($HIGHEST/15); if ($VALUE == 0) { $VALUE=1; } print "\nACCESSES BY HOUR: ( # = $VALUE logins )\n"; for ($i=15;$i>0;$i--) { for ($j=0;$j<24;$j++) { if (int($TIME{$j}/$VALUE) >= $i) {print " # ";} else { print " "; } } print "\n"; } print "--------------------------------------------------------------------------\n"; print " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n\n";