#! /bin/sh

# get statistics from a DCC server
#   [-x]	debugging
#   [-q]	quiet
#   [-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) 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.9 $Revision$
#	Generated automatically from stats-get.in by configure.

DEBUG=
QUIET=
STATS=
STATS_NEW=/dev/null
CLNT_ID="1"
PASSWD=
USAGE="`basename $0`: [-xq] [-s stats-file] [-i client-ID] [-p password] host"
while getopts "xqs:i:p:" c; do
    case $c in
	x) set -x; DEBUG=-x;;
	q) QUIET="-q";;
	s) if test $OPTARG != /dev/null; then
		STATS=$OPTARG
		STATS_NEW=$STATS.$$
		trap "/bin/rm -f $STATS_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
    echo "$USAGE" 1>&2
    exit 1
fi

HOST=$1

if test ! -z "$QUIET"; then
    /usr/bin/cdcc "id $CLNT_ID; $PASSWD host $HOST; stats" 2>/dev/null
else
    /usr/bin/cdcc "id $CLNT_ID; $PASSWD host $HOST; stats"
fi | tee $STATS_NEW | awk '/hash entries/{ hashes = $4; }
	/query/{
	    queries = $8;
	}
	/^ *[0-9][0-9 ]*reports/{
	    reports = $1;
	}
	/answers.*>10/{
	    split($2,bulk,">");
	    spam=$5;
	}
	/accepted/{
	    flooded=$1;
	    printf "%s:%s:%s:%s:%s\n",
		reports+queries, bulk[1], spam, hashes, flooded;
	}'

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