#!/bin/sh

# Script to generate some stats about Big Brother
# Copyright 2002 Jeff Stoner
# Written by Jeff Stoner <jstoner@blackboard.com>

# INSTALL:
#  - copy to your cgi-bin directory, set ownership and permissions as appropriate
#  - set the BBHOME and BBVAR variables as appropriate
#  - uncomment the line you'd like to use for counting your "configured" clients (NUM_CONFIG_CLIENTS)

# NOTES/GOTCHAS:
#  - you may need to adjust the 'du' lines as not all platforms support the '-h' option
#  - if you don't have a lot of clients, you may need to adjust the SIZE_LOGSK variable
#    so it reports in bytes instead of kilobytes ('man du' for more info)
#  - the grep used in NUM_CONFIG_CLIENTS uses the character class [[:space:]] to catch spaces/tabs
#    this may not be portable

# Set some stuff
BBHOME="/opt/bb"
BBVAR="/opt/bbvar"


# Gather some stats
# Uncomment the method that fits your site

# If you use BBConfig
#NUM_CONFIG_CLIENTS=`grep -c "^host:[[:space:]]" $BBHOME/etc/bb-config`

# for "parsing" bb-hosts directly
NUM_CONFIG_CLIENTS=`grep -c "^[[:space:]]*[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*[[:space:]]*" $BBHOME/etc/bb-hosts`

# number of clients actually reporting in
NUM_ACTUAL_CLIENTS=`/bin/ls -1 $BBVAR/logs | cut -d. -f1 | sort | uniq | wc -l`

# "clean" up loose spaces/tabs
NUM_CONFIG_CLIENTS=$NUM_CONFIG_CLIENTS
NUM_ACTUAL_CLIENTS=$NUM_ACTUAL_CLIENTS

# count the number of host.service
NUM_LOGS=`/bin/ls -1 $BBVAR/logs | wc -l`
NUM_LOGS=$NUM_LOGS

# calculate the average # of tests per host
AVG_TESTS=`expr $NUM_LOGS / $NUM_ACTUAL_CLIENTS`

# get the amount of disk space used by Big Brother
SIZE_LOGS=`du -sh $BBVAR/logs`  # size in "human readable" form - not supported on all platforms
SIZE_LOGSK=`du -sk $BBVAR/logs` # size in kilobytes
SIZE_HIST=`du -sh $BBVAR/hist`  # size in "human-readable" form
SIZE_HISTLOGS=`du -sh $BBVAR/histlogs`  # size in "human-readable" form

set bogus $SIZE_LOGS $SIZE_HIST $SIZE_HISTLOGS
SIZE_LOGS=$2
SIZE_HIST=$4
SIZE_HISTLOGS=$6

# Calculate some more stuff
set bogus $SIZE_LOGSK
SIZE_LOGSK=$2
# RATE = (size of log files in kilobytes) / (seconds per test)
RATE=`expr $SIZE_LOGSK / 300`
# TEST_RATE = (number of unique tests) / (seconds per test)
TEST_RATE=`expr $NUM_LOGS / 300`
# SIZE_HOUR = (size of logs files in kilobytes) * (number of tests per hour)
SIZE_HOUR=`expr $SIZE_LOGSK \* 12`

# Output header
echo "<HTML><HEAD><TITLE>Big Brother Stats</TITLE></HEAD><BODY>"

# Output body
echo "<P>Big Brother Statistics (not scientific)</P>"
echo "<BR>Number of configured servers: $NUM_CONFIG_CLIENTS"
echo "<BR>Number of clients actually reporting: $NUM_ACTUAL_CLIENTS"
echo "<BR>Number of unique host.service tests performed: $NUM_LOGS"
echo "<BR>Average number of tests performed per client: $AVG_TESTS"
echo "<BR>Size of the logs directory (BBVAR/logs): $SIZE_LOGS"
echo "<BR>Size of the history directory (BBVAR/hist): $SIZE_HIST"
echo "<BR>Size of the historical logs directory (BBVAR/histlogs): $SIZE_HISTLOGS"
echo "<P>Traffic stats</P>"
echo "<BR>The Big Brother daemon processes test results at $RATE KB per second"
echo "<BR>Further, the Big Brother daemon is processing over $SIZE_HOUR KB every hour."
echo "<BR>Finally, the Big Brother daemon handles $TEST_RATE tests per second."


# Finish the job
echo "<CENTER><P><A HREF=\"/\">Return from whence you came</A></P></CENTER></BODY></HTML>"

exit 0

