[plug] login stats

Matt Kemner zombie at wasp.net.au
Thu Oct 5 14:25:19 WST 2000


On Thu, 5 Oct 2000, Bret Busby wrote:

> What I am seeking, is advice as to how to perform the "time arithmetic",
> on the fields (finding the time elapsed from the start time of a current
> login session, and, adding times, such as the times displayed in the
> last column of the report that is displayed, when last is run).

As previously suggested, learn Perl.

Perl is ideal for this sort of text processing, and if you intend to do a
lot of it, the language is well worth learning.

Alternatively, pay someone to write the script for you :)

Anyway, to give you an idea, this is a script I've hacked up in the 10
mins since reading your email:

#!/usr/bin/perl
use HTTP::Date;

$total=0;
foreach $line (<>) {
  chomp $line;
  if ($line =~ s/(... ... .. ..:..)(   still logged in)/$1/) {
    $stop=time;
    $start=str2time($line);
    $diff=int(($stop-$start)/60);
    print "start: $start, stop $stop\n";
  } elsif ($line =~ s/(... ... .. ..:..) - ..:..  (\(..:..\))/$2/) {
    print "time:$line\n";
    ($hrs,$mins)=split (":", $line);
    $diff=($hrs*60)+$mins;
  }
  print "diff: $diff\n";
  $total += $diff;
}

print "\nTotal: $total mins\n";
# End of script

It needs a lot of refining and cleaning up, and you can comment out most
of the print statements (all but the last one are there for debugging),
and it needs to be taught about logins that span more than one day (which
shouldn't be hard), but otherwise, it is working code, or at least it
works for me, for the sample input:

Thu Oct  5 12:33   still logged in   
Thu Oct  5 12:33 - 12:33  (00:00)    
Thu Oct  5 11:16   still logged in   
Wed Oct  4 21:44   still logged in   
Tue Oct  3 16:14   still logged in   
Tue Oct  3 14:11 - 14:11  (00:00)    
Tue Oct  3 08:54   still logged in   
Tue Oct  3 08:52   still logged in   
Mon Oct  2 17:50 - 08:17  (14:26)    
Mon Oct  2 13:41 - 08:17  (18:35) 

which I obtained by running  "last -10 zombie|cut -c40-80"

It also requires the "HTTP/Date.pm" module, which converts text
strings such as "Mon Oct  2 13:41" to the standard unix time format,
and which under Debian can be found in the libwww-perl

 - Matt




More information about the plug mailing list