#!/bin/sh
#
# xymon client init.d script.
#
#----------------------------------------------------------------------------#
# This invokes xymonlaunch, which in turn runs the Xymon client and any      #
# extensions configured.                                                     #
#                                                                            #
# Copyright (C) 2005-2010 Henrik Storner <henrik@hswn.dk>                    #
#                                                                            #
# Integration of runclient.sh and xymon-client.initd by Japheth Cleaver 2011 #
# Rewrote init script for ALT Linux by Sergey Y. Afonin 2013                    #
#                                                                            #
# This program is released under the GNU General Public License (GPL),       #
# version 2. See the file "COPYING" for details.                             #
#                                                                            #
#----------------------------------------------------------------------------#
#
# chkconfig: - 80 20
# description: Xymon is a network monitoring tool that can monitor hosts \
#		and services. The client reports local system statistics \
#		(cpu, memory, disk, etc) to Xymon server.
#
# processname: xymonlaunch
# pidfile: /var/run/xymon/xymonlaunch.pid
# config: /etc/xymon-client/xymonclient.cfg
# config: /etc/xymon-client/clientlaunch.cfg autoreload
#
### BEGIN INIT INFO
# Provides: xymon-client
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Short-Description: start and stop the xymon client
# Description: Xymon is a network monitoring tool that can monitor hosts
#              and services. The client reports local system statistics
#              (cpu, memory, disk, etc) to Xymon server.
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

USER=xymon
DAEMON=xymonlaunch
RUNPATH=/usr/bin
LOCKFILE=/var/lock/subsys/$DAEMON
XYMONCLIENTHOME="/usr/share/xymon-client"
RETVAL=0

# Default settings for this client
MACHINEDOTS="`uname -n`"                         # This system's hostname
SERVEROSTYPE="`uname -s | tr '[A-Z/]' '[a-z_]'`" # This system's operating system in lowercase
CONFIGCLASS=""                                   # This system's config class

# Source config files
. /etc/sysconfig/xymon-client

# Check for overrides from /etc/sysconfig/xymon-config
[ -n "$CLIENTHOSTNAME" ]        && MACHINEDOTS="$CLIENTHOSTNAME"
[ -n "$CLIENTOS" ]              && SERVEROSTYPE="$CLIENTOS"
[ -n "$CLIENTCLASS" ]           && CONFIGCLASS="$CLIENTCLASS"

MACHINE="`echo $MACHINEDOTS | sed -e 's/\./,/g'`"
XYMONOSSCRIPT="xymonclient-${SERVEROSTYPE}.sh"
XYMONLAUNCHOPTS="$XYMONLAUNCHOPTS"

export MACHINE MACHINEDOTS SERVEROSTYPE XYMONOSSCRIPT XYMONLAUNCHOPTS XYMONCLIENTHOME CONFIGCLASS

# Values used in the remainder of the initscript
envfile="/etc/xymon-client/xymonclient.cfg"
configfile="/etc/xymon-client/clientlaunch.cfg"
logfile="/var/log/xymon/xymonlaunch.log"
pidfile="/var/run/xymon/xymonlaunch.pid"

# Check to make sure our pidfile's directory exists
rundir="`dirname $pidfile`"
[ -d "$rundir" ] || install -d -o "$USER" -g "$USER" "$rundir" || exit 1

start()
{
	start_daemon --pidfile "$pidfile" --lockfile "$LOCKFILE" --expect-user $USER --user $USER -- $RUNPATH/$DAEMON \
		$XYMONLAUNCHOPTS --env=$envfile --config=$configfile --log=$logfile --pidfile=$pidfile
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$pidfile" --lockfile "$LOCKFILE" --expect-user $USER -- $RUNPATH/$DAEMON
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading $DAEMON
	stop_daemon --pidfile "$pidfile" --expect-user $USER -HUP -- $RUNPATH/$DAEMON
	RETVAL=$?
	return $RETVAL
} 

# 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)
		status --pidfile "$pidfile" --expect-user $USER -- $RUNPATH/$DAEMON
		RETVAL=$?
		;;
	rotate)
		cat $rundir/*.pid 2>/dev/null | xargs -r kill -HUP
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status|rotate}"
		RETVAL=1
esac

exit $RETVAL
