3 #=======================================================================
5 # File ID: 7501e37a-f740-11dd-8b2b-000475e441b9
7 # Convert access log files generated by the Apache HTTP Server to TAB
8 # separated data suitable for import into a database.
10 # Character set: UTF-8
11 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
12 # License: GNU General Public License version 2 or later, see end of
13 # file for legal stuff.
14 #=======================================================================
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.2.0';
37 Getopt
::Long
::Configure
('bundling');
40 'help|h' => \
$Opt{'help'},
41 'initdb' => \
$Opt{'initdb'},
42 'quiet|q+' => \
$Opt{'quiet'},
43 'server|s=s' => \
$Opt{'server'},
44 'verbose|v+' => \
$Opt{'verbose'},
45 'version' => \
$Opt{'version'},
47 ) || die("$progname: Option error. Use -h for help.\n");
49 $Opt{'verbose'} -= $Opt{'quiet'};
50 $Opt{'help'} && usage
(0);
51 if ($Opt{'version'}) {
64 CREATE TABLE IF NOT EXISTS access_log (
78 CREATE TABLE IF NOT EXISTS domains (
83 CREATE OR REPLACE FUNCTION ip2host(inet) RETURNS text
85 SELECT hostname FROM domains
92 length($Opt{'server'}) || die("Need to specify the --server (-s) option\n");
96 (\d
+\
.\d
+\
.\d
+\
.\d
+) # 1: IP adress
98 (\S
+) # 2: identd info (Not used)
100 (.+?
) # 3: User name if http auth
102 \
[(.*?
)\
] # 4: Timestamp
104 "(.*?)" # 5: Method + Page + Protocol
106 (\d
+) # 6: Status code
108 (\S
+) # 7: Bytes sent
110 "(.*?)" # 8: Referrer
121 postgresql_copy_safe
($3),
123 postgresql_copy_safe
($5),
126 postgresql_copy_safe
($8),
127 postgresql_copy_safe
($9),
132 chomp(my $Line = $_);
133 warn("Line $.: Unknown line: '$Line'\n");
141 sub postgresql_copy_safe
{
144 $Str =~ s/\\/\\\\/gs;
153 # Print program version {{{
154 print("$progname $VERSION\n");
160 # Send the help message to stdout {{{
163 if ($Opt{'verbose'}) {
169 Convert access log files generated by the Apache HTTP Server to TAB
170 separated data suitable for import into a database.
172 Usage: $progname -s servername [options] [file [files [...]]]
177 Output SQL commands to create the initialise the database.
181 Be more quiet. Can be repeated to increase silence.
183 Server identification. Required.
185 Increase level of verbosity. Can be repeated.
187 Print version information.
192 $progname --initdb | psql DBNAME
193 cat *ACCESS_LOG_FILES* | \\
194 $progname -s SERVERNAME | \\
195 psql DBNAME -c "copy access_log from stdin"
199 $progname --initdb | sqlite3 DBNAME
200 cat *ACCESS_LOG_FILES* | \\
201 $progname -s SERVERNAME | \\
202 sqlite3 DBNAME '.separator "\\t"' '.import /dev/stdin access_log'
210 # Print a status message to stderr based on verbosity level {{{
211 my ($verbose_level, $Txt) = @_;
213 if ($Opt{'verbose'} >= $verbose_level) {
214 print(STDERR
"$progname: $Txt\n");
222 # This program is free software; you can redistribute it and/or modify
223 # it under the terms of the GNU General Public License as published by
224 # the Free Software Foundation; either version 2 of the License, or (at
225 # your option) any later version.
227 # This program is distributed in the hope that it will be useful, but
228 # WITHOUT ANY WARRANTY; without even the implied warranty of
229 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
230 # See the GNU General Public License for more details.
232 # You should have received a copy of the GNU General Public License
233 # along with this program.
234 # If not, see L<http://www.gnu.org/licenses/>.
236 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :