#! /usr/bin/env perl
=comment
Example program to manually parse a 2012.1 P4D structured server log file.
You can do the same thing client-side via 'p4 logparse'. See p4logtodb.pl
for a program to take 'p4 logparse' output and stick it into a SQL db.
Requires P4-Perl.
$Author: jason_gibson $
=cut
=comment
Copyright (c) 2012, 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
use strict;
use warnings FATAL => qw( all );;
use feature ':5.10';
use Fcntl qw / :flock SEEK_END /;
use autodie;
#use Data::Dumper;
use Carp;
use URI::Escape;
# These aren't default libraries, so assume you've already unpacked
# and run Make on them in relative directories to this script.
use P4; # Perforce Perl API (P4-PERL)
use lib 'URI-1.59/blib/lib';
# 'common-sense-3.4/blib/lib/', # req. for the next lib.
# 'Linux-Inotify2-1.22/blib/lib',
# 'Linux-Inotify2-1.22/blib/arch',;
#use Linux::Inotify2;
sub p4_emsg { join '', map { $_->GetText() } $_[ 0 ]->Errors, $_[ 0 ]->Warnings}
sub p4_errs { $_[ 0 ]->WarningCount() + $_[ 0 ]->ErrorCount() }
sub check { croak p4_emsg $_[ 0 ] if p4_errs $_[ 0 ] }
my $c = new P4;
# Identify ourself in the Perforce server's log file via RCS keywords.
my $rcs_re = qr /^\$\S+: (.*) \$$/;
'$File: //guest/jason_gibson/misc/p4logparse.pl $' =~ $rcs_re;
$c->SetProg( $1 );
'$Change: 8074 $' =~ $rcs_re;
$c->SetVersion( "\@$1" );
$c->Connect();
check $c;
# Build an array of arrays holding the field names for each record type.
# The position in the array is also the record type number.
my @log_types;
$log_types[ $_->{ f_recordType } ][ $_->{ f_field } ] = $_->{ f_name }
foreach $c->Run( 'logschema', '-a' );
check $c;
$c->Disconnect();
# Names for the record types.
my %rtypes = map { state $n = 0; ( $n++, $_ ) }
qw / command_start command_compute command_end error_all
error_failed error_fatal audit track_rusage track_rpc
track_db user trigger event purge /;
# The path/name of the log file to tail. A quick way to play with structured
# logging is to run this on your test server:
#
# p4 -u super configure set serverlog.file.1=all.csv
#
# Note that all.csv is SUPER VERBOSE. Don't say I didn't try to warn you.
my $lf = @ARGV == 0 ? 'all.csv' : $ARGV[ 0 ];
# Lock, (to coordinate with writing p4d processes) then seek to EOF
# before reading to make sure we're on a line boundary and that we
# don't read old log entries.
open my $lfh, "<$lf";
flock $lfh, LOCK_EX;
# Uncomment the seek if you want to immediately jump to EOF to skip old records.
#seek $lfh, 0, SEEK_END;
flock $lfh, LOCK_UN;
#my $inotify = new Linux::Inotify2 or die "no inotify: $!";
my $code = sub {
while ( <$lfh> ) {
chomp;
my %d;
# The log is basically CSV-formatted. Commas within a field are
# percent-encoded, so this split is safe. The hash-slice then
# matches-up the key names with the values.
@d{ @{ $log_types[ substr( $_, 0, 1 ) ] } } = split ',', $_;
$d{ args } = '' if ! defined $d{ args };
# Unescape the data. Note that multiple command arguments are separated
# by colons in the log - we just change that to a space here for printing.
map { $d{ $_ } =~ s/:/ /g if $_ eq 'args';
$d{ $_ } = uri_unescape $d{ $_ }
} keys %d;
my $rtype = $rtypes{ $d{ eventtype } };
my $msg = "msg: $rtype ";
# Create different messages based off of what's in the record.
given ( $rtype ) {
when ( /command_/ ) { $msg .= "$d{ user } $d{ func } $d{ args }" }
when ( /error_/ ) { $msg .= "$d{ text }" }
default { $msg .= "\n" }
}
say $msg;
}
};
# Only listen to events where we expect data was written.
#my $watch = $inotify->watch( $lf, IN_CLOSE_WRITE, $code )
# or die "watch failed: $!";
#
#1 while $inotify->poll;
$code->();