My version of userlog

John-David Childs (jdc@denver.net)
Sun, 27 Jul 1997 00:27:03 -0600 (MDT)

Included below is my version of the oft-posted "userlog" script. This one
is based on the userlog script posted earlier today by Joe Hartley (which
had a missing '}" in the if ARGV[1] construct).

I have changed userlog so that *instead of* printing the username (kinda
redundant :-) and port on each line, it prints the input & output octets
AND the DISCONNECT CAUSE for each login. This makes userlog very useful
for seeing patterns of online time, bytes transferred, and disconnect
causes. Hope this is useful to the list. When I get some time, I'm
planning to write this in C for speed...but that might not be until after
the year 2000 :-)
==========================================================================

John-David Childs (JC612) @denver.net/Internet-Coach
System Administrator Enterprise Internet Solutions
& Network Engineer 901 E 17th Ave, Denver 80218
"I used up all my sick days... so I'm calling in dead!"
==========================================================================

#!/usr/local/bin/perl
##
# userlog - this program prints a log of a user's activity.
#
# Usage: userlog username [mm.yy]
# where username = the login of the user in question
# mm.yy = the numeric month and year of a previous period
#
# Copyright (C) 1995
# -Dave Andersen <angio@aros.net>
#
# Modified from Dave's "Lineparser" to work with 1 user
# 1/1/96 - Joe Hartley <jh@brainiac.com>
#
# Modified to allow specification of a single portmaster
# 10/29/96 - Joe Hartley <jh@brainiac.com>
#
# Modified to include the port attached to
# 3/26/97 - Joe Hartley <jh@brainiac.com>
#
# Modified to REMOVE the port attached to and the username
# and instead print input bytes, output bytes, and disconnect cause
# 7/26/97 - John-David Childs <jdc@griz.net>

##

### change this to the name of your radius accounting logs directory
### detail files are stored in subdirectory of $logdir

$logdir = "/var/log/pm";

# set the user to look for from the command line
if ($ARGV[0]) {
$testuser = sprintf("%-8s", $ARGV[0]);
}
else {
die("Usage: userlog username PMname|all [mm.yy]\n");
}

# Set the server list
if ($ARGV[1] =~ "all") {
@servlist = ("pm1","pm2","pm3");
}
else {
@servlist = ($ARGV[1]);
}

if ($ARGV[2]) {
$period = $ARGV[2];
}
else {
$period = '';
}

# Test for current month specified on command line - this keeps the program
# from failing if the current month is asked for!

chop($thismonth = `/bin/date +%m`);
$thisyear = `/bin/date +%y`;
chop($reqmonth = "$thismonth.$thisyear");
if ($reqmonth == $period) {
$period = '';
}

$servno = 0;
# Open the file for first PM
$server = $servlist[$servno];
$servno += 1;
while ($server) {
open(IN, "$logdir/$server$period") ||
die "Could not open file $server\n";

$begin_record = 1;
$end_record = 0;

# Variables
# $date - 09/11/75 format
# $thismonth - the current month in MM format
# $daytime - hh:mm:ss format of _logout_ time
# $username
# $time - time online
# $port - port they logged in on
# $inbyte - Acct-Input-Octets
# $outbyte - Acct-Output-Octets

print("\nActivity log for user $testuser on $server\n\n");
print("Date Logout BytesIn BytesOut Minutes TOTAL Disconnect\n");
print(" Online Hours\n");
print("---------------------------------------------------------------------------\n");

while (<IN>) {
chop;
if (!length($_)) {
if ($end_record) {
if ($username =~ $testuser) {
$totaltime += $time / 60;
printf("%-8.8s %-8.8s %-8s %8s %-7.7s %-7.7s %-12s\n",
$date, $daytime, $inbyte, $outbyte, $time, $totaltime, $disconnect);
}
}
$end_record = 0;
$begin_record = 1;
next;
}
if ($begin_record && /^[a-zA-Z]/) {
($wday, $month, $mday, $daytime, $year) = split;
$month = &convert_month($month);
$year =~ s/19//;
$date = sprintf("%2.2d/%2.2d/%2.2d", $month, $mday, $year);
$begin_record = 0;
$end_record = 1;
next;
}
if ($begin_record) {
next;
}

if (/User-Name/) {
s/[^\"]*"([^\"]*).*/$1/;
s/\s+//g;
$username = sprintf("%-8s", $_);
next;
}

if (/NAS-Port /) {
s/NAS-Port = //;
s/\s+//g;
$port = $_;
next;
}

# Acct-Terminate-Cause = User-Request

if (/Acct-Input-Octets/) {
s/Acct-Input-Octets = //;
s/\s+//g;
$inbyte = $_;
next;
}

if (/Acct-Output-Octets/) {
s/Acct-Output-Octets = //;
s/\s+//g;
$outbyte = $_;
next;
}

if (/Acct-Terminate-Cause/) {
s/Acct-Terminate-Cause = //;
s/\s+//g;
$disconnect = $_;
next;
}

if (/Acct-Status-Type/) {
if (!/Stop/) {
$begin_record = $end_record = 0;
next;
}
}

if (/Acct-Session-Time/) {
s/Acct-Session-Time = //;
s/\s+//g;
$time = $_ / 60;
next;
}
}
$server = $servlist[$servno];
$servno += 1;
}

sub convert_month {
local($_) = $_[0];
if ($_ eq "Jan") { "01"; }
elsif ($_ eq "Feb") { "02"; }
elsif ($_ eq "Mar") { "03"; }
elsif ($_ eq "Apr") { "04"; }
elsif ($_ eq "May") { "05"; }
elsif ($_ eq "Jun") { "06"; }
elsif ($_ eq "Jul") { "07"; }
elsif ($_ eq "Aug") { "08"; }
elsif ($_ eq "Sep") { "09"; }
elsif ($_ eq "Oct") { "10"; }
elsif ($_ eq "Nov") { "11"; }
elsif ($_ eq "Dec") { "12"; }
else { "-1"; }
}