#!/bin/bash
# (C) 2005 Mark Boddington, http://www.badpenguin.co.uk
# Licensed Under the GNU GPL Version 2.

# Apache Mod_proxy cacheing stats
# This script accepts an apache logfile in the format 
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Cache}o\""
# and generates statistics for the hits/misses/uncacheables
# for that log.

getstats()
{
	cat $1 | awk '
	BEGIN {
		hit=0
		miss=0
		none=0
	} {
		if ( $11 ~ "\"HIT" ) {
			hit++;
			hstat[$9]++;
		} else if ( $11 ~ "\"MISS" ) {
			miss++;
			mstat[$9]++
		} else {
			none++;
			nstat[$9]++;
		}
	} END {
		pcent = hit / ((hit + miss) / 100 )
		tpcent = hit / ((hit + miss + none ) / 100 )
		print "Total requests served", ( hit + miss + none )
		print "HIT:", hit, "MISS:", miss, "Uncacheable:", none
		for (item in hstat)
		print "Hits with return code", item, hstat[item];
		for (item in mstat)
		print "Misses with return code", item, mstat[item];
		for (item in nstat)
		print "Uncacheable with return code", item, nstat[item];
		print "Percent hits (excluding uncacheable)", pcent "%"
		print "Percent hits (including uncacheable)", tpcent "%"
	}'
}

dohelp()
{
	cat <<-EOF

	$0 [ logfile [ logfile [...]]]

	This script accepts a list of one or more logfiles and it will give
	you the cacheing stats for each of those files. The Logfiles need
	to be created using the apache format:
	
	LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Cache}o\""
	
	EOF
	exit
}

if [ -z "$1" ]
then
	dohelp
else
	while [ -n "$1" ]
	do
		if [ -f "$1" ]
		then
			echo -e "\nCacheing stats for $1"
			getstats $1
		else
			dohelp
		fi
	done
fi



