#!/bin/sh

### BEGIN INIT INFO
# Provides: monasca-agent
# Short-Description: Start and start monasca-agent
# Description: monasca-agent is the monitoring Agent component OpenStack Monitoring
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO

. /lib/lsb/init-functions
PATH=$PATH:/sbin # add the location of start-stop-daemon on Debian
export PYTHONPATH=$PYTHONPATH:/usr/monasca/agent/

AGENTPATH="/usr/local/bin/monasca-collector"
AGENTCONF="/etc/monasca/agent/agent.conf"
MONASCASTATSDPATH="/usr/local/bin/monasca-statsd"
AGENTUSER="monasca-agent"
FORWARDERPATH="/usr/local/bin/monasca-forwarder"
NAME="monasca-agent"
DESC="Monasca Monitoring Agent"
AGENT_PID_PATH="/var/run/monasca-agent.pid"
SUPERVISOR_PIDFILE="/var/run/monasca-agent-supervisord.pid"
SUPERVISOR_FILE="/etc/monasca/agent/supervisor.conf"
SUPERVISOR_SOCK="/var/tmp/monasca-agent-supervisor.sock"
SUPERVISORD=$(which supervisord)

# This script is considered a configuration file and will not be
# removed by dpkg unless the --purge option it set. Therefore we
# make sure that the Agent is actually installed before we try to do anything:
if [ ! -x $AGENTPATH ]; then
    echo "$AGENTPATH not found. Exiting."
    exit 0
fi

check_status() {
    # If the socket exists, we can use supervisorctl
    if [ -e $SUPERVISOR_SOCK ]; then
        # If we're using supervisor, check the number of processes
        # supervisor is currently controlling, and make sure that it's the
        # same as the number of programs specified in the supervisor config
        # file:

        supervisor_processes=$(supervisorctl -c $SUPERVISOR_FILE status)
        supervisor_processes=$(echo "$supervisor_processes" |
                                       grep -v pup |
                                       grep $NAME |
                                       grep -c RUNNING)
        supervisor_config_programs=$(grep -v pup $SUPERVISOR_FILE |
                                     grep -c '\[program:')

        if [ "$supervisor_processes" -ne "$supervisor_config_programs" ]; then
            echo "$supervisor_processes"
            echo "$DESC (supervisor) is NOT running all child processes"
            return 1
        else
            echo "$DESC (supervisor) is running all child processes"
            return 0
        fi
    else
        echo "$DESC (supervisor) is not running"
        return 1
    fi
}

# Action to take
case "$1" in
    start)
        if [ ! -f $AGENTCONF ]; then
            echo "$AGENTCONF not found. Exiting."
            exit 3
        fi

        check_status > /dev/null
        if [ $? -eq 0 ]; then
            echo "$DESC is already running"
            exit 0
        fi

        su $AGENTUSER -c "$AGENTPATH configcheck" > /dev/null
        if [ $? -ne 0 ]; then
            log_daemon_msg "Invalid check configuration. Please run sudo /etc/init.d/monasca-agent configtest for more details."
            log_daemon_msg "Resuming starting process."
        fi

       
        log_daemon_msg "Starting $DESC (using supervisord)" "$NAME"
        start-stop-daemon --start --quiet --oknodo --exec $SUPERVISORD -- -c $SUPERVISOR_FILE --pidfile $SUPERVISOR_PIDFILE
        if [ $? -ne 0 ]; then
            log_end_msg 1
        fi
   
        # check if the agent is running once per second for 10 seconds
        retries=10
        while [ $retries -gt 1 ]; do
          if check_status > /dev/null; then
              # We've started up successfully. Exit cleanly
              log_end_msg 0
              exit 0
          else
              retries=$(($retries - 1))
              sleep 1
          fi
        done
        # After 10 tries the agent didn't start. Report an error
        log_end_msg 1
        check_status # report what went wrong
        $0 stop
        exit 1
        ;;
    stop)
       
        log_daemon_msg "Stopping $DESC (stopping supervisord)" "$NAME"
        start-stop-daemon --stop --retry 30 --quiet --oknodo --pidfile $SUPERVISOR_PIDFILE
   
        log_end_msg $?

        ;;

    info)
        shift # Shift 'info' out of args so we can pass any
              # addtional options to the real command
              # (right now only monasca-agent supports additional flags)
        su $AGENTUSER -c "$AGENTPATH info $@"
        COLLECTOR_RETURN=$?
        su $AGENTUSER -c "$MONASCASTATSDPATH info"
        MONASCASTATSD_RETURN=$?
        su $AGENTUSER -c "$FORWARDERPATH info"
        FORWARDER_RETURN=$?
        exit $(($COLLECTOR_RETURN+$MONASCASTATSD_RETURN+$FORWARDER_RETURN))
        ;;

    status)
        check_status
        ;;

    restart|force-reload)
        $0 stop
        $0 start
        ;;

    configcheck)
        su $AGENTUSER -c "$AGENTPATH configcheck"
        exit $?
        ;;

    configtest)
        su $AGENTUSER -c "$AGENTPATH configcheck"
        exit $?
        ;;

    jmx)
        shift
        su $AGENTUSER -c "$AGENTPATH jmx $@"
        exit $?
        ;;

    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|info|status|configcheck|configtest|jmx}"
        exit 1
        ;;
esac

exit $?
