Skip to content

Instantly share code, notes, and snippets.

@caseymhunt
Created January 14, 2013 21:29
Show Gist options
  • Select an option

  • Save caseymhunt/4533698 to your computer and use it in GitHub Desktop.

Select an option

Save caseymhunt/4533698 to your computer and use it in GitHub Desktop.
Lang: PERL Descrip: Log parser- looks for a line in apache log that contains 'searchbox.php' then grabs the arguments passed in the URL with the names, query, engine and subengine. Dumps the results into a file with -parsed.log appended to source filename.
#!/usr/bin/perl -w
use URI::Escape; #Use URI decoding module.
$#ARGV >= 0 or die "No log file name supplied.";
#Assign argument (source file) to variable
$filename = $ARGV[0] . "-parsed.log";
#Print start message in terminal.
print "\n *** Processing log file for\: $ARGV[0] ***\n \n";
print "Please wait... \n";
#Create file to write parsed info to.
open (WFILE, ">$filename");
my @arguments= ('engine', 'query', 'subengine');
print WFILE join('*', ('date', 'time', @arguments)) . "\n";
open (FILE, $ARGV[0]);
while (<FILE>) {
chomp;#remove trailing newline
#store line for clarity
my $line = $_;
#test current line for presence of 'searchbox.php'
if ($line =~ m/searchbox.php/) {
#declare empty array to hold the values
my @vals = ();
if ($line =~ /\[([^\:|^\ ]*):(\S*)/) {
#$time = $1 . $2;
push(@vals, ($1, $2));
}
#loop through the above defined arguments
foreach $argument (@arguments) {
#test the line for presence of each argument, and record the value in a group ($2)
if ($line =~ /[^\w]($argument)=([^\&|^\ ]*)/) {
#Remove URL encoding from each value
my $decode = uri_unescape($2);
#Replace all '+' with space
$decode =~ s/\+/ /g;
#push the found value on the end of the values array
push(@vals, $decode);
}
}
#print the values array separated by double colon, terminated by newline
print WFILE join('*', @vals) . "\n";
}
}
print "\n *** Done! **** \n";
close (FILE);
close (WFILE);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment