I have a custom monitor written that runs every five minutes on several web servers. The very first time it runs it gets the timestamp from the last line of the access file. Five minutes later, it opens the log file, reads it line by line till it gets to that timestamp, and then tallys up the unique users from that point forward.
Some of these logs are very large and the process takes close to 10 seconrds to run on them. I'd like to rewrite it and make it smarter. I started to do this using seek and tell but then hit a snag.
What happens if the log rotates? I'll have to watch for that. I was thinking I could do this by keeping the first line of the log file and if it doesn't match on the next run then I know it's a new log.
My questions. Is seek and tell significantly faster than simply walking through the file with a while loop until I get to where I need to be?
Could someone give me a sample of how I would rewrite this part to do this? I'd appreciate it. I hate creating a bigger than necessary CPU hit on the server.
I was thinking at a minimum, my "next if" statement could be a lot better.
Thanks bunches,
Derek
$FLAG = 0;
# Get last timestamp
if (-e "$OutputDir/$NAME/last_timestamp") {
open LAST, "<$OutputDir/$NAME/last_timestamp";
$last_timestamp = <LAST>;
chomp $last_timestamp;
close LAST;
}
else {
# Prime the Pump
$data = `tail -1 $LOG`;
@data = split(/\ /,$data);
open LAST, ">$OutputDir/$NAME/last_timestamp";
print LAST "$data[3]";
close LAST;
exit;
}
if (-e "$LOG") {
open LOG, "<$LOG";
while (<LOG>) {
@data = split(/\ /);
next if (($data[3] ne $last_timestamp)&&($last
+_timestamp)&&(!$FLAG));
$FLAG=1;
next if (($skip)&&($_ =~ m/$skip/));
$last_timestamp = $data[3];
$login_id{$data[2]}++;
}
close LOG;
@Result = keys %login_id;
print "USERS=$#Result\n";
open LAST, ">$OutputDir/$NAME/last_timestamp";
print LAST "$last_timestamp";
close LAST;
}
else {
print "LOG FILE NOT FOUND\n";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.