#!/bin/sh
#
# netatop	Startup script for the netatop kernel module and daemon
#
# chkconfig: - 11 89
# description:	Gather per-process statistics about network utilization
#
### BEGIN INIT INFO
# Provides:             netatop
# Required-Start:	$local_fs
# Required-Stop:	$local_fs
# Default-Start:	3 5
# Default-Stop:		0 1 2 6
# Short-Description: Gather per-process statistics about network utilization
# Description: Gather per-process statistics about network utilization
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

PIDFILE=/var/run/netatop.pid
LOCKFILE=/var/lock/subsys/netatop
RETVAL=0

load_mod()
{
	action "Loading netatop module:" modprobe -q netatop
}

unload_mod()
{
	action "Unloading netatop module:" modprobe -qr netatop
}

startd()
{
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- netatopd
	RETVAL=$?
	[ $RETVAL -eq 0 ] && pidof netatopd > "$PIDFILE"
	return $RETVAL
}

stopd()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- netatopd
	RETVAL=$?
	return $RETVAL
}

start()
{
	RETVAL=1
	load_mod && startd
	return $RETVAL
}

stop()
{
	stopd && unload_mod
	return $RETVAL
}

restart()
{
	stopd && startd
}

reload()
{
	stop && start
}

Status()
{
	grep -qs '^netatop' /proc/modules && echo "netatop module is loaded"
	status --pidfile "$PIDFILE" --expect-user root -- netatopd
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		Status
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
