Calculating time of employee session from the log date/time stampusing perl

Discussion in 'Perl Misc' started by Jayesh Kamdar, May 24, 2005.

  1. Hi,

    I would appreciate if someone can point me to any perl command/module
    that will let me calculate the session of the person was logged on to
    corporate server. I have included a sample of log.

    employee XXXXX logged in via VPN3000 at 01:37:34 on 04/18/2005 from
    111.111.1.111
    employee XXXX logged out of VPN3000 at 08:12:46 on 04/18/2005

    Thanks,
    Jayesh
     
    Jayesh Kamdar, May 24, 2005
    #1
    1. Advertisements

  2. Regexes would be useful for splitting up the lines of the log:

    perldoc perlretut
    perldoc perlre

    And Date::Manip would help parse and compare the time/date stamps.

    sherm--
     
    Sherm Pendley, May 24, 2005
    #2
    1. Advertisements

  3. use Date::Calc 'Delta_DHMS';
    my (%in, %sessions);

    sub timeparse {
    local $_ = shift;
    my @date = m{at\s+(\d+):(\d+):(\d+)\s+on\s+(\d+)/(\d+)/(\d+)};
    [ @date[5,3,4,0,1,2] ];
    }

    while ( <> ) {
    if ( /^employee\s+(\S+)\s+logged\s+in/ ) {
    $in{$1} = timeparse($_);
    } elsif ( /^employee\s+(\S+)\s+logged\s+out/ and
    exists $in{$1} ) {
    push @{ $sessions{$1} },
    [ Delta_DHMS( @{ delete $in{$1} }, @{ timeparse($_) } ) ];
    }
    }

    for my $empl ( keys %sessions ) {
    print "$empl sessions:\n";
    for ( @{ $sessions{$empl} } ) {
    printf " %d days %d hours %d min %d sec\n", @$_;
    }
    print "\n";
    }
     
    Gunnar Hjalmarsson, May 25, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.