Question

I am working on an assignment where i need to parse a log file and create a website based on said log file. one of the requirements is that i count the number of hits that happened on yesterdays, im lost when it comes to this ive attached my code and the log file im working with hoping that someone can offer some advice, thanks

#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_DAY);


my $yesterday = localtime() - ONE_DAY();
print $yesterday;




open(LOGFILE,"<", "access.log")or die"Could not open log file.";
my $yesterdayHits=0;

my $totalhits=0;
my $webPage='log.html';

open(WEBPAGE,">",$webPage);
print WEBPAGE ("<HEAD><TITLE>Access Counts</TITLE></HEAD>");
print WEBPAGE ("<BODY>");
print WEBPAGE ("<H1> today is: ",scalar(localtime), "</H1>");
print WEBPAGE ("<h3>Yesterday was $yesterday</h3>");
print WEBPAGE ("<TABLE BORDER=1 CELLPADDING=10 width='500px'>");

foreach my $line (<LOGFILE>) {
    $totalhits++;
    my $w = "(.+?)";
    $line =~ m/^$w $w $w \[$w:$w $w\] "$w $w $w" $w $w/;

    my $site     = $1;
    my $logName  = $2;
    my $fullName = $3;
    my $date     = $4;
    my $time     = $5;
    my $gmt      = $6;
    my $req      = $7;
    my $file     = $8;
    my $proto    = $9;
    my $status   = $10;
    my $length   = $11;
    #if($line =~ m/$yesterday/){$yesterdayHits++}


print WEBPAGE ("<Tr><TD>$site</TD><TD>$line</TD></Tr>\n\n");


}



close(LOGFILE);
print WEBPAGE ("<h2>Total hits: $totalhits</h2>");
print WEBPAGE ("<h3>Hits Yesterday: $yesterdayHits</h3>");
print WEBPAGE ("</TABLE></P>");
print WEBPAGE ("</BODY></HTML>");
close(WEBPAGE); 

Access log

66.249.65.107 - - [11/Nov/2012:19:33:01 -0400] "GET /support.html HTTP/1.1" 200 11179 
111.111.111.111 - - [11/Nov/2012:19:33:01 -0400] "GET / HTTP/1.1" 200 10801
111.111.111.111 - - [08/Oct/2007:11:17:55 -0400] "GET /style.css HTTP/1.1" 200 3225
123.123.123.123 - - [26/Apr/2000:00:23:48 -0400] "GET /pics/wpaper.gif HTTP/1.0" 200 6248
123.123.123.123 - - [26/Apr/2000:00:23:40 -0400] "GET /asctortf/ HTTP/1.0" 200 8130
123.123.123.123 - - [26/Apr/2000:00:23:48 -0400] "GET /pics/5star2000.gif HTTP/1.0" 200 4005
123.123.123.123 - - [26/Apr/2000:00:23:50 -0400] "GET /pics/5star.gif HTTP/1.0" 200 1031
123.123.123.123 - - [26/Apr/2000:00:23:51 -0400] "GET /pics/a2hlogo.jpg HTTP/1.0" 200 4282
123.123.123.123 - - [26/Apr/2000:00:23:51 -0400] "GET /cgi-bin/newcount?jafsof3&width=4&font=digital&noshow HTTP/1.0" 200 36 
172.16.130.42 - - [26/Apr/2000:00:00:12 -0400] "GET /contacts.html HTTP/1.0" 200 4595 
10.0.1.3 - - [26/Apr/2000:00:17:19 -0400] "GET /news/news.html HTTP/1.0" 200 16716
129.21.109.81 - - [26/Apr/2000:00:16:12 -0400] "GET /download/windows/asctab31.zip HTTP/1.0" 200 1540096
192.168.198.92 - - [22/Dec/2002:23:08:37 -0400] "GET / HTTP/1.1" 200 6394
192.168.198.92 - - [22/Dec/2002:23:08:38 -0400] "GET /images/logo.gif HTTP/1.1" 200 807
192.168.72.177 - - [22/Dec/2002:23:32:14 -0400] "GET /news/sports.html HTTP/1.1" 200 3500 
192.168.72.177 - - [22/Dec/2002:23:32:14 -0400] "GET /favicon.ico HTTP/1.1" 404 1997
192.168.72.177 - - [04/Nov/2012:23:32:15 -0400] "GET /style.css HTTP/1.1" 200 4138
192.168.72.177 - - [22/Dec/2002:23:32:16 -0400] "GET /js/ads.js HTTP/1.1" 200 10229
192.168.72.177 - - [22/Dec/2002:23:32:19 -0400] "GET /search.php HTTP/1.1" 400 1997
127.0.0.1 - - [10/Apr/2007:10:39:11 +0300] "GET / HTTP/1.1" 500 606 
127.0.0.1 - - [10/Apr/2007:10:39:11 +0300] "GET /favicon.ico HTTP/1.1" 200 766 
139.12.0.2 - - [10/Apr/2007:10:40:54 +0300] "GET / HTTP/1.1" 500 612 
139.12.0.2 - - [10/Apr/2007:10:40:54 +0300] "GET /favicon.ico HTTP/1.1" 200 766 
127.0.0.1 - - [10/Apr/2007:10:53:10 +0300] "GET / HTTP/1.1" 500 612
127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET / HTTP/1.0" 200 3700 
127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET /style.css HTTP/1.1" 200 614
127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET /img/pti-round.jpg HTTP/1.1" 200 17524
127.0.0.1 - - [10/Apr/2007:10:54:21 +0300] "GET /unix_sysadmin.html HTTP/1.1" 200 3880
217.0.22.3 - - [04/Nov/2012:10:54:51 +0300] "GET / HTTP/1.1" 200 34 
217.0.22.3 - - [10/Apr/2007:10:54:51 +0300] "GET /favicon.ico HTTP/1.1" 200 11514
217.0.22.3 - - [10/Apr/2007:10:54:53 +0300] "GET /cgi/pti.pl HTTP/1.1" 500 617 
127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET / HTTP/0.9" 200 3700 
217.0.22.3 - - [10/Apr/2007:10:58:27 +0300] "GET / HTTP/1.1" 200 3700 
217.0.22.3 - - [10/Apr/2007:10:58:34 +0300] "GET /unix_sysadmin.html HTTP/1.1" 200 3880 
217.0.22.3 - - [10/Apr/2007:10:58:45 +0300] "GET /talks/Fundamentals/read-excel-file.html HTTP/1.1" 404 311 
Était-ce utile?

La solution

use POSIX;

$yesterday = strftime("%d/%b/%Y",localtime(time()-86400));

$yesterday now contains yesterday's date in the logfile's format (e.g. "11/Nov/2012"). You can filter lines by checking $line =~ /$yesterday/;

http://perldoc.perl.org/POSIX.html

Autres conseils

A basic approach is to modify the $yesterday variable (to be the same as logfile) like this :

$yesterday =~ s!\w+\s+(\w+)\s+(\d+)\s+\d{2}:\d{2}:\d{2}\s+(\d+)!$2/$1/$3!;

now you can de-comment and change the line

if ($date eq $yesterday) { $yesterdayHits++ }

to start counting.

Not-so-fast but quite precise version; uses UNIX timestamps:

#!/usr/bin/env perl
use strict;
use warnings 'all';

use HTTP::Date;
while (<>) {
    my $stamp;
    print if
        m{\s+\[(.+?)\]\s+\"}x
        and $stamp = str2time($1)
        and $stamp > time - 86_400 * 2
        and $stamp < time - 86_400;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top