#!/bin/sh
#
# netconsole     Launches netconsole service.
#
# chkconfig: - 11 89
# description:  System administrator can use netconsole service to enable \
#               kernel messages logging to remote syslog-ng daemon.
# processname: -
# config: /etc/sysconfig/netconsole
# pidfile: -

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
. shell-error

[ -s /etc/sysconfig/netconsole ] || fatal "/etc/sysconfig/netconsole is missing"
. /etc/sysconfig/netconsole

RETVAL=0

start()
{
    msg_starting "netconsole"
    echo "7 4 1 7" > /proc/sys/kernel/printk
	sleep 5
    /sbin/modprobe netconsole netconsole=$SRCPORT@$SRCIP/$DEV,$TGTPORT@$TGTIP/$TGTMAC
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        success "netconsole startup"
    else
        failure "netconsole startup"
    fi
    echo
    return $RETVAL
}

stop()
{
    /sbin/modprobe -r netconsole
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        success "netconsole stop"
    else
        failure "netconsole stop"
    fi
    echo
    return $RETVAL

}

restart()
{
    stop
    start
}

reload()
{
    restart
} 

module_status="$(/bin/grep -c ^netconsole /proc/modules)"

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        restart
        ;;
    condstop)
	if [ "$module_status" = 1 ]; then
        stop
	fi
        ;;
    condrestart)
	if [ "$module_status" = 1 ]; then
        restart
	fi
        ;;
    condreload)
	if [ "$module_status" = 1 ]; then
        reload
	fi
        ;;
    status)
        if [ "$module_status" = 1 ]; then
            echo "netconsole is running"
            RETVAL=0
            exit $RETVAL
        fi
        echo "netconsole is stopped"
        RETVAL=3
        ;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
        RETVAL=1
esac

exit $RETVAL
