A A
RSS

Apache Log Parsing Bash

Mon, Feb 1, 2010

Apache, Log Analysis, Scripts

Apache log has various formats, out of which common and combined are mostly used ones. Bash script that is provided below parses apache common/combined logs and displays information out of its stats. In the resulting output, the “User Agent” part can be extended further to display many other information like Browsers, OS and so on.

Any help required to get more details of it can Contact Us.

Script:

#!/bin/bash
Empty_File=`du $2 | awk ‘{print $1}’`
if [ $Empty_File -eq 0 ]; then
echo “Specified Log File is Empty”
else
if [ -z $1 ] && [ -z $2 ]; then
echo $”Usage: $0 logformat[combined|common] logfile”
echo “Example: $0 common /var/log/httpd/access_log”
else
myifs=$IFS
IFS=$(echo -en “\n”)
logfile=`cat $2`
for i in $logfile
do
if [ $1 == common ]; then
echo $i | awk ‘{print “Remote Host=” $1 “  ” “Client Identity=” $2 “  ” “Client Identity=” $2 “  ” “Remote User=” $3 “  ” “Request Time=” $4″ “$5  “  ” “Method Used=” $6 “  ” “Requested Resource=” $7 “  ” “Protocol Used=” $8″  ” “Ressponse Code=” $9 “  ” “Returned Object Size=” $10}’
else
if [ $1 == combined ]; then
echo $i | awk ‘{print “Remote Host=” $1 “  ” “Client Identity=” $2 “  ” “Client Identity=” $2 “  ” “Remote User=” $3 “  ” “Request Time=” $4″ “$5  “  ” “Method Used=” $6 “  ” “Requested Resource=” $7 “  ” “Protocol Used=” $8″  ” “Ressponse Code=” $9 “  ” “Returned Object Size=” $10 “  ” “Referer=” $11 “  ” “User Agent=”$12″ “$13″ “$14″ “$15″ “$16″ “$17″ “$18″ “$19″ “$20″ “$21}’
else
echo “Unable To Determine Log Format”
fi
fi
done
IFS=$myifs
fi
fi

Running the script:

sh script_name.sh <common/combined> /var/log/httpd/access_log

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)

Tags: , ,

Leave a Reply

Sponsored Links

Categories

Blogroll

Archives

Ad Ad