#!/usr/bin/bash
# chkconfig: 2345 95 20
# description: sfacctd monitoring agent
# assures that sfacctd is running

function getDBPort() {
	local DBUSER=$1
	local DBPASS=$2
	local ret

	if [[ $DBPASS =~ ^$ ]]; then
		ret=$(mysql -A opcfg -u $DBUSER -e 'SELECT value FROM system_conf WHERE var="pmacctd_port"' 2>&-)
	else
		ret=$(mysql -A opcfg -u $DBUSER --password=$DBPASS -e 'SELECT value FROM system_conf WHERE var="pmacctd_port"' 2>&-)
	fi

	code=$?
	[ $code = 0 ] && echo $ret | sed 's/value //'

	return $code
}

function readDBUserPass() {
	if [ ! -f "$DB_CONF" ]; then
		ERROR="Database configuration file not found."
		return 1
	fi

	DBUSER=$(fgrep DBUSER $DB_CONF 2>&-)
	if [ $? !=  0 ]; then
		ERROR="User configuration not found."
		return 1
	fi

	DBUSER=$(echo $DBUSER | sed 's/.*="//;s/";.*//')

	DBPASS=$(fgrep DBPASS $DB_CONF 2>&-)
	if [ $? !=  0 ]; then
		ERROR="Password configuration not found."
		return 1
	fi

	DBPASS=$(echo $DBPASS | sed 's/.*="//;s/";.*//')

	return 0
}

function updateConfigFile() {
	readDBUserPass

	if [ $? != 0 ]; then
		return 1
	fi

	PORT=$(getDBPort $DBUSER $DBPASS)
	[ $? != 0 ] && ERROR='Failed to read information from the database.' && return 1

	sed -i "s/sfacctd_port:.*/sfacctd_port: $PORT/" $PMACCTD_CONF 2>/dev/null

	if [ $? != 0 ]; then
		return 1
	fi

	return 0
}

function running() {
	pgrep -l $1 1>&-
}

function stop () {
	local force=$1

	[ -z $force ] && force=0

	if [ $force = 1 ]; then
	    killall -INT $PNAME 2>&-
	else
		killall -9 $PNAME 2>&-
	fi
}

function start () {
    if [ -n "$INTERFACES" ]; then
    	for i in $INTERFACES; do
			ip link set $i up
			sudo -u apache $DAEMON -D -f $CONFDIR/sfacctd.$i.conf $DAEMON_OPTS 1>&-
		done
    else
    	sudo -u apache  $DAEMON -D -f $CONFDIR/sfacctd.conf $DAEMON_OPTS 1>&-
    fi
}

source /etc/rc.d/init.d/functions

DAEMON=/usr/sbin/sfacctd
NAME=$(basename $0)
DESC="OpMon Traffic Analyser"
CONFDIR=/etc/pmacct
PNAME=sfacctd
TRAFFICWRITERLOCK=/tmp/.traffic_writer

if [ ! -x $DAEMON ]; then
	ERROR="The file $DAEMON doesn't exist or isn't an executable."
	return 1
fi

[ -z $2 ] && UPDATE_CONF=update || UPDATE_CONF=$2


GREEN='\033[1;32m'
RED='\033[1;31m'
DEFAULT='\033[0m'
STATUS_POSITION='\r\033[70C'
OK="$STATUS_POSITION[$GREEN  OK$DEFAULT  ]"
FAIL="$STATUS_POSITION[$RED FAIL$DEFAULT ]"

DB_CONF=/usr/local/opmon/etc/db.php
PMACCTD_CONF=$CONFDIR/sfacctd.conf
ERROR="";

case "$1" in
	start)
		echo -n "Starting $DESC... "
		
		#remove lock 
		test -f $TRAFFICWRITERLOCK && rm -f $TRAFFICWRITERLOCK &> /dev/null
		
		running $PNAME
		if [ $? = 0 ]; then
			echo -e "$OK"
			echo "The $DESC is already running."
			exit 1
		fi

		start

		running $PNAME
		if [ $? != 0 ]; then
			echo -e "$FAIL"
			echo "The $DESC isn't starting."
			exit 1
		fi

		echo -e "$OK"
		;;
	stop)
		echo -n "Stopping $DESC... "

		stop
		sleep 1

		running $PNAME
		if [ $? = 0 ]; then
			echo -e "$FAIL"
			echo -n "Forcing stopping the $DESC... "

			stop 1
			sleep 1
			running $PNAME

			[ $? != 0 ] && echo -e "$OK" || echo -e "$FAIL"
			exit 0
		fi

		echo -e "$OK"
		exit 0
		;;
	restart)
		$0 stop $2
		$0 start $2
		;;
	status)
		running $PNAME
		[ $? = 0 ] && echo -e $DESC is$GREEN running$DEFAULT. || echo -e $DESC is$RED stopped$DEFAULT.
		;;
	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|status} [noupdate]" >&2
		exit 1
		;;
esac
