#!/bin/bash
#
# groupmon      Script to start and stop the Onappstore control daemon.
#
# Author:       Julian Chesterfield <julian@onapp.com>
#
# chkconfig: 30 95 00
# description: Starts and stops the Onappstore control daemon.

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

MONPID_FILE=/var/run/groupmon.pid
LOCKFILE=/var/lock/subsys/groupmon
MONPROG=groupmon
INITSANNET=/usr/pythoncontroller/initSANnet
GROUPMON=/usr/bin/groupmon
BDEVCLIENT=/onappstore/sbin/bdevclient
ONAPPSTORECONF=/onappstore/onappstore.conf
MASTERONAPPSTORECONF=/.rw/onappstore.conf
MAX_NBD_DEVS=4096
DIRTY_BG_RATIO=5
DIRTY_RATIO=10
MIN_FREE_KBYTES=131072
XM=/usr/sbin/xm

stop()
{
    DB_RUNNING=`ps axw | grep "/usr/pythoncontroller/groupmon" | grep -v grep | wc -l`
    if [ $DB_RUNNING -gt 0 ]
    then
        echo -n "Stopping program $MONPROG"
        $GROUPMON -t
        sleep 2
        DB_RUNNING=`ps axw | grep "/usr/pythoncontroller/groupmon" | grep -v grep | wc -l`
        if [ $DB_RUNNING -gt 0 ]; then echo_failure; exit; else echo_success; fi
    fi

    echo -n "Stopping DBs"
    /etc/init.d/isd stop
    
    rm -rf /onappstore/DB/*
    umount /onappstore/DB &> /dev/null
    loopdev=`losetup -a | awk '/dbimage/{sub(/:/,"");print $1}'`
    if [ ! -z $loopdev ]; then
	losetup -d $loopdev
    fi
    rm -f /dbimage
    rm -f  $LOCKFILE /tmp/*lock /tmp/DS-* /tmp/VDISK-*
    rm -f /DB/NODE-*/*.txn
}

start()
{
    /usr/pythoncontroller/python /usr/pythoncontroller/diskutil.pyc -m
    if [ $? -ne 0 ]; then
	echo "in maintenance mode, not starting groupmon" >&2
        exit 1
    fi
    modprobe dm-mirror_sync
    modprobe nbd nbds_max=${MAX_NBD_DEVS}
    for i in `seq 0 ${MAX_NBD_DEVS}`; do
	if [ -e /sys/block/nbd${i}/queue/scheduler ]; then
	    echo deadline > /sys/block/nbd${i}/queue/scheduler
	fi
	if [ -e /sys/block/nbd${i}/queue/add_random ]; then
	    echo 0 > /sys/block/nbd${i}/queue/add_random
	fi
    done

    pushd /dev
    for i in `ls -d sd* 2>/dev/null`; do
	if [ -e /sys/block/${i}/queue/add_random ]; then
	    echo 0 > /sys/block/${i}/queue/add_random
	fi
    done
    popd

    if [ -f $LOCKFILE ]; then 
        echo -e "OnappstoreSAN already running (lockfile exists)"
        return; 
    fi

    if [ -e $MASTERONAPPSTORECONF ]; then
	cp -f $MASTERONAPPSTORECONF $ONAPPSTORECONF
    fi

    if [ -e $ONAPPSTORECONF ]; then
	#Source variables from onappstore.conf
	. $ONAPPSTORECONF
    fi

    /sbin/sysctl -w net.ipv4.tcp_reordering=127 &> /dev/null
    if [ -e $XM ]; then
	echo $DIRTY_BG_RATIO > /proc/sys/vm/dirty_background_ratio
	echo $DIRTY_RATIO > /proc/sys/vm/dirty_ratio
	echo $MIN_FREE_KBYTES > /proc/sys/vm/min_free_kbytes
    fi
    sleep 2

    #InfiniBand startup issue
    if [ -e /sys/class/net/ib0 ]; then
	sleep 30
    fi

    if [ ! -e /sys/class/net/onappstoresan ]; then
	$INITSANNET reset=true
    fi

    rm -f /onappstore/toolstackversion.txt && cp /onappstore/package-version.txt /onappstore/toolstackversion.txt
    touch $LOCKFILE
}

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

echo
