/rc6.d/K20ntop
http://github.com/brinkman83/bashrc · Shell · 146 lines · 112 code · 11 blank · 23 comment · 21 complexity · 2e1befcc986c105d30faf23b01377724 MD5 · raw file
- #! /bin/sh
- ### BEGIN INIT INFO
- # Provides: ntop
- # Required-Start: $remote_fs $syslog
- # Required-Stop: $remote_fs $syslog
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- ### END INIT INFO
- DAEMON="/usr/sbin/ntop"
- NAME="ntop"
- DESC="network top daemon"
- INIT="/etc/default/ntop"
- HOMEDIR="/var/lib/ntop"
- LOGDIR="/var/log/ntop"
- # Workaround for a rrd problem, see #471862.
- export LANG=C
- # end of workaround
- test -f $DAEMON || exit 0
- test -f $INIT || exit 0
- . $INIT
- sanity_check() {
- # Sanity check, we expect USER And INTERFACES to be defined
- # (we could also set defaults for them before calling INIT...)
- if [ -z "$USER" ] ; then
- echo -n "ERROR: Cannot start ntop since USER is not defined, check the configuration file $INIT" >&2
- return 1
- fi
- if [ -z "$INTERFACES" ] ; then
- echo "ERROR: Cannot start ntop since INTERFACES is not defined, check the configuration file $INIT" >&2
- return 1
- fi
- return 0
- }
- check_log_dir() {
- # Does the logging directory belong to the User running the application
- # If we cannot determine the logdir return without error
- # (we will not check it)
- [ -n "$LOGDIR" ] || return 0
- [ -n "$USER" ] || return 0
- if [ ! -e "$LOGDIR" ] ; then
- echo -n "ERR: logging directory $LOGDIR does not exist"
- return 1
- elif [ ! -d "$LOGDIR" ] ; then
- echo -n "ERR: logging directory $LOGDIR does not exist"
- return 1
- else
- real_log_user=`stat -c %U $LOGDIR`
- # An alternative way is to check if the the user can create
- # a file there...
- if [ "$real_log_user" != "$USER" ] ; then
- echo -n "ERR: logging directory $LOGDIR does not belong
- to the user $USER"
- return 1
- fi
- fi
- return 0
- }
- check_interfaces() {
- # Check the interface status, abort with error if a configured one is not
- # available
- [ -z "$INTERFACES" ] && return 0
- { echo $INTERFACES | awk -F , '{ for(i=1;i<=NF;i++) print $i }' |
- while read iface ; do
- if ! ifconfig "$iface" | grep -w UP >/dev/null; then
- echo "ERR: interface $iface is DOWN..."
- return 1
- fi
- done
- return 0
- }
- return $?
- }
- case "$1" in
- start)
- if pidof $DAEMON > /dev/null ; then
- echo "Not starting $DESC, it has already been started."
- exit 0
- fi
- echo -n "Starting $DESC: "
- if ! sanity_check || ! check_log_dir || ! check_interfaces; then
- echo " will not start $DESC!"
- exit 1
- fi
- start-stop-daemon --start --quiet --name $NAME --exec $DAEMON -- \
- -d -L -u $USER -P $HOMEDIR \
- --access-log-file=$LOGDIR/access.log -i "$INTERFACES" \
- -p /etc/ntop/protocol.list \
- -O $LOGDIR $GETOPT
- if pidof $DAEMON > /dev/null ; then
- echo ntop
- else
- # WARNING: This might introduce a race condition in some (fast) systems
- # Wait for a sec an try again
- sleep 1
- if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
- echo ntop
- else
- echo "ntop not started. Read /usr/share/doc/ntop/README.Debian."
- exit 1
- fi
- fi
- ;;
- stop)
- echo -n "Stopping $DESC: "
- start-stop-daemon --stop --oknodo --name $NAME --user $USER --retry 9
- if pidof $DAEMON > /dev/null ; then
- # WARNING: This might introduce a race condition in some (fast) systems
- # Wait for a sec an try again
- sleep 1
- if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
- echo "Ntop not stopped. Need to kill manually."
- exit 1
- else
- echo ntop
- fi
- else
- echo ntop
- fi
- ;;
- restart | force-reload)
- $0 stop
- sleep 2
- $0 start
- ;;
- reload)
- echo "reload option not implemented"
- exit 3
- ;;
- *)
- N=/etc/init.d/$NAME
- echo "Usage: $N {start|stop|restart|force-reload|reload}" >&2
- exit 1
- ;;
- esac
- exit 0