Gossamer Forum
Home : General : Perl Programming :

Getting hits

Quote Reply
Getting hits
I know that this script is probably a security risk because I am using the system command but what I am trying to do is get the hits of a webpage. They read from the access logs and puts the output into a table. My problem is that it isn't getting the correct number. Each access file is the name of the domain.

system("ls /var/log/apache/access/* > found");

open (INF, "found");
@grok = <INF>;
close(INF);

foreach $i (@grok) {
$i =~ s/.*[\/\\](.*)/$1/;
$count = system("grep -c GET $i");
print "<tr><td><font size=1>$i</font></td><td><font size=1>$count</font></td></tr>";
}

I know that I am only grepping the file when I should be grepping the path and the file but I couldn't get it to all work together. Could somebody see what I am doing wrong. Somebody said that you couldn't put the count from a grep into a variable. Any ideas? I can't use modules as I am in a class learning basic perl and I don't want to get to far ahead with modules. So if possible I would like to use the system command.
Quote Reply
Re: [constantm] Getting hits In reply to
You could just count the file lines instead of grepping/regexing etc...will that do what you want?

Each line in the log is a hit so counting the lines should be accurate enough.
Quote Reply
Re: [Paul] Getting hits In reply to
Yes you are correct I could do that. Could you give me a couple of lines on how to do it. This is what I have so far

system("ls /var/log/apache/access* > found");
open (INF, "found");
@grok = <INF>;
close(INF);

foreach $i (@grok) {
$i =~ s/.*[\/\\](.*)/$1/;
}

Now I got to figure out a loop that will open up a file and count for each domain.
Quote Reply
Re: [constantm] Getting hits In reply to
Paul I got it. Thanks for your help. I appreciate it. Here is what I came up with

system("ls /var/log/apache/access/* > found");

open (INF, "found");
@grok = <INF>;
close(INF);

foreach $i (@grok) {
$count = 0;
open (COUNT, "$i");
@count = <COUNT>;
close(COUNT);

foreach $line(@count) {
$count++
}

$i =~ s/.*[\/\\](.*)/$1/;
print "<tr><td><font size=1>$i</font></td><td><font size=1>$count</font></td></tr>";
}
Quote Reply
Re: [constantm] Getting hits In reply to
Im not sure what that code is supposed to be doing to be honest but it will be slow (it looks like you are putting all file names into found then looping found and opening all the files etc?). To count the lines in a file you can simply use:

Code:
my $lines = 0;

open(FILE, $filename) or die $!;
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;

Last edited by:

Paul: Jun 26, 2002, 8:23 AM
Quote Reply
Re: [Paul] Getting hits In reply to
Thanks for the code. Yes you were right about what my code was doing. I knew when I made it that it looked kinda sloppy. I am going to try your code tonight.
Quote Reply
Re: [constantm] Getting hits In reply to
Do you not have one access log to read from?....normally with apache you have access_log, error_log etc....or are you wanting to read old logs too?

Last edited by:

Paul: Jun 26, 2002, 8:59 AM
Quote Reply
Re: [Paul] Getting hits In reply to
I set it up so I have one directory and in there are all the access logs. That is why I do the ls command is to get the list of files. Everything is working right now with the perl script so I am going to try your code tonight just to see how it works because mine is sloppy.
Quote Reply
Re: [constantm] Getting hits In reply to
Code:
my $dir = '/path/to/apache/access';
my @logs = ();
my $lines = 0;

opendir DIR, $dir or die "Cannot read $dir: $!\n";
@logs = grep { ! /^\./ } readdir(DIR);
closedir DIR;

for my $log (@logs) {
open LOG, $log or die "Cannot read log: $!\n";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close LOG;
}

Something like that should work. It also lets you get rid of the system command.

Last edited by:

Paul: Jun 26, 2002, 10:24 AM
Quote Reply
Re: [Paul] Getting hits In reply to
Defiantely use a while statement... I can't imagine trying to put the whole access log into memory as an array. :)