#! /bin/sh
#
# ascservicemonitor     Script that keeps track of required ASC daemons
#

exec 2>&1
LOGFILENAME=/var/log/asc/services.log
LOGFILESIZE=$(stat -c%s "$LOGFILENAME")
CTIME=`date`

# wrap around logfiles > 5MiB
if [ $LOGFILESIZE -gt 5242880 ]; then
    echo "$CTIME :- Sanity check on required ASC services ---" > $LOGFILENAME
else
    echo "$CTIME :- Sanity check on required ASC services ---" >> $LOGFILENAME
fi;

# check webserver
RETVAL=0
/etc/init.d/lighttpd status 1>/dev/null 2>/dev/null
RETVAL=$?
if [ $RETVAL == 0 ]; then
    echo "lighttpd                         : OK"    >> $LOGFILENAME
elif [ $RETVAL == 3 ]; then
    echo "lighttpd                         : Stopped" >> $LOGFILENAME
    if [ -e "/etc/SuSE-release" ]; then
        # SUSE reports 3 for crash as well
        echo "Restarting lighttpd..."   >> $LOGFILENAME
        /etc/init.d/lighttpd stop 1> /dev/null 2> /dev/null
        /etc/init.d/lighttpd start 1> /dev/null 2> /dev/null
    fi;
elif [ $RETVAL == 2 ]; then
    echo "lighttpd                         : DEAD"  >> $LOGFILENAME
    echo "Restarting lighttpd..."   >> $LOGFILENAME
    /etc/init.d/lighttpd stop 1> /dev/null 2> /dev/null
    /etc/init.d/lighttpd start 1> /dev/null 2> /dev/null
fi;

# check policy service
RETVAL=0
/etc/init.d/ascpolicy status 1>/dev/null 2>/dev/null
RETVAL=$?
if [ $RETVAL != 0 ]; then
	if [ -f /var/lock/subsys/ascpolicy ]; then
        echo "ascpolicy                        : DEAD" >> $LOGFILENAME
        echo "Restarting ascpolicy..." >> $LOGFILENAME
		/etc/init.d/ascpolicy stop 1>/dev/null 2>/dev/null
		/etc/init.d/ascpolicy start 1>/dev/null 2>/dev/null
    else
        echo "ascpolicy                        : Stopped" >> $LOGFILENAME
	fi;
else 
    echo "ascpolicy                        : OK" >> $LOGFILENAME
fi;
