#! /usr/bin/perl =comment Copyright (c) 2010, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =cut =comment User contributed content on the Perforce Public Depot is not supported by Perforce, although it may be supported by its author. This applies to all contributions even those submitted by Perforce employees. =cut ################################################################################ =comment This script converts the Perforce server's P4AUDIT log to SQL for easier access. The data types used in the DDL are specific to SQLite. SQLite is used because the databases it creates are single-file and easier to archive than other, more complicated systems. Archiving will be of interest due to the amount of data created. $Id: //guest/jason_gibson/misc/auditlog2sql.pl#4 $ $DateTime: 2010/03/31 16:15:08 $ $Author: jason_gibson $ $Change: 7605 $ =cut use warnings FATAL => qw( all ); use strict; use Time::Local; my $db_name = "p4audit"; my $debug = 0; # You either specify both the input/output files, or use the defaults: # e.g.: p4-auditlogtosql.pl audit.log p4audit.db # e.g.: cat audit.log | p4-auditlogtosql.pl # my ($input_file, $db_file) = @ARGV == 2 ? @ARGV : ( "-", "$db_name.db" ); open IN, "<$input_file" or die "Couldn't open '$input_file': $@\n"; open OUT, "| sqlite3 --init $db_file" or die "Couldn't open 'sqlite3': $@\n"; # This makes it go a little faster at the expense of some safety. #print OUT "PRAGMA synchronous = OFF;\n"; my $ddl = "CREATE TABLE $db_name ( date INT, user TEXT, client TEXT, " . "proxyIP TEXT, clientIP TEXT, cmd TEXT, cmdSrc TEXT, file TEXT, " . "rev INT );\n"; print OUT $ddl if( `sqlite3 $db_file '.schema $db_name' 2>&1` !~ /^CREATE TABLE/ ); print OUT "BEGIN TRANSACTION;\n"; # format: date time user@client clientIP command file#rev # e.g. 2010/03/26 11:57:26 user@ws 127.0.0.1 sync //depot/%23$%25^&%2A\\#1 # # format: date time user@client proxyIP/clientIP command-src file#rev # e.g. 2010/03/26 16:10:09 u@c 127.0.0.1/127.0.0.1 print-proxy //depot/abc#1 # Note that the file name on-disk is: #$%^&*\\ # # Note that the IP can be the string 'unknown'. # # Known audit-log commands: branch, annotate, diff, deliver, merge, print, # revert, resolve, sync, grep. "deliver" is a server->proxy transfer. my $DATE = qr !(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2})!; my $ID = qr !(\S+)\@(\S+)!; my $IP = qr !\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|unknown!; my $IPS = qr !($IP|$IP/$IP)!; my $HOW = qr !(\w+|\w+-proxy)!; my $FILE = qr !(//.+)!; my $REV = qr !(\d+)!; my $AUDIT = qr !^$DATE $ID $IPS $HOW $FILE#$REV$!; my $SPLIT_IP = qr !^($IP)[/]*($IP){0,1}$!; my $SPLIT_CMD = qr !^(\w+)(?:(?:-)*(\w+))*$!; my $line_no = 1; while( my $line = ) { # Skip blank lines. They shouldn't appear in normal auditlog files. # next if $line =~ /^$/; chomp $line; my ( $year, $mon, $mday, $hour, $min, $sec, $user, $client, $client_ip, $cmd, $file, $rev ) = $line =~ $AUDIT; die "Couldn't parse line $line_no of $input_file on $db_file: $line\n" if( !defined $rev ); # convert to unix epoch for use with SQLite's date functions. my $epoch = timelocal( $sec, $min, $hour, $mday, $mon - 1, $year ); my $proxy_ip = "NULL"; my $cmd_src = "NULL"; # double-up single-quotes $user =~ s!'!''!g; $client =~ s!'!''!g; $file =~ s!'!''!g; $client_ip =~ $SPLIT_IP; if( defined $2 ) { $proxy_ip = "'$1'"; $client_ip = "'$2'"; } else { $client_ip = "'$client_ip'"; } $cmd =~ $SPLIT_CMD; if( defined $2 ) { $cmd = "'$1'"; $cmd_src = "'$2'"; } else { $cmd = "'$cmd'"; } my $sql = "INSERT INTO $db_name VALUES( '$epoch', '$user', '$client', " . "$proxy_ip, $client_ip, $cmd, $cmd_src, '$file', '$rev' );\n"; print OUT $sql; $line_no++; } print OUT "COMMIT;\n";