-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_apache.pl
More file actions
executable file
·144 lines (120 loc) · 3.18 KB
/
parse_apache.pl
File metadata and controls
executable file
·144 lines (120 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/local/bin/perl5 -w
#
# Author: Floyd Moore (redfc.hp.com)
# $Header:$
# Description:
# Analyze the apache logs
#
# "parse_apache.pl" created by red
#
# $Log:$
#
use strict;
use subs qw(handler show_usage parse_options file_mtime);
use POSIX qw(strftime);
use Time::Local;
use vars qw($opt_l $opt_m $opt_x $opt_v $opt_V);
use vars qw($client $identuser $authuser $date $time $tz $method $url);
use vars qw($protocol $status $bytes $rest);
my %Clients;
my %Pages;
my $page_limit=5;
my ($Rev, $RunDate, $DirName, $ProgName);
use vars qw($LogFile $datestamp);
$RunDate = strftime '%Y/%m/%d %H:%M:%S', localtime;
$Rev = (split(' ', '$Revision: 2 $', 3))[1];
$0 =~ m!(.*)/!; $ProgName = $'; $DirName = $1; $DirName = '.' unless $DirName;
$SIG{'HUP'} = \&handler;
$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
$SIG{'TERM'} = \&handler;
use Getopt::Std;
sub handler
{
my($sig) = @_;
warn "$ProgName:INFO: Caught a SIG$sig -- shutting down\n";
exit(0);
}
sub show_usage
{
print "$ProgName $Rev\t\t$RunDate\n";
print "$ProgName [-x] [-v] [-V] [-m <machine_name> [-l logfile] \n";
print " -m <machine_name> : check machine\n";
print "\n";
exit 0;
}
# my options parser
sub parse_options
{
if ( $#ARGV > 0 && $ARGV[0] =~ "-help"){
&show_usage();
exit(1);
}
unless (&Getopt::Std::getopts('Vvxl:m:')) {
&show_usage();
exit(1);
}
if ($opt_V) { die "$ProgName $Rev\n"; };
}
sub file_mtime {
my $filename = shift;
# (stat("file"))[9] returns mtime of file.
return (stat($filename))[9];
}
######################################
# Main Program #####################
######################################
# Global variables
parse_options;
$LogFile="/var/opt/apache/logs/access_log";
if (defined($opt_l)){
$LogFile=$opt_l;
}
if ( ! -r $LogFile) {
die "Cannot read Apache logfile $LogFile\n";
}
open (LOG, "<$LogFile" ) ||
die "Cannot open file $LogFile for read access\n";
print "# $ProgName $Rev\t\t$RunDate\n\n";
while (<LOG>){
chomp;
#print "Line: $_\n";
($client, $identuser, $authuser, $_) =
split(/ /, $_, 4);
($date, $time, $tz, $method, $url, $protocol, $status, $bytes) =
/\[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] "(\S+) (.*?) (\S+)" (\S+) (\S+)$/;
#($client, $identuser, $authuser, $date, $time, $tz, $method, $url,
# $protocol, $status, $bytes) =
#/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+) "(\S+) (.*?) (\S+)"
# (\S+) (\S+)$/;
print "Client: $client\n";
if ( exists($Clients{$client})){
$Clients{$client}++;
} else {
$Clients{$client}=1;
}
print " url = $url\n";
if ( exists($Pages{$url})){
$Pages{$url}++;
} else {
$Pages{$url}=1;
}
print " date = $date at $time\n";
print " status= $status\n";
if ($status == 200){
print " bytes= $bytes\n";
}
}
print "===============================\n";
print "Client Stats:\n";
for my $key (sort keys %Clients){
printf "%-40s: %d\n", $key, $Clients{$key};
}
print "===============================\n";
print "Page Stats:\n";
for my $key (sort keys %Pages){
if ($Pages{$key} > $page_limit){
printf "%-40s: %d\n", $key, $Pages{$key};
}
}
__END__