tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
* SUMMARY: "last" command woes
@ 2001-04-12 18:24 Nikola Milutinovic
  0 siblings, 0 replies; only message in thread
From: Nikola Milutinovic @ 2001-04-12 18:24 UTC (permalink / raw)
  To: tru64-unix-managers

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: multipart/mixed; boundary="--------------9CC10BB5EDB5B6C77E031D03", Size: 6130 bytes --]

This is a multi-part message in MIME format.
--------------9CC10BB5EDB5B6C77E031D03
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Thanks to:

"Ahmed, Mohamed K." <MAHMED@thcg.org>
Richard Loken <tech@athabascau.ca> 
George Gallen <ggallen@slackinc.com> 
Paul Crittenden <crittend@simpson.edu>
Serguei Patchkovskii <patchkov@ucalgary.ca> !!!
Kevin Dea <kdea@alpine-la.com>

Most people sugested that they saw that and that the only permanent cure was to
clear out wtmp.

# cd /var/adm
# cp wtmp wtmp.1
# cat /dev/null > wtmp (or echo "\c" > wtmp)

Mohamed Ahmed pointed me to some additional tools that could help (fwtmp).
Unfortunately, I didn't install the Accounting Utilities, so I can't tell right
now. It's worth a try.

Serguei Patchkovskii provided a Perl script that is, in his words, more robust
than "last". I'm attaching it to this message, since it is small enough and
might be valuable to all of us.

Nix.
--------------9CC10BB5EDB5B6C77E031D03
Content-Type: application/x-perl;
 name="glast.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="glast.pl"

#!/freeware/bin/perl -w

#            ut_user ut_id ut_line ut_type ut_pid e_termination e_exit time_t ut_host
$UTlength  = 32     +14   +32     +2      +4     +2            +2      +4    +64 ;
$UTSep     = "<:::::>" ;
$wtmpName  = "/var/adm/wtmp" ;
%userGuess = () ;  # Filled by GuessUser as necessary

sub ruSendRecord { # internal procedure for ReadUtmp
   local ($line,$logout) = @_ ;

   if( ! defined($user) || $user eq $users{$line} || 
       (substr($users{$line},0,1) eq '?' && substr($users{$line},1) eq substr($user,1) ) ){
      push @entries, join($UTSep,($host,$users{$line},$line,$login_time{$line},
                                  $logout,$login_host{$line})) ;
      }
   delete $users{$ut_line} ;
   }

sub ReadUtmp {
   local ($name,$host,$user) = @_ ;
   local (*FILE,@entries) ;
   local ($ut_user,$ut_id,$ut_line,$ut_type,$ut_pid,
          $e_termination,$e_exit,$ut_time,$ut_host) ;
   local (%users,%login_time,%login_host) ; # All indexed by connection ID

   open(FILE,"< $name") || die "Can't open $name for reading: $!\n" ;
   while( read(FILE,$ut_entry,$UTlength) == $UTlength ){
      ($ut_user,$ut_id,$ut_line,$ut_type,$ut_pid,
       $e_termination,$e_exit,$ut_time,$ut_host) =
#             ut_user ut_id ut_line ut_type ut_pid e_termination e_exit time_t ut_host
      unpack("A32     A14   A32     s       l      s             s      L      A64",
             $ut_entry) ;
      if( $ut_type == 2 ){ # Boot
         foreach $ut_line (keys(%users)){
            &ruSendRecord( $ut_line, $ut_time ) ;
            }
#        push @entries, join($UTSep,($host,"","reboot",$ut_time,$ut_time,"")) ;
         }
      if( $ut_type == 7 ){ # Login
         $ut_user = &GuessUser($ut_user) ;
         if( defined($users{$ut_line}) ){ # User typed 'login' at the shell prompt
            &ruSendRecord( $ut_line, $ut_time ) ;
            }
         else {
            $login_host{$ut_line} = $ut_host ;
            }
         $users{$ut_line}      = $ut_user ;
         $login_time{$ut_line} = $ut_time ;
         }
      if( $ut_type == 8 ){ # Logout
         if( defined($users{$ut_line}) ){
            &ruSendRecord( $ut_line, $ut_time ) ;
            }
         }
      }
   close(FILE) ;
#  Currently logged in users
   foreach $ut_line (keys(%users)){
      &ruSendRecord( $ut_line, 0 ) ;
      }
   @entries
   }

sub GuessUser {
   local ($user) = @_ ;
   local ($letter,$test,@matches) ;

   return ($user) unless( $user =~ /^\0/ ) ;
   return ($userGuess{$user}) if( defined($userGuess{$user}) ) ;
   for $letter ('a' .. 'z'){
      $test = $letter . substr($user,1) ;
      push(@matches,$test) if( defined(getpwnam($test)) ) ;
      }
   $test             = '?' . substr($user,1) ;
   $test             = $matches[0] if( $#matches == 0 ) ;
   $userGuess{$user} = $test ;
   $test
   }

sub btReverseLoginTime {
   local ($aLogin) = (split($UTSep,$a))[3] ;
   local ($bLogin) = (split($UTSep,$b))[3] ;

   $bLogin <=> $aLogin
   }

sub LoadWtmps {
   local ($user) = @_ ;
   local (@entries) ;
   local (*FILE) ;

   @entries = &ReadUtmp( $wtmpName, 'localhost', $user ) ;
   sort btReverseLoginTime @entries
   }

sub PrintWtmps {
   local (@entries) = @_ ;
   local ($entry) ;
   local ($host,$user,$line,$login,$logout,$from) ;
   local ($login_ascii,$logout_ascii,$delta,$delta_ascii) ;
   local ($host_ascii) ;
   local ($=) = 1000000 ;

   foreach $entry (@entries){
      ($host,$user,$line,$login,$logout,$from) = split($UTSep,$entry) ;
      $login_ascii  = localtime($login) ;
      $logout_ascii = localtime($logout) ;
      if( $logout == 0 ){
         $logout       = time ;
         $logout_ascii = 'Still logged in' ;
         }
      $host_ascii   = $host ;
      $host_ascii   =~ s/^(cobalt|co)([0-9]+)(\.chem\.ucalgary\.ca)?$/co$2/ ;
      $delta        = $logout - $login ;
      $delta        = 0 if( $delta < 0 ) ;
      $delta_ascii  = sprintf "%dsec", $delta ;
      if( $delta > 99 ){
         $delta /= 60.0 ;
         $delta_ascii = sprintf "%dmin", $delta ;
         if( $delta > 99 ){
            $delta /= 60.0 ;
            $delta_ascii = sprintf "%dhrs", $delta ;
            if( $delta > 47 ){
               $delta /= 24.0 ;
               $delta_ascii = sprintf "%dday", $delta ;
               if( $delta > 14 ){
                  $delta /= 7.0 ;
                  $delta_ascii = sprintf "%dwks", $delta ;
                  }
               }
            }
         }
      write ;
      }
   }

format STDOUT_TOP =
@<<<<<<< @<<<<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>> @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<
'User', 'Node',    'Line', 'Coming From',                 'For ','From',         'To'
.

format STDOUT = 
@<<<<<<< @<<<<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>> @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<
$user,   $host_ascii,$line,$from,                         $delta_ascii,$login_ascii,  $logout_ascii
.


&PrintWtmps(&LoadWtmps($ARGV[0])) ;

--------------9CC10BB5EDB5B6C77E031D03--

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-04-12 18:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-04-12 18:24 SUMMARY: "last" command woes Nikola Milutinovic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox