Logging user name & login and logout times

Hello y’all

I’m an administrator of a university microscope facility where the 'scopes
are hooked up to two G4 Macs running OS 10.2. I need to track usage for
both billing and in order to keep track of who is breaking the microscopes, and
so I need a fairly simple script to merely log users and times of login and logout.

I had hoped to run this entirely using AppleScript, since I can hack that without
a great deal of extra effort. However, it is looking like I can’t track logout events
so easily…while I could use a script that ran at startup to grab the time of startup,
I can’t figure out a way to get the username of the current user…or to log the
time when the user shuts down the system.

Questions:

  1. Can I use the OS X logs to track usage? I can parse logs containing the information
    just fine if I know how to deal with them. I think the system.log (when I can find it)
    has the info, but it doesn’t seem to last beyond shutdown. Any clues about how to deal
    with the problem this way?

  2. Is there are way to track user name, login and logout times using Applescript?
    This seems like it should be possible…isn’t there an AppleEvent for shutdown?
    And while I can’t seem to find it, shouldn’t it be mind-numbingly simple to find
    out who is currently logged in?

  3. I got a shell script to track some of this from a Mike’s OS X Management site, one
    a logout script by Jason Prell:

++++++++++++++++++++++++++++++++++++++++++++

 # If user is admin or root, exit script 
 if [ $1 = "admin" ] || [ $1 = "root" ]; then 

exit
fi

------------------------------------------

Mac Logout Tracker

------------------------------------------

mkdir /private/var/tmp/macstats
mount -t afp afp://user:pwd@server/share /private/var/tmp/macstats
NODENAME=grep APPLETALK_HOSTNAME /etc/hostconfig | cut -d '=' -f 2
LOGINNAME=$1

------------------------------------------

Sets file handles

------------------------------------------

 PREFIX=$NODENAME 
 SUFFIX=txt 
 FILENAME="$PREFIX.$SUFFIX" 

------------------------------------------

Sets time values

------------------------------------------

 RIGHTNOW=`date +%m/%d/%y%t%H:%M:%S%t%s` 
 more /.local/macstats/$FILENAME>>/private/var/tmp/macstats/$FILENAME 
 echo "LOGOUT $NODENAME $LOGINNAME $RIGHTNOW">>/private/var/tmp/macstats/$FILENAME 
 umount -f /private/var/tmp/macstats 
 rm -R /private/var/tmp/macstats 
 rm -R /.local/macstats/* 
 rm -R /Library/Caches/* 
 lookupd -flushcache 

+++++++++++++++++++++++++++++++++++++++++++++

and another login script from the same site (here’s a section of it):

+++++++++++++++++++++++++++++++++++++++++++++

#*****************************************************

OPEN A FILE FOR LOGGING

#*****************************************************

open (outputFile, “>/private/var/root/Documents/Scripts/loginScript.out”);
print outputFile “----------------------------n”;
print outputFile date;

$argCount = $#ARGV + 1;

If there are no input arguments, log it and exit

if ($argCount < 1)
{
print outputFile “No input argument. Exiting…n”;
print outputFile “----------------------------n”;
close outputFile;
exit(0);
};

#*****************************************************

end OPEN A FILE FOR LOGGING

#*****************************************************

#*****************************************************

GET INFORMATION ON LOGGED IN USER

#*****************************************************

First argv input is username from LoginWindow

$userLoggedIn = $ARGV[0];
print outputFile "User logged in: ", $userLoggedIn, “n”;

Get fullname, user id number and group id number of user

$fullName = (getpwnam($userLoggedIn))[6];
$uidNum = (getpwnam($userLoggedIn))[2];
$gidNum = (getpwnam($userLoggedIn))[3];
print outputFile "tFull name: ", $fullName, “n”;
print outputFile "tUID num: ", $uidNum, “n”;
print outputFile "tGID num: ", $gidNum, “n”;

#*****************************************************

end GET INFORMATION ON LOGGED IN USER

#*****************************************************

++++++++++++++++++++++++++++++++++++++++++++++

but I am loathe to try to throw together a UNIX script and install it as a login or
logout script without being pretty damn confident that it won’t bolix up the works
hugely.

When I started trying to do this, it seemed like it would be impossibly simple. I mean,
aren’t these pretty fundamental values? And shouldn’t there be an easy way to keep
track of them? I’m not even that worried about security, just desperate to get access
to these values so I can open the facility fully for business.

Any help you could provide would be greatly appreciated. One person sent me a single
line of Applescript code that opened up a log file that seemed to have the information
I needed…but I lost the email in a recent hard disk crash. If this is as simple as saving
a log file and the parsing it for logins and logouts, that would be fabulous. The more
I look at this, though…

Again, thanks in advance,

Richard

I have no experience with what you are attempting to do but it seems like it could be handled fairly easily with a shell script/osascript/applescript/cron combination. Cron could run the script every x seconds or minutes to determine the current user and time and then write the results to a log file. Maybe something like this:

set userInfo to do shell script "whoami;date"
set ptd to path to desktop as string
set logFile to (ptd & "user_log")
try
	set f to open for access file logFile with write permission
	write (userInfo & return) to f starting at eof
	close access f
on error
	try
		close access f
	end try
end try

I’ll leave the cron stuff to someone who is more skilled. :wink:

– Rob

You might be able to use something simple like the “last” command redirected to a text file…

do shell script "last >>usagelog.txt"

This script would save the output to a text file named “usagelog.txt” at the root folder of your drive. You could save it to any directory like this…

do shell script "cd /Path/to/Save_It_Here/;last >>usagelog.txt"