[ale] perl for patman

Jennifer Taylor JMTaylor at wpo.co.chatham.ga.us
Fri Jul 28 16:06:07 EDT 2000


** Low Priority **

Hey there, patman

Don't bother with the other script I sent you.  I took your friend's advice to its logical conclusion and wrote this one instead.  It's attached and unfortunately had to go thru a windows machine first, so you may need to remove nasty windows line breaks.  
do 
cat -v log.pl and if you see any ^M characters, then do the following:


sed 's/^M//' log.pl > log.cgi

(to print out that ^M, do ctrl+v, then ctrl+m)

That will get rid of them (apologies if you knew this already).

I tested it out on my own apache log scripts, so you'll want to replace 'engineer3.pl' with whatever it is you're grepping for, and you'll want to make sure that where it says '/usr/local/apache/logs' it points to your own apache log directory.  I think that's all you should need to change, unless you want to change the names of the files.  If it doesn't spit out 3 files (tempfile, weekfile, permfile) make sure you've got write permissions on that directory.

Good luck, and thanks for the challenge!!! :)

jenn
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           


#!/usr/bin/perl

##############################################################################
#Script written by djinn 7.28.2000 Destroy at will
##############################################################################

##############################################################################
###This part opens your log file, assigns your grep to a variable, and writes it
#to the temporary file
##############################################################################
##

open (ACCESS, '/usr/local/apache/logs/access_log');
@access = <ACCESS>;
$logno = grep(/engineer3.pl/, @access);
close (ACCESS);

open(OUT, '>tempfile');
print OUT "$logno";
close (OUT);

##############################################################################
###This part compares your current grep with greps of grepping past and tells 
#the script what to do if the number currently in your log file is bigger than
#the number the last time you grepp'd
##############################################################################
##

open (TEMPFILE, 'tempfile');
$tempfile = <TEMPFILE>;
close (TEMPFILE);
open (WEEKFILE, 'weekfile');
$weekfile = <WEEKFILE>;
close (WEEKFILE);
if ($tempfile > $weekfile) {
open (PERMFILE, 'permfile');
$permfile = <PERMFILE>;
close (PERMFILE);

##############################################################################
###If your number from your log file is bigger than that of your weekfile (should 
#be every time but once a week) then you want to take the difference (which is
#the number of new hits) and ADD it to your permanant file from which you 
#publish the hit counter
##############################################################################
##

open (PERMOUT, '>permfile');
print PERMOUT ($tempfile - $weekfile) + $permfile;
close (PERMOUT);

##############################################################################
###THEN you want to update your weekfile to reflect the current number of hits for
#when you start the process all over again.
##############################################################################
##

open (WEEKOUT, '>weekfile');
print WEEKOUT "$tempfile";
close (WEEKOUT);
}

##############################################################################
##############################################################################
####
#Now you're done with the first part, that should handle everything EXCEPT when
#your log file resets itself.  This next part takes care of that.
##############################################################################
##############################################################################
####

elsif ($tempfile < $weekfile) {

##############################################################################
###Add the value gotten from your grep of the log file to the value in the 
#permfile.
##############################################################################
##

open (PERMFILE2, 'permfile');
$permfile2 = <PERMFILE2>;
close (PERMFILE2);
open (LESSPERM, '>permfile');
print LESSPERM $permfile2 + $tempfile;
close (LESSPERM);

##############################################################################
###Now, copy what's in tempfile into your weekfile, and you get to play it again
#from the top, maestro!
##############################################################################
##

open (WEEKOUT2, '>weekfile');
print WEEKOUT2 "$tempfile";
close (WEEKOUT2);
}




##############################################################################
##Please note: Because the filehandles WEEKOUT and PERMFILE and variable $permfile are 
##contained in separate "if" statements, it is really not necessary to append a "2" to the end.
##However, with my luck, the parser would get confused if I didn't, so there ya go.
##############################################################################





More information about the Ale mailing list