#!/bin/sh
######################################################################
#            _       _        _        _
#           (_)     | |      | |      | |
#            _ _ __ | |_ ___ | |_ __ _| |
#           | | '_ \| __/ _ \| __/ _` | |
#           | | |_) | || (_) | || (_| | |
#           |_| .__/ \__\___/ \__\__,_|_|
#             | |
#             |_| V0.3 Dec. 13, 2001
#		  By Antoine Megens
#		  webmaster@dingetje.homeip.net
#
# RRD daemon script to measure total IP bandwidth usage
# without the need for an SNMP daemon
#
# part of iptotal package for FREESCO
#
######################################################################

. /etc/iptotal/iptotal.cfg

ARCHIVE_DONE=0

do_archive() {
DAYNAME=`${DATECMD} +%d%b%Y`                              
DOARCHIVE=0

TIMESTAMP=`${DATECMD} +%H%M`
if [ ${TIMESTAMP} -gt 2358 ]; then  # Just before Midnight?
   if [ ${ARCHIVE_DONE} = 0 ]; then # and not archived yet?
       ARCHIVEFILE=${ARCHIVE}/${DAYNAME}.png
       DOARCHIVE=1
   fi
fi
if [ ${TIMESTAMP} -lt 0005 ]; then # Just after Midnight?
   ARCHIVE_DONE=0 # clear flag
fi
if [ ${DOARCHIVE} = 1 ]; then
	#
	# create a daily graph from the current rrd data in archive
	#
	/usr/bin/rrdtool graph ${ARCHIVEFILE} \
          --title "Total data throughput (${DAYNAME})" \
          --imgformat PNG \
          --width 600 \
          --height 150 \
          --end 'now-60s' \
          --start 'end-24h' \
          DEF:in=/var/lib/iptotal/iptotal.rrd:input:AVERAGE \
          DEF:out=/var/lib/iptotal/iptotal.rrd:output:AVERAGE \
          DEF:maxin=/var/lib/iptotal/iptotal.rrd:input:MAX \
          DEF:maxout=/var/lib/iptotal/iptotal.rrd:output:MAX \
          AREA:in#009828:"In kbyte(s)" \
          LINE2:out#ff0000:"Out kbyte(s)" \
          LINE2:maxin#0030ff:"Max In" \
          LINE2:maxout#ff8040:"Max Out"

	#
	# now remove files older than 30 days from archive
	#
	${ROOT_DIR}/bin/find ${ARCHIVE}/*.png +30 -type f exec rm -rf {} \;

	ARCHIVE_DONE=1
fi
}

######################################################################
#
# go to work....
#
if [ ! -f $ETH_DBASE ]; then

	echo Creating new RRD database...

	/usr/bin/rrdtool create $ETH_DBASE \
		--step ${INTERVAL} \
		DS:total:GAUGE:120:0:10000 \
		DS:input:GAUGE:120:0:10000 \
		DS:output:GAUGE:120:0:10000 \
		RRA:AVERAGE:0.5:1:1440 \
		RRA:AVERAGE:0.5:60:1200 \
		RRA:MIN:0.5:60:1200 \
		RRA:MAX:0.5:60:1200 \
		RRA:AVERAGE:0.5:1440:365 \
		RRA:MIN:0.5:1440:365 \
		RRA:MAX:0.5:1440:365

	# make it readable for user nobody (HTTP user)
	chown www-data:www-data $ETH_DBASE
fi

#
# collect data, but only if..
#
if [ -f $ETH_DBASE ]; then
   #
   # the database exists, and..
   #
   if [ -f ${ROOT_DIR}/sbin/iptotal ]; then
        #
        # iptotal is installed...

        #--------------------------------------------------
        # save PID of this endless loop, so we can stop it
	#--------------------------------------------------
	echo $$ > /var/run/iptotal.pid
	
	while true
	do
		#-------------------------------------------------
		# run iptotal and get output in TOTAL
		#-------------------------------------------------
		RESULT=`${ROOT_DIR}/sbin/iptotal -r ${INTERVAL} ${ETHDEV}`

		total=0;
		input=0;
		output=0;

		set $RESULT
		#
		#  $1   $2  $3     $4 $5  $6      $7  $8
		#Total: 0 kbyte(s) In: 0 kbyte(s) Out: 0 kbyte(s)
		#--------------------------------------------------
		# we need 2nd, 5th and 8th element of the output
		#--------------------------------------------------
		if [ ! -z "$2" ]; then
		   total=$2
		fi
		if [ ! -z "$5" ]; then
		   input=$5
		fi
		if [ ! -z "$8" ]; then
		   output=$8
		fi
		
		#
		# show result in text file for web page to include
		#
		if [ ! -f /var/lib/iptotal/result.txt ]; then
			touch /var/lib/iptotal/result.txt
			chown www-data:www-data /var/lib/iptotal/result.txt
		fi
		echo $RESULT > /var/lib/iptotal/result.txt

		#---------------------------
		# update the database
		#---------------------------
		/usr/bin/rrdtool update $ETH_DBASE \
		   --template total:input:output \
		   N:$total:$input:$output

		#
		# check if daily graph should be created in archive
		#	
		do_archive
	done
   fi
fi
