Hello,

I've got some huge apache log files which I want to search backwards for the last access to a particular url. I can do this from the command line very effectively with this:

tac reall_big_access_log | grep -m 1 "/myapp/"

It's really fast, which is what I like about it. The -m 1 part of the grep command kills tac upon finding the first match, so file reading is minimized.

I've tried several ways of writing a script to interate over these access logs and execute the same statement, but I've had no luck. Each method I've tried that involved system() resulted in the tac command not being killed by the grep -m 1 statement, so the tac commands crawl through the entirety of every log file, which takes a very long time.

Below is my current script, which relies on File::ReadBackwards to emulate tac. This is quite a bit slower than the command line method as well. Could anyone suggest a good way to just execute the command line above inside a script?

Thank You,
Troy

#!/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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.