#!/bin/sh
echo "Content-type: text/html"
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"
GRAPHS="/usr/local/bb/www/graphs"
RRDTOOL="/usr/local/rrdtool/bin/rrdtool"


echo "<HTML> <HEAD> <TITLE> Memory trends for $DYNAMIC_HOSTNAME "
echo "</TITLE> </HEAD> <BODY> <CENTER>"
echo "<H1> Memory trends for $DYNAMIC_HOSTNAME </H1> "

# $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 memory 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}.mem.rrd`
do
	# need some vars again
	HOST=`echo $rrd | $SED 's/\\.mem\.rrd//' `

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

	# make the hourly graph
	$RRDTOOL graph $HOURLY -s e-48h \
		--title "$HOST % Memory Used Last 48 Hours" \
		-z -w 576 -v "% Memory Used" -u 100 -l 0 -a PNG  \
		DEF:avg=$rrd:mem: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" >>/dev/null


	# make the daily graph
	$RRDTOOL graph $DAILY -s e-12d \
		--title "$HOST % Memory Used Last 12 Days" \
		-w 576 -z -a PNG -v "% Memory Used" -u 100 -l 0 \
		DEF:avg=$rrd:mem:AVERAGE AREA:avg#$COLOR:"30 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" >>/dev/null

	# make the weekly graph
	$RRDTOOL graph $WEEKLY -s e-48d \
		--title "$HOST % Memory Used Last 48 Days" \
		-w 576 -z -a PNG -v "% Memory Used" -u 100 -l 0 DEF:avg=$rrd:mem:AVERAGE \
		AREA:avg#$COLOR:"2 Hour 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" >>/dev/null


	# make the monthly graph
	$RRDTOOL graph $MONTHLY -s e-576d \
		--title "$HOST % Memory Used Last 576 Days" \
		-w 576 -z -a PNG -v "% Memory Used" -u 100 -l 0 DEF:avg=$rrd:mem:AVERAGE \
		AREA:avg#$COLOR:"1 Day 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" >>/dev/null

done


echo "<center>"

echo "<table>"
echo "<tr> <th>Hourly</th> </tr>"
echo "<tr> <td><img SRC="/bb/graphs/${HOST}.mem.hourly.png"><br></td> </tr>"
echo "<tr> <th>Daily</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.mem.daily.png\"><br></td> </tr>"
echo "<tr> <th>Weekly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.mem.weekly.png\"><br></td> </tr>"
echo "<tr> <th>Monthly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.mem.monthly.png\"></td> </tr>"
echo "</table> </center>"

echo "<BR> larrd is proud to be:"
echo "<A HREF=\"http://ee-staff.ethz.ch/~oetiker/webtools/rrdtool/\">"
echo "<IMG SRC="/bb/gifs/rrdtool.gif"></A> "
echo "</body> </html>"


