#!/bin/sh
echo "Content-type: image/png"
echo

if test ! "$1"
then
        echo "$0: We need a hostname!"
        echo "Usage: $0 hostname"
        exit 1
fi

DYNAMIC_HOSTNAME="$1"
export BBHOME=/usr/local/bb
export BBRRDS=$BBHOME/rrd


# $Id: disk-graph.sh,v 1.3 2000/05/05 06:48:19 pearcec Exp $

#set -x

#  need to add disk used 'absolute'

# disk-graph.sh

# this script will build disk PNG's
# using $RRDTOOL graph with RRD's built via
# the BB ext script larrd.sh

if test ! "$BBHOME"
then
        echo "$0: BBHOME is not set"
        exit 1
fi

if test ! -d "$BBHOME"
then
        echo "$0: BBHOME is invalid"
        exit 1
fi

# source the conf
. $BBHOME/etc/bbdef.sh
. $BBHOME/ext/larrd/larrd.conf

# get current date
DATE=`date`
# initialize color counter
c=0

# let's keep the graphs seperate from the RRD's
if [ ! -d $GRAPHS ]
then
	mkdir $GRAPHS	
	echo "$0 did not find $GRAPHS, created." 
fi

# get to work
cd $BBRRDS

# figure out which hosts have disk info
for HOST in `ls ${DYNAMIC_HOSTNAME}.disk* | $SED 's/\.disk.*.rrd//' | uniq ` 
do
	# variable reset for each host
	DEFS=""
	LINES=""
	c=0
	p=0
	
	# get list of parition RRD's and build RRD DEF's and LINES
	for rrd in `ls $HOST.disk,*.rrd` 
	do
		# gosh i miss perl
		PART=`echo $rrd | $SED 's/^.*disk,/,/' | $SED 's/\.rrd//' `
		# the PARTRRD is arbitrary, so trying to use
		# partition name was senseless cause rrdtool
		# is very sensitive to non-alphanumeric characters
		PARTRRD=$p
		p=`expr $p + 1`

		# the comment should try and look nice
		PARTCOMM=`echo $PART | $SED 's/,/\//g' `
		if [ $PARTCOMM = "/root" ]
		then
			PARTCOMM="/"
		fi		
		
		# build the DEFs var
		DEFS="$DEFS DEF:$PARTRRD=$rrd:pct:AVERAGE"

		# get the color to use
		# this is soooooo bad but it was the best 
		# i could come up with
		i=0
		for col in $COLORS
		do
			if [ $c -eq $i ]
			then
				COL=$col
			fi
			i=`expr $i + 1`
		done
		if [ $c -eq 12 ]
		then
			c=0
		else
			c=`expr $c + 1`
		fi
				
		# build the LINES var
		LINES="$LINES LINE2:$PARTRRD#$COL:$PARTCOMM"
	done

	# now start building graphs
	# define the graphs for each time period
	HOURLY=$GRAPHS/$HOST.diskpct.hourly.png

	# make the hourly graph
	$RRDTOOL graph - -s e-48h --title="$HOST Disk Utilization last 48 Hours" \
	-w 576 -a PNG -z -v "% Full" -u 100 -l 0 $DEFS $LINES \
	COMMENT:"\s" COMMENT:"$DATE\r"
	

done
