#!/usr/bin/perl
use strict;
use CGI;
use Date::Parse;
my $log_dir = '/var/log/httpd/';
my $session_limit = 3600; # 1 hour
my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$month++;
my $date_cmp_str = join('', ($year, sprintf('%02d', $month), sprintf('%02d', $mday), $hour, $min, $sec));
my $cgiq = new CGI;
print $cgiq->header();
print '
myapp user status';
print 'myapp user status
';
print "\nLast known user activity:
\n\n";
my @expired;
my @active;
opendir(DIR, $log_dir);
my @files = grep { /_access_log$/ } readdir(DIR);
closedir DIR;
print STDERR "Files = " . join(", ", @files) . "\n\n";
sleep(3);
foreach my $lf (@files) {
# make sure this is an myapp site
my ($domain, $t1, $t2) = split("_", $lf);
my $myapp_dir = '/w3/' . $domain . '/myapp/';
if (!-e $myapp_dir) {
next;
}
else {
# Find last login
my $file = $log_dir . $lf;
my $last_line = '';
my $pid = open IN, "tac $file | grep -m 1 /myapp/ |" or die $!;
while( ) {
$last_line = $_;
}
close IN;
if ($last_line eq '') {
# print "No login in logs.\n";
}
else {
my ($ip, $d1, $d2, $date, $tz, $method, $url, $rest) = split(" ", $last_line);
$date = substr($date, 1);
$tz = substr($tz, 0, -1);
my $cdate = $date . ' ' . $tz;
my ($ss,$mm,$hh,$dy,$mon,$yr,$zone) = strptime($cdate);
$yr += 1900;
$mon++;
my $date_str = join('', ($yr, sprintf('%02d', $mon), sprintf('%02d', $dy), $hh, $mm, $ss));
my ($domain, $t3, $t4) = split("_", $lf);
if ($date_str > ($date_cmp_str - $session_limit)) {
print "- User logged in! $domain last hit $mon/$dy/$yr $hh:$mm:$ss by IP $ip
\n";
}
else {
print "- $domain last hit $mon/$dy/$yr $hh:$mm:$ss by IP $ip
\n";
}
}
}
}
print "
";