#!/bin/sh
#################################################################
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
#################################################################
#
# Requirements
# - web server with cband module enabled
# - installed wget 
#
#################################################################
#
# Configuration section
#
# URL to the script to check eaccelerator status
URL="http://localhost/cband-status?refresh=15&unit=K";
#
WGET=`which wget`;
WGET_FLAGS="-Yoff"; # refer to wget manual, you may set extra parameters like disable proxy
#
#
#################################################################
#
# Changelog
#
# Revision 0.1 Tue 09 May 2009 02:16:02 PM CET
# - initial release, 
# 
#################################################################




#################################################################
#################################################################
# Settigs required for autoconf
#%# family=manual
#%# capabilities=autoconf



if [ "$1" = "autoconf" ]; then
	echo no
	exit 0
fi

if [ "$1" = "config" ]; then

        echo 'graph_title CBandwith Usage '
        echo 'graph_args -l 0'
        echo 'graph_category apache'
        echo 'graph_info This graph shows per virtual host traffic from the cband module.'
	echo 'graph_vlabel Kbytes per ${graph_period}'

	wget -q $WGET_FLAGS "$URL" -O - | grep -A 3 http |grep "^<td.*http" |sed -e 's^.*http://\(.*\)".*^\1^' | while read site; do
		metricname=$(echo $site | sed -e 's/\.//g')
		#echo $metricname $site
		echo $metricname'.label '$site
		echo $metricname'.info Traffic generated on '$site'.'
		echo $metricname'.min 0'
		echo $metricname'.type DERIVE'
	done;
	exit 0
fi


#################################################################
# run the sript
    if [ -x $WGET ]; then
#            quiet                 output to stdout
	SITES=$(wget -q $WGET_FLAGS "$URL" -O - | grep -A 3 http |grep "^<td.*http" |sed -e 's^.*http://\(.*\)".*^\1^')
	$WGET -q $WGET_FLAGS "$URL" -O - | grep -A 3 http |grep "^<td.*\(color\|http\)" | while read v; do
		echo -n $v | sed -e 's^.*http://\(.*\)".*^\1^' | sed -e 's/\.//g'
		echo -n '.value '
		read v;
		echo $v | sed -e 's^.*\/\(.*\)KB.*^\1^'
	done;
	exit 0
    fi

exit 0

# end of file
