/usr/bin/mysql_import_combined_log2 is in libapache2-mod-log-sql 1.100-16.3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | #!/usr/bin/perl -w
# $Id: mysql_import_combined_log.pl,v 1.4 2004/02/21 18:09:50 urkle Exp $
# Written by Aaron Jenson.
# Original source: http://www.visualprose.com/software.php
# Updated to work under Perl 5.6.1 by Edward Rudd
use strict;
use Getopt::Long qw(:config bundling);
use DBI;
use Date::Parse;
my %options = ();
my $i = 0;
my $sql = '';
my $valuesSql = '';
my $line = '';
my $dbh = 0;
my $sth = 0;
my @parts = ();
my $part;
my $TIMESTAMP = 3;
my $REQUEST_LINE = 4;
my @cols = (
'remote_host', ## 0
'remote_logname', ## 1
'remote_user', ## 2
'request_time', ## 3.string
'time_stamp', ## 3.posix
'request_line', ## 5
'request_method', ## 6
'request_uri', ## 7
'request_args', ## 8
'request_protocol', ## 9
'status', ## 10
'bytes_sent', ## 11
'referer', ## 12
'agent' ## 13
);
my $col = '';
GetOptions (\%options,
"version" => sub { VERSION_MESSAGE(); exit 0; },
"help|?" => sub { HELP_MESSAGE(); exit 0; },
"host|h=s",
"database|d=s",
"table|t=s",
"username|u=s",
"password|p=s",
"logfile|f=s");
$options{host} ||= 'localhost';
$options{database} ||= '';
$options{username} ||= '';
$options{password} ||= '';
$options{logfile} ||= '';
if( ! $options{database} )
{
HELP_MESSAGE();
print "Must supply a database to connect to.\n";
exit 1;
}
if( ! $options{table} )
{
HELP_MESSAGE();
print "Must supply table name.\n";
exit 1;
}
if( $options{logfile} )
{
if( ! -e $options{logfile} )
{
print "File '$options{logfile}' doesn't exist.\n";
exit 1;
}
open(STDIN, "<$options{logfile}") || die "Can't open $options{logfile} for reading.";
}
$dbh = Connect();
if (! $dbh) {
exit 1;
}
$sql = "INSERT INTO $options{table} (";
foreach $col (@cols)
{
$sql .= "$col," if( $col );
}
chop($sql);
$sql .= ') VALUES (';
my ($linecount,$insertcount) = (0,0);
while($line = <STDIN>)
{
$linecount++;
@parts = SplitLogLine( $line );
next if( $parts[$TIMESTAMP+1] == 0 );
$valuesSql = '';
for( $i = 0; $i < @cols; ++$i )
{
$parts[$i] =~ s/\\/\\\\/g;
$parts[$i] =~ s/'/\\'/g;
$valuesSql .= "'$parts[$i]'," if( $cols[$i] );
}
chop($valuesSql);
$sth = $dbh->prepare("$sql$valuesSql)");
if( ! $sth->execute() )
{
print "Unable to perform specified query.\n$sql$valuesSql\n" . $sth->errstr() . "\n";
} else {
$insertcount++;
}
$sth->finish();
}
print "Parsed $linecount Log lines\n";
print "Inserted $insertcount records\n";
print "to table '$options{table}' in database '$options{database}' on '$options{host}'\n";
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Connects to a MySQL database and returns the connection.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
sub Connect
{
my $dsn = "DBI:mysql:$options{database};hostname=$options{host}";
return DBI->connect( $dsn, $options{username}, $options{password} );
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Splits up a log line into its parts.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
sub SplitLogLine
{
my $line = shift;
my $i = 0;
my $inQuote = 0;
my $char = '';
my $part = '';
my @parts = ();
my $count = 0;
chomp($line);
for( $i = 0; $i < length($line); ++$i )
{
$char = substr($line, $i, 1);
if( $char eq ' ' && ! $inQuote )
{
## print "Found part $part.\n";
if( $count == $TIMESTAMP )
{
push(@parts, "[".$part."]");
$part = str2time($part);
}
push(@parts, $part);
if( $count == $REQUEST_LINE )
{
my @request = split(/[ ?]/, $part);
push(@parts, $request[0]);
push(@parts, $request[1]);
if( $request[3] )
{
push(@parts, $request[2]);
push(@parts, $request[3]);
}
else
{
push(@parts, '');
push(@parts, $request[2]);
}
$count += 5;
}
else
{
++$count;
}
$part = '';
}
elsif( $char eq '"' || $char eq '[' || $char eq ']' )
{
$inQuote = !$inQuote;
}
else
{
$part .= $char;
}
}
push(@parts,$part) if $part;
return @parts;
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Prints the usage/help message for this program.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
sub HELP_MESSAGE
{
print<<EOF;
Imports an Apache combined log into a MySQL database.
Usage: mysql_import_combined_log.pl -d <database name> -t <table name> [-h <hostname>] [-u <username>] [-p <password>] [-f <filename]
--host|-h <host name> The host to connect to. Default is localhost.
--database|-d <database name> The database to use. Required.
--username|-u <username> The user to connect as.
--password|-p <password> The user's password.
--table|-t <table name> The name of the table in which to insert data.
--logfile|-f <file name> The file to read from. If not given, data is read from stdin.
--help|-? Print out this help message.
--version Print out the version of this software.
EOF
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Prints the version information for this program
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
sub VERSION_MESSAGE
{
print "mysql_import_combined_log.pl version 1.2\n";
print "Version 1.0 Written by Aaron Jenson.\n";
print "Update to work with perl 5.6.1 by Edward Rudd\n";
}
1;
|