#!/bin/bash
# *****************************************************************************
# file: httplogs.sh
# date: 2007-12-17 15:50
# author: Marko Schulz - <info@tuxnet24.de>
# description: Dispay a list of predefined column of http logfiles.
# *****************************************************************************
# This function display the program usaage.
function f_alert () {
local error=$1
local header=$( head -n7 $0 | grep -v '/bin/bash' )
echo -e "$header\n"
[ -n "$error" ] && echo -e "\aERROR: ${error}\n"
echo -e "Usage: $( basename $0 ) -f /path/to/htttp/logfile [ -c \"<column1> <column2> <columnX>\" ] [ -h ]\n"
echo -e "\t-f http logfile (e.g. /var/log/apache/access.log)"
echo -e "\t-c column list, 0 is the first column (e.g. \"1 2 5\")"
echo -e "\t-h display this screen\n\n"
echo -e " meaning of columns\n"
echo -e "\t 0 => Date"
echo -e "\t 1 => Server Alias"
echo -e "\t 2 => Host"
echo -e "\t 3 => User"
echo -e "\t 4 => Method"
echo -e "\t 5 => Request URL"
echo -e "\t 6 => Protocol"
echo -e "\t 7 => MimeType"
echo -e "\t 8 => ResponseCode"
echo -e "\t 9 => Size (in bytes)"
echo -e "\t10 => ProcessTime"
echo -e "\t11 => Useragent"
echo -e "\t12 => Referer"
echo -e "\t13 => Cookie\n"
exit 1
}
# *****************************************************************************
# This function read a text or compressed text file.
function f_cat () {
case "$1" in
*gz|*Z) zcat $1 ;;
*) cat $1 ;;
esac
}
# *****************************************************************************
# get the commad line arguments...
while getopts f:c:h Optionen 2>/dev/null; do
case $Optionen in
f) pFile=$OPTARG ;;
c) pLine=$OPTARG ;;
h) f_alert ;;
*) f_alert "invalid argument" ;;
esac
done
# check if logfile is defined...
[ ! -f "$pFile" ] && f_alert "No file defined"
# replace spaces with dashes...
pLine=$( echo $pLine | sed 's/[[:space:]]/-/g' )
# read & dispay http logfile -> Perl rules ;-)
f_cat $pFile | perl -ne '
BEGIN {
$string="'$pLine'";
}
# split line in array elements...
my @line=split( /"\s+"/, $_ );
# remove new line and " character...
map { $_ =~ s/("|\n)//g } @line;
# split request in array elements...
my @req=split( /\s+/, $line[4] );
# extract the request entry ( like: GET /index.php HTTP/1.1 )
# and replace them with the single entrys from array "@req".
splice( @line, 4, 1, $req[0] );
splice( @line, 5, 0, $req[1] );
splice( @line, 6, 0, $req[2] );
if ( $string ne "" ) {
my @args=split( /-/, $string );
foreach my $i ( @args ) { print $line[$i]." "; }
print "\n";
} else {
foreach my $i ( @line ) { print $i." "; }
print "\n";
}
'
exit 0
# *****************************************************************************
# end of this script...