#!/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"
BBRRDS="/usr/local/bb/rrd"
BBHOME="/usr/local/bb"


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

# set -x

# mem-graph.sh

# this script will build load swap available percentage 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`

# 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

for rrd in `ls ${DYNAMIC_HOSTNAME}.swap.rrd`
do
	# need some vars again
	HOST=`echo $rrd | $SED 's/\\.swap\.rrd//' `

	# define the graphs for each time period
	HOURLY=$GRAPHS/$HOST.swap.hourly.png
	DAILY=$GRAPHS/$HOST.swap.daily.png
	WEEKLY=$GRAPHS/$HOST.swap.weekly.png
	MONTHLY=$GRAPHS/$HOST.swap.monthly.png

	# make the hourly graph
	$RRDTOOL graph - -s e-48h \
		--title "$HOST % Swap Used Last 48 Hours" \
		-z -w 576 -v "% Swap Used" -a PNG -u 100 -l 0  \
		DEF:avg=$rrd:swap:AVERAGE AREA:avg#$COLOR:"5 Minute Average" \
		GPRINT:avg:LAST:'Current\: %5.1lf%S' \
		GPRINT:avg:MIN:'Min\: %5.1lf%S' \
		GPRINT:avg:AVERAGE:'Average\: %5.1lf%S' \
		GPRINT:avg:MAX:'Max\: %5.1lf%S\j' \
		COMMENT:"$DATE\l"

done
