#! /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) 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.17 $Revision$
#	Generated automatically from stats-get.in by configure.

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
    echo "$USAGE" 1>&2
    exit 1
fi

HOST=$1

if test "$USE_STDIN" = yes; then
    cat
else
    exec </dev/null
    if test -n "$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
fi								\
    | tee $STATSFILE_NEW 					\
    | awk '
	/hash entries/{ 
	    hashes = $4; 
	}
	/query/{
	    queries = $8;
	    sub(/^-/,"",queries);
	}
	/reports.*many/{
	    reports = $1;
	    sub(/^-/,"",reports);
	}
	/answers.*>10/{
	    split($2,bulk,">");
	    spam=$5;
	    sub(/^-/,"",spam);
	}
	/accepted/{
	    flooded=$1;
	    sub(/^-/,"",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
