#
# $Id$
#
# control tool for maintaining Kamailio
#
#===================================================================

##### ----------------------------------------------- #####
### UNIXSOCK specific variables and functions
#

##### ----------------------------------------------- #####
### load CTL base
#
if [ -f "$MYLIBDIR/kamctl.ctlbase" ]; then
	. "$MYLIBDIR/kamctl.ctlbase"
else
	mwarn "Cannot load CTL core functions '$MYLIBDIR/kamctl.ctlbase' ..."
	# exit -1
fi

#
##### ----------------------------------------------- #####
### parameters
#
export CHROOT_DIR # needed for kamunix
if [ -z "$OSER_UNIXSOCK" ]; then
	OSER_UNIXSOCK=$CHROOT_DIR/tmp/kamailio.sock
fi

if [ -z "$OSERUNIX" ]; then
	OSERUNIX=kamunix
fi

#
##### ----------------------------------------------- #####
### functions
#
usage_unixsock() {
	echo
	mecho " -- command 'unixsock'"
	echo
cat <<EOF
 unixsock ........................... send raw unixsock command
EOF
}
USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_unixsock"


unixsock_cmd()
{
	mdbg "entering unixsock_cmd $*"
	if [ "$#" -lt 1 ]; then
		merr "unixsock_cmd must take at least command name as parameter"
		exit
	fi

	# construct the command now
	CMD=":$1:\n";
	shift
	while [ -n "$1" ] ; do
		CMD="${CMD}${1}\n"
		shift
	done
	CMD="${CMD}\n"
	
	printf "$CMD" | $OSERUNIX $OSER_UNIXSOCK | filter_fl

	mdbg "UNIXSOCK command was:\n$CMD"
}

CTLCMD=unixsock_cmd

unixsock_kamailio_monitor() {
		attempt=0
		if [ "$2" == "" ]; then
		    loops=-1;
		else
		    loops=$2;
		fi
		clear
		while [ $loops -ne $attempt ] ; do
			attempt=$(($attempt + 1))
			#clear
			tput cup 0 0

			# print_stats $attempt
			mecho "[cycle #: $attempt; if constant make sure server lives]"
			unixsock_cmd version
			unixsock_cmd uptime

			mecho "Transaction Statistics"
			unixsock_cmd get_statistics UAS_transactions
			unixsock_cmd get_statistics UAC_transactions
			unixsock_cmd get_statistics inuse_transactions

			mecho "Stateless Server Statistics"
			unixsock_cmd get_statistics sent_replies
			unixsock_cmd get_statistics sent_err_replies
			unixsock_cmd get_statistics received_ACKs

			mecho "UsrLoc Stats"
			unixsock_cmd get_statistics registered_users
			unixsock_cmd get_statistics location-users
			unixsock_cmd get_statistics location-contacts
			unixsock_cmd get_statistics location-expires

			if [ $loops -ne $attempt ] ; then
				sleep $WATCH_PERIOD
			fi
				
		done
		exit 0

}

KAMAILIO_MONITOR=unixsock_kamailio_monitor