#! /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
#   [-R]	    do not make spam ratio graphs
#   [-m]	    generate graphs of total message rates.
#   [-h dcc_homedir]
#   [-T /usr/local/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/dcc/stats


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

exec 1>&2 </dev/null

DCC_HOMEDIR=/etc/dcc
DEBUG=
# check the args once to get the home directory
while getopts "xqBdRmh: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"
AVGFMT="%.1lf %S"
M_YGRID="--alt-y-mrtg"
P_YGRID="--y-grid 25:2"
H_YGRID="--alt-y-mrtg"
XYEAR_MONTHS=2
GRAPH_DB=
GRAPH_TRAFFIC=
GRAPH_RATIO=yes
RRDOPTS=
RRDTOOL=/usr/local/bin/rrdtool
TITLE_SET=
SPAN="1day,1week,1month,1year"
USAGE="`basename $0`: [-xqBdRm] [-h homedir] [-T rrdtool] [-O rrdopts]
    [-t title] [-s span] gname rrd1 rrd2 rrd3 ..."
OPTIND=1
while getopts "xqBdRmh:T:O:t:s:" c; do
    case $c in
	x) ;;
	q) QUIET="-q";;
	B) ATTRIBS="--width 600 --height 240"; AVGFMT="%.0lf/day"
	    M_YGRID= P_YGRID= H_YGRID="--alt-autoscale-max"
	    XYEAR_MONTHS=1;;
	d) GRAPH_DB=yes;;
	R) GRAPH_RATIO=;;
	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 -n "$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
STEP=1
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

    NSTEP=`$RRDTOOL info $FILE | sed -n -e 's/^step = \([0-9][0-9]*\)/\1/p'`
    if test 0$NSTEP -gt $STEP; then
	STEP=$NSTEP
    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"

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="`/usr/bin/perl -e 'use POSIX qw(strftime);		\
		print strftime "%x %R %Z", localtime(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="--x-grid 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="--x-grid 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="--x-grid WEEK:1:WEEK:1:WEEK:1:0:%b/%d"
	    SPAN=1month
	    ;;
	1year)
	    # 28*24*60*60 = 2419200
	    XGRID="--x-grid MONTH:1:YEAR:1:MONTH:$XYEAR_MONTHS:2419200:%b"
	    SPAN=1year
	    ;;
	2years)
	    # 28*24*60*60 = 2419200
	    XYEAR_MONTHS=`expr $XYEAR_MONTHS \* 2`
	    XGRID="--x-grid MONTH:1:YEAR:1:MONTH:$XYEAR_MONTHS:2419200:%b"
	    SPAN=2years
	    ;;
	3years)
	    # 28*24*60*60 = 2419200
	    XYEAR_MONTHS=`expr $XYEAR_MONTHS \* 3`
	    XGRID="--x-grid MONTH:1:YEAR:1:MONTH:$XYEAR_MONTHS:2419200:%b"
	    SPAN=3years
	    ;;
    esac

    if test "$GRAPH_RATIO" = yes; then
	ONAME=$GNAME-spam-ratio.$DUR.$FTYPE
	echo "$ONAME: " | tr -d '\012'
	if test -n "$TITLE"; then
	    TITLE_STR="Spam Ratio at $TITLE"
	fi
	$RRDTOOL graph $ONAME $RRDOPTS					\
	    --end $END --start end-$SPAN				\
	    $ATTRIBS --title "$TITLE_STR"				\
	    $XGRID $P_YGRID						\
	    --unit '%%' --lower-limit 0 --upper-limit 100		\
	    $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"
    fi

    if test "$GRAPH_TRAFFIC" = yes; then
	ONAME=$GNAME-spam.$DUR.$FTYPE
	echo "$ONAME: " | tr -d '\012'
	if test -n "$TITLE"; then
	    TITLE_STR="Mail Checked at $TITLE"
	fi
	$RRDTOOL graph $ONAME $RRDOPTS					\
	    --end $END --start end-$SPAN				\
	    $ATTRIBS --title "$TITLE_STR"				\
	    $XGRID $M_YGRID						\
	    --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 $AVGFMT"			\
	    "GPRINT:bulk:AVERAGE:$AVGFMT"				\
	    "COMMENT:$TS\j"
    fi

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