#! /bin/sh

# get statistics from a DCC server
#   [-x]	    debugging
#   [-q]	    quiet
#   [-S]	    read `cdcc stats` from stdin
#   [-s stats-file] save raw `cdcc stats` output in stats-file
#   [-i client-ID]  that DCC server will accept
#   [-p password]   that DCC server will accept
#   host	    server to ask for numbers

# A single line of 5 decimal numbers separated by colons is sent to stdout:
#   For example,
#	115492:71318:44694:2462909:155924
#   means the DCC server has
#	115492  received reports of checksums from DCC clients
#	71318	received reports where the target count was >= 10
#	44694	received reports where the target count was "many"
#	2462909	entries in its hash table
#	155924	received reports by flooding


# Copyright (c) 2008 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 without
# changes for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear in all
# copies and any distributed versions or copies are either unchanged
# or not called anything similar to "DCC" or "Distributed Checksum
# Clearinghouse".
#
# Parties not eligible to receive a license under this agreement can
# obtain a commercial license to use DCC by contacting Rhyolite Software
# at sales@rhyolite.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.103-1.20 $Revision$
#	@configure_input@

DEBUG=
QUIET=
STATSFILE=
STATSFILE_NEW=/dev/null
CLNT_ID="1"
PASSWD=
USAGE="`basename $0`: [-xqS] [-s stats-file] [-i client-ID] [-p password] [host]"
while getopts "xqSs:i:p:" c; do
    case $c in
	x) set -x; DEBUG=-x;;
	q) QUIET="-q";;
	S) USE_STDIN=yes;;
	s) if test $OPTARG != /dev/null; then
		STATSFILE=$OPTARG
		STATSFILE_NEW=$STATSFILE.$$
		trap "/bin/rm -f $STATSFILE_NEW" 0 1 2 15
	    fi
	    ;;
	i) CLNT_ID="$OPTARG";;
	p) if test "$OPTARG" != ""; then
		PASSWD="password $OPTARG;"
	    fi
	    ;;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 1; then
    if test "$#" -ne 0 -o -z "$USE_STDIN"; then
	echo "$USAGE" 1>&2
	exit 1
    fi
fi

HOST=$1

if test "$USE_STDIN" = yes; then
    cat
else
    exec </dev/null
    if test -n "$QUIET"; then
	@bindir@/cdcc "id $CLNT_ID; $PASSWD host $HOST; stats" 2>/dev/null
    else
	@bindir@/cdcc "id $CLNT_ID; $PASSWD host $HOST; stats"
    fi
fi								\
    | tee $STATSFILE_NEW					\
    | awk '
	/hash entries/{
	    hashes = $4;
	}
	/query/{
	    queries = $8;
	    if (queries ~ /^-/) {
		# negative numbers are 32-bit overflows
		queries = 4294967296 + queries;
	    }
	}
	/reports.*many/{
	    reports = $1;
	    if (reports ~ /^-/) {
		reports = 4294967296 + reports;
	    }
	}
	/answers.*>10/{
	    split($2,bulk,">");
	    if (bulk[1] ~ /^-/) {
		bulk[1] = 4294967296 + bulk[1];
	    }
	    spam=$5;
	    if (spam ~ /^-/) {
		spam = 4294967296 + spam;
	    }
	}
	/accepted/{
	    flooded=$1;
	    if (flooded ~ /^-/) {
		flooded = 4294967296 + flooded;
	    }
	}
	END {
	    printf "%s:%s:%s:%s:%s\n",
		    reports+queries, bulk[1], spam, hashes, flooded;
	}'

if test $STATSFILE_NEW != /dev/null -a -s $STATSFILE_NEW; then
    rm -f $STATSFILE
    mv $STATSFILE_NEW $STATSFILE
fi
