?? fig12_11.pl
字號:
#!/usr/bin/perl
# Figure 12.11: fig12_11.pl
# Analyzes the Apache access log file
use warnings;
use strict;
use English;
# System settings changed to make output appear correctly
$FORMAT_LINES_PER_PAGE = 25;
$FORMAT_FORMFEED = '';
my $access='C:\Program Files\Apache Group\Apache\logs\access.log';
my $interval = 3;
( $~, $^, $-, $% ) = ( 'PRINTACCESS', 'PRINTACCESS_TOP', 0, 0 );
my ( @entry, %most, %hourly );
# loop that prints each entry and gathers the statistics
open( FILE, $access ) or die( "Cannot open $access" );
while ( <FILE> ) {
next if ( $_ eq "\n" ); # skip blank lines
@entry = parseAccessEntry( $_ );
write();
prompt() if ( $FORMAT_LINES_LEFT == 1 );
$most{ $entry[ 3 ] }++;
my $hour = ( split( /:/, $entry[ 1 ], 3 ) )[ 1 ];
$hourly{ $hour.'requests' }++;
$hourly{ $hour.'transferred' } += $entry[ 6 ]
unless ( $entry[ 6 ] eq '-' );
}
close( FILE ) or die( "Cannot close: $!" );
print( "\n" x ( $- - 1 ) );
prompt();
# printing most frequently accessed statistics
( $~, $^, $-, $% ) = ( 'MOST', 'MOST_TOP', 0, 0 );
my $key;
foreach $key ( sort { $most{ $b } <=> $most{ $a } }
( keys( %most ) ) ) {
write();
prompt() if ( $FORMAT_LINES_LEFT == 1 );
}
print( "\n" x ( $- - 1 ) );
prompt();
# printing hourly access rate and bytes transferred.
( $~, $^, $-, $% ) = ( 'TIME', 'TIME_TOP', 0, 0 );
my ( $requests, $transferred, $startTime );
for ( $startTime = 0; $startTime < 24;
$startTime += $interval ) {
( $requests, $transferred ) = ( 0, 0 );
for ( my $hour = $startTime;
$hour < $startTime + $interval && $hour < 24;
$hour++ ) {
$requests += $hourly{ $hour.'requests' }
if exists( $hourly{ $hour.'requests' } );
$transferred += $hourly{ $hour.'transferred' }
if exists( $hourly{ $hour.'transferred' } );
}
write();
prompt() if ( $- == 1 );
}
print( "\n" x ( $- - 2 ) );
sub parseAccessEntry
{
my $entry = shift;
if ( $entry =~ /^( \d+\.\d+\.\d+.\d+ )\s-\s-\s\[ #IP Address
( [\d\w:\/]+ )\s-\d{4}\]\s\" #time
( \w* )\s #Request type
( [^\s]* )\s #File name
( [^\"]* )\"\s #Protocol
( \d+ )\s #Request result
( \d+|\- )\s*\n$/x ) { #Total bytes
return ( $1, $2, $3, $4, $5, $6, $7 );
}
warn "Not a normal access log entry.\n$entry\n";
return undef;
}
sub prompt
{
print( "--- Press Enter to Continue ---" );
<STDIN>;
$FORMAT_LINES_LEFT--;
}
# format definitions
format PRINTACCESS_TOP =
access.log Page @<<
$%
Requester Date and time Type File Size Code
------------ -------------------- ---- ---- ---- ----
.
format PRINTACCESS =
@<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<< @<<<<<<<<<<<<<... @<<<<<@<<
$entry[0], $entry[1], $entry[2], $entry[3], $entry[6], $entry[5]
.
format MOST_TOP =
Most Accessed Files Page @<<
$%
File Requests
-------------------- --------
.
format MOST =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @|||||||
$key, $most{ $key }
.
format TIME_TOP =
Page @<<
$%
Times Requests Total bytes Transferred
----------- -------- ----------------------
.
format TIME =
@#:00 - @#:00 @||||||| @|||||||||||||||||||||
{ hour( $startTime ), hour( $startTime + $interval ),
$requests, $transferred }
.
sub hour
{
return ( $_[ 0 ] % 12 == 0 )? 12 : ( $_[ 0 ] % 12 );
}
###########################################################################
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. #
# All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
###########################################################################
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -