Re: Userlog

Joe Hartley (jh@metheny.brainiac.com)
Thu, 14 Nov 96 09:59:56 EST

> I'm wondering.. Has this been modified or updated at all to handle
> multiple detail files? Is there a later version wifh more functionality
> available?

I've added the ability to query one or both of our PM's. Since most users
will dial into the same POP over and over, it's helpful to specify the PM
if know. I can also specify "all" and have it work through the list
of PMs hardcoded in the script. There's probably a way to have the
script default to "all", even if a date is specified, but I'm not that
slick with perl and don't have the time to tweak it, so a PM name or the
parameter "all" is required.

I didn't think this was a big enough mod to be worth posting, so I
hadn't submitted it! This is being copied to the PM list just in case
someone else wants it. As always, thanks to Dave Andersen for the
original script.

Here 'tis:

#!/usr/local/bin/perl
##
# userlog - this program prints a log of a user's activity based on
# RADIUS detail logs.
#
# Usage: userlog username PMname|all [mm.yy]
# where username = the login of the user in question
# PMname = the name of a PortMaster with a RADIUS file
# all = specifies the use of all PM's in servlist
# (Note - either a PMname or all MUST be specified)
# mm.yy = the numeric month and year of a previous period
# (optional - if omitted, the current detail file is
# used. To use this option, detail files from previous
# months must be named in the form detailmm.yy)
#
# 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>
##
# 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");
}
else {
@servlist = ($ARGV[1]);
}

# Set the period variable, if any
if ($ARGV[2]) {
$period = $ARGV[2];
}
else {
$period = '';
}

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

$begin_record = 1;
$end_record = 0;

# Variables
# $date - 09/11/75 format
# $daytime - hh:mm:ss format of _logout_ time
# $username
# $time - time online

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

while (<IN>) {
chop;
if (!length($_)) {
if ($end_record) {
if ($username =~ $testuser) {
$totaltime += $time / 60;
printf("%-8.8s %-8.8s %-8.8s %-7.7s %-7.7s\n",
$date, $daytime, $username, $time, $totaltime);
}
}
$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 (/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"; }
}

========================================================================
Joe Hartley - jh@brainiac.com - brainiac services, inc
PO Box 5069 : Greene, RI : 02827 - vox 401.539.9050 : fax 401.539.2070
Without deviation from the norm, "progress" is not possible. - FZappa