#!/usr/bin/perl use strict; use Date::Parse; use File::ReadBackwards; 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)); print 'myapp user status' . "\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) { 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 $bw = File::ReadBackwards->new($file) or die "can't + read '$file' $!"; my $found = 0; while( !$found && defined( my $log_line = $bw->readlin +e ) ) { if ($log_line =~ /GET \/myapp\//) { $last_line = $log_line; $bw->close(); } } if ($last_line eq '') { print "No access 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)); # print "Last myapp hit on $lf to $url by $ip +on $date_str \n"; #print "$mon/$dy/$yr $hh:$mm:$ss > $month/$mda +y/$year $hour:$min:$sec ?\n"; my ($domain, $t3, $t4) = split("_", $lf); if ($date_str > ($date_cmp_str - $session_limi +t)) { print "User logged in! $domain last hi +t $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"; } } } }
In reply to Emulating command line pipe by davistv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |