summaryrefslogtreecommitdiffstats
path: root/contrib/make_combined_log.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/make_combined_log.pl')
-rw-r--r--contrib/make_combined_log.pl139
1 files changed, 0 insertions, 139 deletions
diff --git a/contrib/make_combined_log.pl b/contrib/make_combined_log.pl
deleted file mode 100644
index 2c55271..0000000
--- a/contrib/make_combined_log.pl
+++ /dev/null
@@ -1,139 +0,0 @@
1#!/usr/bin/perl
2
3# $Id: make_combined_log.pl,v 1.2 2004/02/12 23:32:55 urkle Exp $
4#
5# make_combined_log.pl
6#
7# Usage: make_combined_log <days> <virtual host>
8#
9# This perl script extracts the httpd access data from a MySQL database
10# and formats it properly for parsing by 3rd-party log analysis tools.
11#
12# The script is intended to be run out by cron. Its commandline arguments tell
13# it how many days' worth of access records to extract, and which virtual_host
14# you are interested in (because many people log several virthosts to one MySQL
15# db.) This permits you to run it daily, weekly, every 9 days -- whatever you
16# decide.
17#
18# Note: By "days" I mean "chunks of 24 hours prior to the moment this script is
19# run." So if you run it at 4:34 p.m. on the 12th, it will go back through 4:34
20# p.m. on the 11th.
21#
22# Known issues:
23# * Because GET and POST are not discriminated in the MySQL log, we'll just
24# assume that all requests are GETs. This should have negligible effect
25# on any analysis software. This could be remedied IF you stored the full
26# HTTP request in your database instead of just the URI, but that's going to
27# cost you a LOT of space really quickly...
28#
29# * Because this is somewhat of a quick hack it doesn't do the most robust
30# error checking in the world. Run it by hand to confirm your usage before
31# putting it in crontab.
32
33$| = 1;
34
35use DBI;
36
37# Remove the # in front of this line when you have
38# edited the variables below.
39#$has_edited_source = 1;
40#
41# Set up the proper variables to permit database access
42#
43$serverName = "your.dbhost.com";
44$serverPort = "3306";
45$serverUser = "someuser";
46$serverPass = "somepass";
47$serverTbl = "acc_log_tbl";
48$serverDb = "apache";
49
50if (!defined($has_edited_source)) {
51 print "Please edit this file and configure it first.\n";
52 print "This program is $0\n";
53 exit 1;
54}
55# Remember, $#ARGV is parameters minus one...
56if ($#ARGV != 1) {
57 print "Usage $0 days virtualhost\n";
58 exit 1;
59}
60
61$days = $ARGV[0];
62$virthost = $ARGV[1];
63
64#
65# Other constants
66#
67$st_tz = "-0800";
68$dt_tz = "-0700";
69
70$now = time();
71$start = $now - (86400 * $days);
72
73#
74# Connect and fetch the records
75#
76$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
77if (not $dbh) {
78 die "Unable to connect to the database. Please check your connection variables. (Bad password? Incorrect perms?)";
79}
80
81$records = $dbh->prepare("select remote_host,remote_user,request_uri,time_stamp,status,bytes_sent,referer,agent,request_method,request_protocol from `$serverTbl` where virtual_host='$virthost' and time_stamp >= $start order by time_stamp");
82$records->execute;
83if (not $records) {
84 die "No such table or the select returned no records."
85}
86
87#Right
88#ariston.netcraft.com - - [14/Nov/2001:05:13:39 -0800] "GET / HTTP/1.0" 200 502 "-" "Mozilla/4.08 [en] (Win98; I)"
89#ariston.netcraft.com - - [14/Nov/2001:05:13:39 -0800] "GET / HTTP/1.0" 200 502 "-" "Mozilla/4.08 [en] (Win98; I)"
90
91#Bad
92#ariston.netcraft.com - - [2001-11-14 05:13:39 -0800] "GET / HTTP/1.1" 200 502 "-" "Mozilla/4.08 [en] (Win98; I)"
93#ariston.netcraft.com - - [2001-11-14 05:13:39 -0800] "GET / HTTP/1.1" 200 502 "-" "Mozilla/4.08 [en] (Win98; I)"
94
95
96#
97# Pull out the data row by row and format it
98#
99while (@data = $records->fetchrow_array) {
100 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($data[3]);
101 $year=$year+1900;
102
103 # Create format for leading-zero formatting
104 if ($mday < 10) { $mday = "0$mday"; }
105 if ($mon < 10) { $mon = "0$mon"; }
106 if ($hour < 10) { $hour = "0$hour"; }
107 if ($min < 10) { $min = "0$min"; }
108 if ($sec < 10) { $sec = "0$sec"; }
109
110 # Convert numeric month to string month
111 for ($mon) {
112 if (/00/) { $mon = "Jan";}
113 elsif (/01/) { $mon = "Feb";}
114 elsif (/02/) { $mon = "Mar";}
115 elsif (/03/) { $mon = "Apr";}
116 elsif (/04/) { $mon = "May";}
117 elsif (/05/) { $mon = "Jun";}
118 elsif (/06/) { $mon = "Jul";}
119 elsif (/07/) { $mon = "Aug";}
120 elsif (/08/) { $mon = "Sep";}
121 elsif (/09/) { $mon = "Oct";}
122 elsif (/10/) { $mon = "Nov";}
123 elsif (/11/) { $mon = "Dec";}
124 }
125
126 # Create the output
127 print "$data[0] $data[1] - [$mday/$mon/$year:$hour:$min:$sec ";
128 if ($isdst) {
129 print "$dt_tz\] ";
130 } else {
131 print "$st_tz\] ";
132 }
133 print "\"$data[8] $data[2] $data[9]\" $data[4] $data[5] \"$data[6]\" \"$data[7]\"\n";
134}
135
136#
137# Done
138#
139$records->finish;