#!/bin/bash
#
# @(#) Fibranet NSP, SL
# Copyright (C) 2005-2009 by Jordi Sanfeliu <admin@fibranet.cat>
#
#	/etc/rc.d/init.d/monitorix
#
# Starts Monitorix on RedHat/Fedora/CentOS Linux systems
#
# chkconfig: 2345 99 10
# description: lightweight system monitoring tool

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

RETVAL=0
LOCK="/var/lock/monitorix"
PROGRAM="Monitorix"

start() {
	# Check if monitorix is already running
	if [ -f $LOCK ] ; then
		echo "$PROGRAM is already running."
		return 0
	fi
	touch $LOCK
	echo -n $"Starting $PROGRAM: "
	# Creates RRDs files (if needed)
	/usr/sbin/monitorix.pl create
	if [ $? -gt 0 ]; then
		echo_failure
		echo
		return $RETVAL
	fi
	/usr/sbin/monitorix.pl init
	if [ $? -eq 0 ]; then
		echo_success
		echo
	else
		echo_failure
		echo
		return $RETVAL
	fi
}

stop() {
	if [ -f $LOCK ] ; then
		rm -f $LOCK
		/usr/sbin/monitorix.pl stop
		echo -n $"Stopping $PROGRAM: "
		echo_success
	else
		echo
		echo "WARNING: You has selected to stop Monitorix AGAIN!"
		echo "WARNING: This may destroy all your IP counters!."
		echo
		echo "If you only want to upgrade a new version, while mantaining Monitorix up"
		echo "and running, simply type 'stop' and then 'start' or 'restart'."
		echo
		echo -n "Do you still want to continue? (N/y): "
		read option
		if [ "$option" != "Y" ] && [ "$option" != "y" ] ; then
			return 1
		fi
		echo "In a near future in this case all your iptables counters will be deleted."
		echo "Stay tunned when this option changes."
		echo "At this moment you can use iptables command like this:"
		echo "iptables -X ; iptables -t nat -X"
	fi
	echo
}

status() {
        if [ -f $LOCK ] ; then
                echo $"$PROGRAM is running."
        else
                echo $"$PROGRAM is stopped."
        fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		stop
		sleep 1
		start
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart}"
		exit 1
esac

exit $RETVAL

