#! /bin/sh

# initialize an RRD file to collect DCC statistics
#   [-x]	    debugging
#   [-q]	    quiet
#   [-h dcc_homedir]
#   [-D data-dir]   where to put the rrdtool file
#   [-T /usr/local/bin/rrdtool]
#		    see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
#			or the FreeBSD package.
#   [-O rrdopts]    "--heartbeat X" or "--step Y"
#   file	    RRD database that receives the collected data


# Copyright (c) 2005 by Rhyolite Software
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.2.74-1.12 $Revision$
#	@configure_input@

exec 1>&2 </dev/null

DCC_HOMEDIR=@prefix@
DEBUG=
# check the args once to get the home directory
while getopts "xqh:D:T:O:" c; do
    case $c in
	x) set -x; DEBUG=-x=;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf

DATADIR=$DCC_HOMEDIR/stats
RRDTOOL=/usr/local/bin/rrdtool
RRDOPTS=
USAGE="`basename $0`: [-xq] [-h homedir] [-D data-dir] [-T rrdtool]
    [-O rrdopts] file.rrd"
OPTIND=1
while getopts "xqh:D:T:O:" c; do
    case $c in
	x) ;;
	q) QUIET=quiet;;
	h) ;;
	D) DATADIR="$OPTARG";;
	T) RRDTOOL="$OPTARG";;
	O) RRDOPTS="$RRDOPTS $OPTARG";;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 1; then
    echo "$USAGE" 1>&2
    exit 1
fi
FILE=$1

if test ! -d $DATADIR; then
    mkdir $DATADIR
fi
cd $DATADIR

if test "$FILE" = "`basename $FILE .rrd`"; then
    FILE=$FILE.rrd
fi
if test -s "$FILE"; then
    echo "$FILE already exists" 1>&2
    exit 1
fi


# Normally sample every 10 minutes.
STEP=600

HBEAT=
while test ! -z "$RRDOPTS"; do
    RRDOPTS=`expr "X$RRDOPTS" : 'X *\(.*\)'`
    if test -z "$RRDOPTS"; then
	break;
    fi
    X=`expr "X$RRDOPTS" : 'X--heartbeat[ 	][	]*\([0-9][0-9]*\)'`
    if test ! -z "$X"; then
	HBEAT=$X
	RRDOPTS=`expr "X$RRDOPTS" :  "X--heartbeat[	]$X *\(.\)"`
	continue
    fi
    X=`expr "X$RRDOPTS" : 'X--step[ 	][	]*\([0-9][0-9]*\)'`
    if test ! -z "$X"; then
	STEP=$X
	RRDOPTS=`expr "X$RRDOPTS" :  "X--step[	]$X *\(.\)"`
	continue
    fi
    echo 'unrecognized RRD option "'"$RRDOPTS"'"' 1>&2
    exit 1
done

# Use a heartbeat of 31 minutes or 1860 seconds to allow a missed sample as
#   well as jitter by cron.  If you use a heartbeat equal to the cron
#   repetition rate, then a single second delay by cron will make the previous
#   period have no samples and be undefined.
if test -z "$HBEAT"; then
    HBEAT=`expr $STEP \* 3 + 60`
fi

# Keep hourly (or 6-step) average data for 3 years
HAVG_STEPS=`expr 3600 / $STEP`
HAVG_ROWS=`expr \( 3 \* 366 \* 24 \* 3600 \) / \( $STEP \* 6 \)`

# Keep 10-minute (or single-step) average data for a month
SAVG_ROWS=`expr 31 \* 24 \* $HAVG_STEPS`

# Keep daily minimum data for 3 years
DMIN_STEPS=`expr \( 3600 \* 24 \) / $STEP`
DMIN_ROWS=`expr 3 \* 366`

$RRDTOOL create $FILE --step $STEP --start -48month		\
    DS:reports:DERIVE:$HBEAT:0:1000				\
    DS:bulk:DERIVE:$HBEAT:0:1000				\
    DS:spam:DERIVE:$HBEAT:0:1000				\
    DS:hashes:GAUGE:$HBEAT:U:U					\
    DS:flooded:DERIVE:$HBEAT:0:100000				\
    RRA:AVERAGE:0.5:$HAVG_STEPS:$HAVG_ROWS			\
    RRA:AVERAGE:0.04:1:$SAVG_ROWS				\
    RRA:MIN:0.9:$DMIN_STEPS:$DMIN_ROWS
