#!/bin/sh
#
### BEGIN INIT INFO
# Provides:       zabbix_agent2
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start:
# Default-Stop:
# Description:    Starts Zabbix_Agent2
### END INIT INFO

# zabbix_agent2	This script starts and stops zabix_agent2 service
#
# chkconfig: - 90 10
# description:	the zabbix network monitor agent2
# processname: zabbix_agent2
# config: /etc/zabbix/zabbix_agent2.conf
# pidfile: /var/run/zabbix/zabbix_agent2.pid


# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/zabbix/zabbix_agent2.pid
LOCKFILE=/var/lock/subsys/zabbix_agent2
RETVAL=0

start()
{
	check_rundir
	get_status >/dev/null
	if [ $RETVAL -eq 1 ]; then
		echo "Removing stale pidfile"
		rm -f "$PIDFILE"
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --background --expect-user zabbix -- zabbix_agent2
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user zabbix -- zabbix_agent2
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	sleep 2
	start
}

reload()
{
	msg_reloading zabbix_agent2
	stop_daemon --pidfile "$PIDFILE" --expect-user zabbix -HUP -- zabbix_agent2
	RETVAL=$?
	return $RETVAL
}

get_status()
{
	status --pidfile "$PIDFILE" --expect-user zabbix -- zabbix_agent2
	RETVAL=$?
}

check_rundir()
{
	if [ ! -d /var/run/zabbix ]; then
		mkdir -p -m 0775 /var/run/zabbix
		chown root:zabbix /var/run/zabbix
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		get_status
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
