#!/usr/bin/bash
#
# chkconfig: 345 99 01
# description: OpMon network monitor
#
# File : opmon
#

# Source function library.
if [ -e /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
elif [ -e /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
fi

prefix=/usr/local/opmon
exec_prefix=${prefix}
opmon_bin=${exec_prefix}/bin/opmon
opmon_cfg_file=${prefix}/etc/opmon.cfg
opmon_status_file=${prefix}/var/ramdisk/status.log
opmon_temp_file=${prefix}/var/opmon.tmp
opmon_retention_file=${prefix}/var/status.sav
opmon_command_file=${prefix}/var/rw/opmon.cmd
opmon_var_dir=${prefix}/var
opmon_run_file=${prefix}/var/opmon.lock
opmon_lock_dir=/var/lock/subsys
opmon_lock_file=${opmon_lock_dir}/opmon
opmon_user=opuser
opmon_starting_file=${prefix}/var/opmon_starting.lock

recreate_issue() {
	IP=$(/sbin/ifconfig 2>/dev/null | /bin/awk '/inet addr:/{ gsub("addr:", "", $2); if ($2 != "127.0.0.1") {print $2; exit} }')
	VERSION=$(/bin/cat /usr/local/opmon/db/version.sql | /bin/grep -i update | /bin/cut -d\' -f2 2>/dev/null)

	FILES="/etc/issue /etc/issue.net"
	for FILE in $FILES
	do
		if [ -z "$VERSION" ]; then
			echo OpMon >$FILE
		else
			echo OpMon release $VERSION >$FILE
            echo Welcome to OpMon r$VERSION >/etc/motd
		fi

		echo >>$FILE

		if [ -z $IP ]; then
			echo Unable to determine server ip address. >>$FILE
		else
			echo To manage this server use your browser to access URL >>$FILE
			echo http://$IP/ >>$FILE
		fi

		echo >>$FILE
    done

    echo -e "\nFor further information and help access:" >> /etc/motd
    echo -e "http://kb.opservices.com.br/" >> /etc/motd

    if [ -d '/var/lib/cloud/instance/' ]; then
        echo -e "\nTo know how to increase the system disk size, please access:" >>/etc/motd
        echo -e "http://kb.opservices.com.br/knowledge-base/root-partition-resize/" >>/etc/motd
    fi

}

reload_opmon() {
    echo -n "Reloading opmon: "
    if [ -e $opmon_run_file ]; then
    	PID=`cat $opmon_run_file`;
    	kill -HUP $PID >/dev/null 2>/dev/null
    else
	pkill -HUP -u ${opmon_user} -f ${opmon_bin}
    fi
    retval=$?
    echo -e " \e[32mdone.\033[0m"
}


is_opmon_running ()
{
	RUNNING=`ps -ef |grep "opmon -d" | grep -v grep | wc -l`
	if [ $RUNNING -eq 0 ]; then
		return 0
	fi

	return 1
}

is_opmon_initscript_running ()
{
	if [ ! -e $opmon_starting_file ]; then
		return 0
	fi

	timestampchange=`/usr/bin/stat -t $opmon_starting_file | /bin/cut -d" " -f13`
	datenow=`/bin/date +%s`
	elapsed_time=`expr $datenow - $timestampchange`
	if [ $elapsed_time -gt 300 ]; then
		/bin/rm -f $opmon_starting_file
		return 0
	fi

	return 1
}


killproc_opmon ()
{
	if [ ! -e $opmon_run_file ]; then
		return 1
	fi

	PID=`head -n 1 $opmon_run_file`
	RIGHTPID=`ps -p $PID 2>/dev/null|grep opmon |wc -l`
	if [ $RIGHTPID -eq 0 ]; then
		return 1;
	fi

	kill $PID
}

all_process ()
{
    s=(gearmand gearman-utils memcached mod-gearman-worker opdiscovery opmonconnector opmonstats opmon)

    if [ $1 = 'status' ]; then
        for i in "${s[@]}"; do
            service $i status
        done
        return 0
    fi
    # reverse daemon order to start/stop
    test $1 = 'start' || reverse=tac
    p=0
    for i in $(seq 0 $((${#s[@]}-1))|${reverse:-cat}); do
        i=${s[$i]}
        #check if using cluster
        if [ ! $(grep "127.0.0.1" /usr/local/opmon/etc/jobservers) ] && [ $i = 'gearmand' -o $i = 'opmon' ]; then
            echo "Cluster mode -> Skipping $i"
            continue
        fi
        #ask if want to stop gearmand
        if [ $i = 'gearmand' -a $1 = 'stop' ]; then
            read -p "Do you wish stop gearmand? (yn)" yn
            case $yn in
                [Nn]* ) continue ;;
                * ) echo "Please answer yes or no.";;
            esac
        fi
        echo "$p -> $i"
        service $i $1
        let p++
    done
}

case "$1" in

	start)
		echo -n "starting opmon network monitor... "

		# Copy naemon daemon to opmon to keep backward compatibility
		/bin/cp -afpv /usr/bin/naemon /usr/local/opmon/bin/opmon  >/dev/null 2>/dev/null

                # save status.sav before start
                /bin/cp -afpv /usr/local/opmon/var/status.sav /usr/local/opmon/status.sav-beforestart  >/dev/null 2>/dev/null


		# update server ISSUE based on version
		recreate_issue

		# restart opmonconnector to ensure connectivity after virtual IP has moved
		service opmonconnector restart >/dev/null 2>/dev/null

		# verify if opmon starting, script called more than onde but opmon not running yet
		is_opmon_initscript_running
		if [ $? = 1 ]; then
			echo -e "initscript is \e[32malready starting\033[0m opmon"
			exit 0
		fi

		# check if already running
		is_opmon_running
		if [ $? = 1 ]; then
			echo -e "opmon is \e[32malready running\033[0m"
			exit 0
		fi

		/bin/touch $opmon_starting_file

		# Checking if database has been created and created/update it if needed
		/usr/bin/php /usr/local/opmon/db/updatedb.php >/dev/null 2>/dev/null
		if [ $? -eq 1 ]; then
			/bin/rm -f $opmon_starting_file
			echo -e "\e[31merror updating opmon database\033[0m"
			exit 1
		fi

		rm -f $opmon_command_file
		rm -f $opmon_run_file
		su - $opmon_user -c "touch $opmon_var_dir/opmon.log $opmon_retention_file $opmon_run_file"

	        if [ -e "/usr/local/opmon/var/objects.precache" ]; then
			/bin/chown opuser.apache /usr/local/opmon/var/objects.precache
			/bin/chmod 665 /usr/local/opmon/var/objects.precache
			daemon --user=$opmon_user $opmon_bin -d -u $opmon_cfg_file
		else
			daemon --user=$opmon_user $opmon_bin -d $opmon_cfg_file
        	fi
		retval=$?
		echo

		if [ -d $opmon_lock_dir ]; then
			test $retval -eq 0 && touch $opmon_lock_file
		fi

		if [ -e "/usr/local/opmon/etc/op_event_guard_active" ]; then
			service eventguard start  >/dev/null 2>/dev/null &
		fi

		/bin/rm -f $opmon_starting_file
		exit $retval
		;;

	stop)
		echo -n "stopping opmon network monitor... "
                # save status.sav before stop
                /bin/cp -afpv /usr/local/opmon/var/status.sav /usr/local/opmon/status.sav-beforestop  >/dev/null 2>/dev/null

		is_opmon_initscript_running
		if [ $? -eq 1 ]; then
			echo -e "\e[33mopmon is going up\033[0m, unable to stop. try again later"
			exit 0
		fi

		is_opmon_running
		if [ $? -eq 0 ]; then
			echo -e "opmon is \e[32malready stopped\033[0m"
			exit 0
		fi

		if [ -e "/usr/local/opmon/etc/op_event_guard_active" ]; then
			service eventguard stop >/dev/null 2>&1
		fi

		killproc -d 5 ${opmon_bin}

		retval=$?
    		echo 
    		test $retval -eq 0 && rm -f $opmon_lock_file
                # clean old checkresults
                rm -f /usr/local/opmon/var/spool/checkresults/*
                rm -f $opmon_status_file
                rm -f $opmon_temp_file
                rm -f $opmon_run_file
                rm -f $opmon_lock_dir/$opmon_lock_file
                rm -f $opmon_command_file

                exit $retval

		;;

	status)
		is_opmon_initscript_running
		if [ $? -eq 1 ]; then
			echo -e "\e[32minit script is starting opmon\033[0m"
			exit 0
		fi

		is_opmon_running
		if [ $? -eq 1 ]; then
			echo -e "opmon is \e[32mrunning\033[0m"
			exit 0
		fi

		echo -e "opmon is \e[31mstopped\033[0m"
		exit 1
		;;

	restart)
		echo -n "running configuration check..."
		su - $opmon_user -c "$opmon_bin -p -v $opmon_cfg_file > /dev/null 2>&1"
		if [ $? -eq 0 ]; then
			echo -e "\e[32mdone\033[0m"
			$0 stop
			$0 start
		else
			echo -e "\e[31mrestart aborted\033[0m. please check your opmon configuration."
			exit 1
		fi
		;;
	stopall)
		echo "Stop all related daemons"
		all_process stop
		;;
	startall)
		echo "Start all related daemons"
		all_process start
		;;
	statusall)
		all_process status
		;;
	reload)
		$0 stop
		$0 start
		;;

	*)
		echo "Usage: opmon {start|stop|restart|status|stopall|startall|statusall|reload}"
		exit 0
		;;

esac

# End of this script
