diff options
Diffstat (limited to 'contrib/mysql_import_combined_log.pl')
| -rw-r--r-- | contrib/mysql_import_combined_log.pl | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/contrib/mysql_import_combined_log.pl b/contrib/mysql_import_combined_log.pl new file mode 100644 index 0000000..bffec20 --- /dev/null +++ b/contrib/mysql_import_combined_log.pl | |||
| @@ -0,0 +1,224 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # $Id: mysql_import_combined_log.pl,v 1.4 2004/02/21 18:09:50 urkle Exp $ | ||
| 3 | # Written by Aaron Jenson. | ||
| 4 | # Original source: http://www.visualprose.com/software.php | ||
| 5 | # Updated to work under Perl 5.6.1 by Edward Rudd | ||
| 6 | use strict; | ||
| 7 | use Getopt::Long qw(:config bundling); | ||
| 8 | use DBI; | ||
| 9 | use Date::Parse; | ||
| 10 | |||
| 11 | my %options = (); | ||
| 12 | my $i = 0; | ||
| 13 | my $sql = ''; | ||
| 14 | my $valuesSql = ''; | ||
| 15 | my $line = ''; | ||
| 16 | my $dbh = 0; | ||
| 17 | my $sth = 0; | ||
| 18 | my @parts = (); | ||
| 19 | my $part; | ||
| 20 | my $TIMESTAMP = 3; | ||
| 21 | my $REQUEST_LINE = 4; | ||
| 22 | my @cols = ( | ||
| 23 | 'remote_host', ## 0 | ||
| 24 | 'remote_logname', ## 1 | ||
| 25 | 'remote_user', ## 2 | ||
| 26 | 'request_time', ## 3.string | ||
| 27 | 'time_stamp', ## 3.posix | ||
| 28 | 'request_line', ## 5 | ||
| 29 | 'request_method', ## 6 | ||
| 30 | 'request_uri', ## 7 | ||
| 31 | 'request_args', ## 8 | ||
| 32 | 'request_protocol', ## 9 | ||
| 33 | 'status', ## 10 | ||
| 34 | 'bytes_sent', ## 11 | ||
| 35 | 'referer', ## 12 | ||
| 36 | 'agent' ## 13 | ||
| 37 | ); | ||
| 38 | my $col = ''; | ||
| 39 | |||
| 40 | GetOptions (\%options, | ||
| 41 | "version" => sub { VERSION_MESSAGE(); exit 0; }, | ||
| 42 | "help|?" => sub { HELP_MESSAGE(); exit 0; }, | ||
| 43 | "host|h=s", | ||
| 44 | "database|d=s", | ||
| 45 | "table|t=s", | ||
| 46 | "username|u=s", | ||
| 47 | "password|p=s", | ||
| 48 | "logfile|f=s"); | ||
| 49 | |||
| 50 | $options{host} ||= 'localhost'; | ||
| 51 | $options{database} ||= ''; | ||
| 52 | $options{username} ||= ''; | ||
| 53 | $options{password} ||= ''; | ||
| 54 | $options{logfile} ||= ''; | ||
| 55 | |||
| 56 | if( ! $options{database} ) | ||
| 57 | { | ||
| 58 | HELP_MESSAGE(); | ||
| 59 | print "Must supply a database to connect to.\n"; | ||
| 60 | exit 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | if( ! $options{table} ) | ||
| 64 | { | ||
| 65 | HELP_MESSAGE(); | ||
| 66 | print "Must supply table name.\n"; | ||
| 67 | exit 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | if( $options{logfile} ) | ||
| 71 | { | ||
| 72 | if( ! -e $options{logfile} ) | ||
| 73 | { | ||
| 74 | print "File '$options{logfile}' doesn't exist.\n"; | ||
| 75 | exit 1; | ||
| 76 | } | ||
| 77 | open(STDIN, "<$options{logfile}") || die "Can't open $options{logfile} for reading."; | ||
| 78 | } | ||
| 79 | |||
| 80 | $dbh = Connect(); | ||
| 81 | if (! $dbh) { | ||
| 82 | exit 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | $sql = "INSERT INTO $options{table} ("; | ||
| 86 | foreach $col (@cols) | ||
| 87 | { | ||
| 88 | $sql .= "$col," if( $col ); | ||
| 89 | } | ||
| 90 | chop($sql); | ||
| 91 | $sql .= ') VALUES ('; | ||
| 92 | my ($linecount,$insertcount) = (0,0); | ||
| 93 | while($line = <STDIN>) | ||
| 94 | { | ||
| 95 | $linecount++; | ||
| 96 | @parts = SplitLogLine( $line ); | ||
| 97 | next if( $parts[$TIMESTAMP+1] == 0 ); | ||
| 98 | $valuesSql = ''; | ||
| 99 | for( $i = 0; $i < @cols; ++$i ) | ||
| 100 | { | ||
| 101 | $parts[$i] =~ s/\\/\\\\/g; | ||
| 102 | $parts[$i] =~ s/'/\\'/g; | ||
| 103 | $valuesSql .= "'$parts[$i]'," if( $cols[$i] ); | ||
| 104 | } | ||
| 105 | chop($valuesSql); | ||
| 106 | |||
| 107 | $sth = $dbh->prepare("$sql$valuesSql)"); | ||
| 108 | if( ! $sth->execute() ) | ||
| 109 | { | ||
| 110 | print "Unable to perform specified query.\n$sql$valuesSql\n" . $sth->errstr() . "\n"; | ||
| 111 | } else { | ||
| 112 | $insertcount++; | ||
| 113 | } | ||
| 114 | $sth->finish(); | ||
| 115 | } | ||
| 116 | print "Parsed $linecount Log lines\n"; | ||
| 117 | print "Inserted $insertcount records\n"; | ||
| 118 | print "to table '$options{table}' in database '$options{database}' on '$options{host}'\n"; | ||
| 119 | |||
| 120 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 121 | # Connects to a MySQL database and returns the connection. | ||
| 122 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 123 | sub Connect | ||
| 124 | { | ||
| 125 | my $dsn = "DBI:mysql:$options{database};hostname=$options{host}"; | ||
| 126 | return DBI->connect( $dsn, $options{username}, $options{password} ); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 131 | # Splits up a log line into its parts. | ||
| 132 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 133 | sub SplitLogLine | ||
| 134 | { | ||
| 135 | my $line = shift; | ||
| 136 | my $i = 0; | ||
| 137 | my $inQuote = 0; | ||
| 138 | my $char = ''; | ||
| 139 | my $part = ''; | ||
| 140 | my @parts = (); | ||
| 141 | my $count = 0; | ||
| 142 | chomp($line); | ||
| 143 | for( $i = 0; $i < length($line); ++$i ) | ||
| 144 | { | ||
| 145 | $char = substr($line, $i, 1); | ||
| 146 | if( $char eq ' ' && ! $inQuote ) | ||
| 147 | { | ||
| 148 | ## print "Found part $part.\n"; | ||
| 149 | if( $count == $TIMESTAMP ) | ||
| 150 | { | ||
| 151 | push(@parts, "[".$part."]"); | ||
| 152 | $part = str2time($part); | ||
| 153 | } | ||
| 154 | push(@parts, $part); | ||
| 155 | if( $count == $REQUEST_LINE ) | ||
| 156 | { | ||
| 157 | my @request = split(/[ ?]/, $part); | ||
| 158 | push(@parts, $request[0]); | ||
| 159 | push(@parts, $request[1]); | ||
| 160 | if( $request[3] ) | ||
| 161 | { | ||
| 162 | push(@parts, $request[2]); | ||
| 163 | push(@parts, $request[3]); | ||
| 164 | } | ||
| 165 | else | ||
| 166 | { | ||
| 167 | push(@parts, ''); | ||
| 168 | push(@parts, $request[2]); | ||
| 169 | } | ||
| 170 | $count += 5; | ||
| 171 | } | ||
| 172 | else | ||
| 173 | { | ||
| 174 | ++$count; | ||
| 175 | } | ||
| 176 | $part = ''; | ||
| 177 | } | ||
| 178 | elsif( $char eq '"' || $char eq '[' || $char eq ']' ) | ||
| 179 | { | ||
| 180 | $inQuote = !$inQuote; | ||
| 181 | } | ||
| 182 | else | ||
| 183 | { | ||
| 184 | $part .= $char; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | push(@parts,$part) if $part; | ||
| 188 | |||
| 189 | return @parts; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 194 | # Prints the usage/help message for this program. | ||
| 195 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 196 | sub HELP_MESSAGE | ||
| 197 | { | ||
| 198 | print<<EOF; | ||
| 199 | Imports an Apache combined log into a MySQL database. | ||
| 200 | Usage: mysql_import_combined_log.pl -d <database name> -t <table name> [-h <hostname>] [-u <username>] [-p <password>] [-f <filename] | ||
| 201 | --host|-h <host name> The host to connect to. Default is localhost. | ||
| 202 | --database|-d <database name> The database to use. Required. | ||
| 203 | --username|-u <username> The user to connect as. | ||
| 204 | --password|-p <password> The user's password. | ||
| 205 | --table|-t <table name> The name of the table in which to insert data. | ||
| 206 | --logfile|-f <file name> The file to read from. If not given, data is read from stdin. | ||
| 207 | --help|-? Print out this help message. | ||
| 208 | --version Print out the version of this software. | ||
| 209 | EOF | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 215 | # Prints the version information for this program | ||
| 216 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||
| 217 | sub VERSION_MESSAGE | ||
| 218 | { | ||
| 219 | print "mysql_import_combined_log.pl version 1.2\n"; | ||
| 220 | print "Version 1.0 Written by Aaron Jenson.\n"; | ||
| 221 | print "Update to work with perl 5.6.1 by Edward Rudd\n"; | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; | ||
