#!/bin/ksh
# $Id: slapdstats.ksh,v 1.5 2008/07/31 13:25:35 ksb Exp $
# sample an instance of Netscape SLAPD for stats, send them  to PEG. (ksb)
#
# our program name and usage message
progname=`basename $0`
PATH=$HOME/bin:$PATH:/usr/local/libexec:/usr/local/bin
export PATH
FIND_LIST="threads current total dtablesize readwaiters opsinitiated opscompleted backlog entriessent bytessent nbackends"

# The slapd-monitor script must output the name of the instance
# as "SLAP_NAME=$name" where host/$MYNAME/app-$name.rrd is the name of
# the app-slapd RRD file.  Else we'll use "slapd".
usage="$progname: usage [-dx] [-p delay] [-N node] [-O admin] [-S suffix] [-T toplevel] [peg][:port]
$progname: -h|-V"


# how to slide a single letter option off the beginning of a bundle
# -barf -> -arf
slide='P=$1; shift; set _ -`expr _"$P" : '\''_-.\(.*\)'\''` ${1+"$@"}; shift'
param='if [ $# -lt 2 ]; then echo "$progname: missing value for $1" 1>&2 ; exit 1; fi'

# default values for all the flags, or leave unset for a ${flag-value} form
SLAP_NAME=slapd
XFLAG=""
DFLAG=false
ADMIN=""
SUFFIX="sac"
DELAY="once"
TOPLEVEL="fedex.com"
VERSION=false
MYNAME=""
CN=monitor
PEG=peg.sac.fedex.com:31415

# get the options from the command line (+ any variables)
while [ $# -gt 0 ]
do
	case "$1" in
	-d)
		DFLAG=true
		shift
		;;
	-d*)
		DFLAG=true
		eval "$slide"
		;;
	-N)
		eval "$param"
		MYNAME=$2
		shift ; shift
		;;
	-N*)
		MYNAME=`expr _"$1" : _'-.\(.*\)'`
		shift
		;;
	-O)
		eval "$param"
		ADMIN=$2
		shift ; shift
		;;
	-O*)
		ADMIN=`expr _"$1" : _'-.\(.*\)'`
		shift
		;;
	-p)
		eval "$param"
		DELAY=$2
		shift ; shift
		;;
	-p*)
		eval "$param"
		DELAY=$2
		shift ; shift
		;;
	-S)
		eval "$param"
		SUFFIX=$2
		shift ; shift
		;;
	-S*)
		eval "$param"
		SUFFIX=$2
		shift ; shift
		;;
	-T)
		eval "$param"
		TOPLEVEL=$2
		shift ; shift
		;;
	-T*)
		eval "$param"
		TOPLEVEL=$2
		shift ; shift
		;;
	-u*)
		CN=`expr _"$1" : _'-.\(.*\)'`
		shift
		;;
	-u)
		eval "$param"
		CN=$2
		shift ; shift
		;;
	-V*)
		echo $progname: '$Id: slapdstats.ksh,v 1.5 2008/07/31 13:25:35 ksb Exp $'
		VERSION=true
		shift
		break
		;;
	-x)
		XFLAG=-x
		shift
		;;
	-x*)
		XFLAG=-x
		eval "$slide"
		;;
	--)
		shift
		break
		;;
	-h|-h*)
		cat <<HERE
$usage
d          display RRD path only, and exit
h          output a brief help message
N node     use this node name, rather than our hostname
O admin    set the administrators department suffix
p delay    update persistantly, about every delay seconds
S suffix   remove this suffix after toplevel, if present
T toplevel remove this from the end of our hostname
u cn       slapd login
V          output the standard version information
x          trace updates on stdout
peg        sample collection host, running rrdd
port       rrdd update port (otherwise 31415)
HERE
		exit 0
		;;
	-*)
		echo "$usage" 1>&2 
		exit 1
		;;
	*)
		# process and continue for intermixed options & args
		break
		;;
	esac
done

if [ -z "$MYNAME" ] ; then
	MYNAME=`(hostname 2>/dev/null || uname -n)`
	MYNAME=${MYNAME%%.$TOPLEVEL}
	MYNAME=${MYNAME%%.$SUFFIX}
	[ -z "$ADMIN" ] || MYNAME="$MYNAME.$ADMIN"
fi

# get the files left on the line
if [ $# -ne 0 ] ; then
	PEG=$1
	shift
fi

if $VERSION ; then
	echo "$progname: host: $MYNAME"
	echo "$progname: peg as $PEG"
	echo "$progname: cn as $CN"
	exit 0
fi

LABELS=""
VALUES=""
function MkUpdate {
	(
	# Find the ldapsearch program and run it to find stats about
	# our local domain (as cn=monitor, or $1). -- ksb
	unset LD_LIBRARY_PATH
	PATH=/opt/netscape/consumer5/shared/bin:/LDAP/usr/iplanet/servers/shared/bin:$PATH;export PATH
	cd $(dirname `whence ldapsearch`)
	ldapsearch -p 8329 -b "cn=$CN" -s base "objectClass=*"
	) | sed -n -e '
/^SLAP_NAME=/p
/^verion:/d
/^dn: /d
/^objectClass: /d
/^objectClass: /d
/^cn: /d
s/^thread[s: ]*\([0-9]*\)/threads=\1/p
s/^currentconnections[s: ]*\([0-9]*\)/current=\1/p
s/^totalconnections[s: ]*\([0-9]*\)/total=\1/p
s/^dtablesize[s: ]*\([0-9]*\)/dtablesize=\1/p
s/^readwaiter[s: ]*\([0-9]*\)/readwaiters=\1/p
s/^opsinitiated[s: ]*\([0-9]*\)/opsinitiated=\1/p
s/^opscompleted[s: ]*\([0-9]*\)/opscompleted=\1/p
s/^request-que-backlog[s: ]*\([0-9]*\)/backlog=\1/p
s/^entriessent[s: ]*\([0-9]*\)/entriessent=\1/p
s/^bytessent[s: ]*\([0-9]*\)/bytessent=\1/p
/^currenttime: /d
/^starttime: /d
s/^nbackend[s: ]*\([0-9]*\)/nbackends=\1/p
/^backendmonitordn: /d' |
	while read line 
	do
		eval $line
	done

	VALUES="N"
	for Name in $FIND_LIST
	do
		eval This=\$$Name
		[ -z "$This" ] && continue
		LABELS="$LABELS:$Name"
		VALUES="$VALUES:$This"
	done
	LABELS=${LABELS#:}
}

if $DFLAG ; then
	echo host/$MYNAME/app-$SLAP_NAME.rrd
	exit 0
fi

while true ; do
	MkUpdate
	echo "update host/$MYNAME/app-$SLAP_NAME.rrd -t $LABELS $VALUES"
	[ once = $DELAY ] && break
	sleep $DELAY
done | rrdup $XFLAG $PEG 

exit 0
