#! /bin/sh

# graph collected DCC statistics in .png files.
#   [-x]	    debugging
#   [-q]	    quiet
#   [-B]	    make big graphs
#   [-d]	    generate graphs of database sizes and flooding rates
#   [-m]	    generate graphs of total message rates.
#   [-h dcc_homedir]
#   [-T /usr/bin/rrdtool]
#		    see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
#			or the FreeBSD package.
#   [-O rrdopts]    additional rrdtool options for all graphs
#   [-t title]	    for graphs
#   [-s span]	    of time covered by graphs.
#			The default is "1day,1week,1month,1year"
#   gname	    basic file name for graphs
#   rrd1,...	    RRD databases that will be combined to produce the graphs

# The rrd files must be initialzed with dcc-stats-init, which is called
#   automatically by dcc-stats-collect.  Data must be collected very
#   10 minutes with dcc-stats-collect.  The rrd files should be in
#   /var/lib/dcc/stats


# Copyright (c) 2003 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.16-1.19 $Revision$
#	Generated automatically from dcc-stats-graph.in by configure.

exec 1>&2 </dev/null

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

QUIET=
ATTRIBS="--width 200 --height 40"
GRAPH_DB=
GRAPH_TRAFFIC=
RRDOPTS=
RRDTOOL=/usr/bin/rrdtool
TITLE_SET=
SPAN="1day,1week,1month,1year"
USAGE="`basename $0`: [-xqdm] [-h homedir] [-T rrdtool] [-O rrdopts]
    [-t title] [-s span] gname rrd1 rrd2 rrd3 ..."
OPTIND=1
while getopts "xqBdmh:T:O:t:s:" c; do
    case $c in
	x) ;;
	q) QUIET="-q";;
	B) ATTRIBS="--width 600 --height 120";;
	d) GRAPH_DB=yes;;
	m) GRAPH_TRAFFIC=yes;;
	h) ;;
	T) RRDTOOL="$OPTARG";;
	O) RRDOPTS="$RRDOPTS $OPTARG";;
	t) TITLE_SET=yes; TITLE="$OPTARG";;
	s) SPAN="$OPTARG";;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -lt 1; then
    echo "$USAGE" 1>&2
    exit 1
fi

cd $DCC_HOMEDIR/stats

if test ! -z "$QUIET"; then
    exec 1>/dev/null
fi

GNAME=$1
if test "$#" -ge 2; then
    # assume .rrd file is same as graph name if absent
    shift
fi
if test -z "$TITLE_SET"; then
    TITLE="$GNAME"
fi

MAX_LAST=0
NDBS=0
CDEF_reports="CDEF:reports=Ureports,UNKN"
CDEF_bulk="CDEF:bulk=Ubulk,UNKN"
CDEF_spam="CDEF:spam=Uspam,UNKN"
while test $# -gt 0; do
    FILE=$1; shift
    if test "$FILE" = "`basename $FILE .rrd`"; then
	# this round-about tactic expands "*.foo.com"
	set -- $FILE.rrd $*
	continue
    fi

    NDBS=`expr $NDBS + 1`
    DB_FILE="$FILE"
    LAST=`$RRDTOOL last $FILE`
    if test "0$LAST" -gt "$MAX_LAST"; then
	MAX_LAST=$LAST
    fi

    for type in reports bulk spam; do
	eval DEFS_$type='"$DEFS_'$type DEF:$type$NDBS=$FILE:$type:AVERAGE'"'

	# When we sum data from servers, count missing data as 0 instead of
	# as unknown.
	if test $NDBS -eq 1; then
	    eval CDEF_U$type='"CDEF:'U$type=${type}1,UN'"'
	    eval CDEF_$type='"$CDEF_'$type,$type$NDBS,UN,0,$type$NDBS,IF'"'
	else
	    eval CDEF_U$type='"$CDEF_'U$type,$type$NDBS',UN,*"'
	    eval CDEF_$type='"$CDEF_'$type,$type$NDBS,UN,0,$type$NDBS,IF,+'"'
	fi
    done
done
CDEF_reports="$CDEF_reports,IF"
CDEF_bulk="$CDEF_bulk,IF"
CDEF_spam="$CDEF_spam,IF"

STEP=600

if test "$MAX_LAST" -ne 0; then
    # avoid odd times when individual servers were polled
    MAX_LAST=`expr $MAX_LAST - $MAX_LAST % $STEP`
    LAST=`expr $LAST - $LAST % $STEP`

    if TS="`date -r $MAX_LAST '+%x %R %Z'  2>/dev/null`"; then : ;
    else
	# deal with systems that do not have `date -r`
	TS="`perl -e 'use POSIX qw(strftime);			\
		print strftime "%x %R %Z", gmtime(time());'`"

    fi

    END=$MAX_LAST
    DB_END=`expr $LAST - $LAST % 86400`
else
    DB_END="now"
    END="now"
fi

# Does rrdtool do any file locking?  I can't find any mention of locking,
#   and so running it from CGI scripts looks too dangerous.
FTYPE=png
ATTRIBS="$ATTRIBS --imgformat `echo $FTYPE | tr '[a-z]' '[A-Z]'`"

for DUR in `echo $SPAN | tr ',' ' '`; do
    case $DUR in
	1day)
	    XGRID="HOUR:1:HOUR:2:HOUR:2:0:%k"
	    # as the "rdtool graph" man page suggests, don't be fooled
	    # by daylight savings time
	    SPAN=24h
	    ;;
	1week)
	    #24*3600 = 86400
	    XGRID="HOUR:6:DAY:1:DAY:1:86400:%a"
	    # as the "rdtool graph" man page suggests, don't be fooled
	    # by daylight savings time
	    SPAN=168h
	    ;;
	1month)
	    XGRID="WEEK:1:WEEK:1:WEEK:1:0:%b/%d"
	    SPAN=1month
	    ;;
	1year)
	    # 28*24*60*60 = 2419200
	    XGRID="MONTH:1:YEAR:1:MONTH:2:2419200:%b"
	    SPAN=1year
	    ;;
    esac

    echo -n "$GNAME-spam-ratio.$DUR.$FTYPE: "
    if test ! -z "$TITLE"; then
	TITLE_STR="Spam Ratio at $TITLE"
    fi
    $RRDTOOL graph $GNAME-spam-ratio.$DUR.$FTYPE $RRDOPTS		\
	--end $END --start end-$SPAN					\
	$ATTRIBS --title "$TITLE_STR"					\
	--x-grid "$XGRID" --alt-y-mrtg					\
	--unit '%%' --lower-limit 0 --upper-limit 100 --y-grid '25:2'	\
	$DEFS_reports $DEFS_bulk $DEFS_spam				\
	"$CDEF_Ureports" "$CDEF_Ubulk" "$CDEF_Uspam"			\
	"$CDEF_reports" "$CDEF_bulk" "$CDEF_spam"			\
	'CDEF:percentbulk=bulk,reports,/,100,*,0,100,LIMIT'		\
	'CDEF:percentspam=spam,reports,/,100,*,0,100,LIMIT'		\
	'AREA:percentbulk#cc0000:likely spam'				\
	'AREA:percentspam#ffb6c1:trapped spam\j'			\
	'GPRINT:percentbulk:AVERAGE:average %.0lf %%        '		\
	"COMMENT:$TS\j"

    if test "$GRAPH_TRAFFIC" = yes; then
	echo -n "$GNAME-spam.$DUR.$FTYPE: "
	if test ! -z "$TITLE"; then
	    TITLE_STR="Mail Checked at $TITLE"
	fi
	$RRDTOOL graph $GNAME-spam.$DUR.$FTYPE $RRDOPTS			\
	    --end $END --start end-$SPAN				\
	    $ATTRIBS --title "$TITLE_STR"				\
	    --x-grid "$XGRID" --alt-y-mrtg				\
	    --vertical-label "msgs/day"					\
	    $DEFS_bulk $DEFS_spam $DEFS_reports				\
	    "$CDEF_Ureports" "$CDEF_Ubulk" "$CDEF_Uspam"		\
	    "$CDEF_reports,86400,*" "$CDEF_bulk,86400,*"		\
	    "$CDEF_spam,86400,*"					\
	    'AREA:reports#87cefa:total mail'				\
	    'AREA:bulk#cc0000:likely spam'				\
	    'AREA:spam#ffb6c1:trapped spam\j'				\
	    "GPRINT:reports:AVERAGE:average %.1lf %S"			\
	    "GPRINT:bulk:AVERAGE:%.1lf %S"				\
	    "COMMENT:$TS\j"
    fi

    if test "$GRAPH_DB" = yes -a "$DUR" != 1day; then
	# take the database values from the last server
	echo -n "$GNAME-hashes.$DUR.$FTYPE: "
	if test ! -z "$TITLE"; then
	    TITLE_STR="Checksums at $TITLE"
	fi
	$RRDTOOL graph $GNAME-hashes.$DUR.$FTYPE $RRDOPTS		\
	    --end $DB_END --start end-$SPAN				\
	    $ATTRIBS --step 86400 --title "$TITLE_STR"			\
	    --x-grid "$XGRID"						\
	    DEF:hashes=$DB_FILE:hashes:MIN				\
	    'AREA:hashes#ffb6c1:'					\
	    "COMMENT:$TS\c"
    fi
done
