Article 5783 of comp.lang.perl:
Xref: feenix.metronet.com comp.unix.admin:5527 comp.lang.perl:5783 comp.unix.aix:10433
Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!howland.reston.ans.net!vixen.cso.uiuc.edu!moxley.aiss.uiuc.edu!lance
From:
[email protected] (C Lance Moxley)
Newsgroups: comp.unix.admin,comp.lang.perl,comp.unix.aix
Subject: Summary: Report of Concurrent Users
Followup-To: comp.unix.admin
Date: 11 Sep 1993 03:29:14 GMT
Organization: AISS/Telecom -- University of Illinois at Urbana-Champaign
Lines: 51
Distribution: world
Message-ID: <
[email protected]>
NNTP-Posting-Host: moxley.aiss.uiuc.edu
Thanks to all who helped with my quest for a program to
report the concurrent use of an AIX workstation. To those
who wanted a copy of my solution, below is pretty much
exactly what I was looking for.
Thanks again!
#!/usr/local/bin/perl
#
$utmp_format = "A8 A14 A12 s i s s l A16";
$utmp_length = 8+14+12+2+4+2+2+4+16;
$wtmp_file = "/var/adm/wtmp";
%inuse = (); # List of terminals in use
$inuse = 0; # Count of terminals in use
open (WTMP, "<$wtmp_file") || die "Could not open wtmp file $wtmp_file: $!\n";
while ($nread = read(WTMP, $record, $utmp_length)) {
die "Error reading wtmp file: only $nread bytes returned, $utmp_length expected\n" unless ($nread == $utmp_length);
#
($user, $id, $line, $type, $pid, $termination, $exit, $time, $host) = unpack($utmp_format, $record);
if ($user ne '') {
if ($inuse{$line}) {
warn "Line $line already in use by $inuse{$line}\n";
$inuse{$line} = $user;
} else {
$inuse{$line} = $user;
$inuse++;
&timeprint ($time, $inuse);
}
} else {
if ($inuse{$line}) {
$inuse--;
delete $inuse{$line};
&timeprint ($time, $inuse);
} else {
warn "Line $line was not known to be in use\n";
}
}
}
sub timeprint {
local ($time, $count) = @_;
local (@time) = localtime ($time);
printf "%.2d/%.2d/%.2d-%.2d:%.2d %d\n",
$time[4]+1, $time[3], $time[5], $time[2], $time[1], $count;
}
--
C Lance Moxley Internet:
[email protected]
AISS/Telecommunications BITNET: UNETCLM at UICVMC
University of Illinois UUCP: uunet!uiucuxc!uiuc.edu!clm