#! /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) 2006 by Rhyolite Software, LLC
#
# This agreement is not applicable to any entity which sells anti-spam
# solutions to others or provides an anti-spam solution as part of a
# security solution sold to other entities, or to a private network
# which employs the DCC or uses data provided by operation of the DCC
# but does not provide corresponding data to other users.
#
# 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.
#
# Parties not eligible to receive a license under this agreement can
# obtain a commercial license to use DCC and permission to use
# U.S. Patent 6,330,590 by contacting Commtouch at http://www.commtouch.com/
# or by email to nospam@commtouch.com.
#
# A commercial license would be for Distributed Checksum and Reputation
# Clearinghouse software.  That software includes additional features.  This
# free license for Distributed ChecksumClearinghouse Software does not in any
# way grant permision to use Distributed Checksum and Reputation Clearinghouse
# software
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
# 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.3.45-1.19 $Revision$
#	Generated automatically from dcc-stats-init.in by configure.

exec 1>&2 </dev/null

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

REPS=
DATADIR=$DCC_HOMEDIR/stats
RRDTOOL=/usr/local/bin/rrdtool
RRDOPTS=
USAGE="`basename $0`: [-xqR] [-h homedir] [-D data-dir] [-T rrdtool]
    [-O rrdopts] file.rrd"
OPTIND=1
while getopts "xqRh:D:T:O:" c; do
    case $c in
	x) ;;
	q) QUIET=quiet;;
	R) REPS=yes;;
	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


# assume no server will handle more than 1000 operations/second
OPSPS=1000

# default sample every 10 minutes.
STEP=600

# be willing to get STEP and heartbeat from the -O arg(s)
HBEAT=
while test -n "$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 -n "$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 -n "$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 5 years
HAVG_STEPS=`expr 3600 / $STEP`
HAVG_ROWS=`expr \( 5 \* 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 5 years
DMIN_STEPS=`expr \( 3600 \* 24 \) / $STEP`
DMIN_ROWS=`expr 5 \* 366`

$RRDTOOL create $FILE --step $STEP --start -48month		\
    DS:reports:DERIVE:$HBEAT:0:$OPSPS				\
    DS:bulk:DERIVE:$HBEAT:0:$OPSPS				\
    DS:spam:DERIVE:$HBEAT:0:$OPSPS				\
    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
