PageRenderTime 172ms CodeModel.GetById 165ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/rc3.d/S20ntop

http://github.com/brinkman83/bashrc
Shell | 146 lines | 112 code | 11 blank | 23 comment | 21 complexity | 2e1befcc986c105d30faf23b01377724 MD5 | raw file
  1#! /bin/sh
  2### BEGIN INIT INFO
  3# Provides:          ntop
  4# Required-Start:    $remote_fs $syslog
  5# Required-Stop:     $remote_fs $syslog
  6# Default-Start:     2 3 4 5
  7# Default-Stop:      0 1 6
  8### END INIT INFO
  9
 10DAEMON="/usr/sbin/ntop"
 11NAME="ntop"
 12DESC="network top daemon"
 13INIT="/etc/default/ntop"
 14HOMEDIR="/var/lib/ntop"
 15LOGDIR="/var/log/ntop"
 16
 17# Workaround for a rrd problem, see #471862.
 18export LANG=C
 19# end of workaround
 20
 21test -f $DAEMON || exit 0
 22
 23test -f $INIT || exit 0
 24
 25. $INIT
 26
 27sanity_check() {
 28# Sanity check, we expect USER And INTERFACES to be defined
 29# (we could also set defaults for them before calling INIT...)
 30	if [ -z "$USER" ] ; then
 31		echo -n "ERROR: Cannot start ntop since USER is not defined, check the configuration file $INIT" >&2
 32		return 1
 33	fi
 34	if [ -z "$INTERFACES" ] ; then
 35		echo "ERROR: Cannot start ntop since INTERFACES is not defined, check the configuration file $INIT" >&2
 36		return 1
 37	fi
 38	return 0
 39}
 40
 41check_log_dir() {
 42# Does the logging directory belong to the User running the application
 43        # If we cannot determine the logdir return without error
 44        # (we will not check it)
 45        [ -n "$LOGDIR" ] || return 0
 46        [ -n "$USER" ] || return 0
 47        if [ ! -e "$LOGDIR" ] ; then
 48                echo -n "ERR: logging directory $LOGDIR does not exist"
 49                return 1
 50        elif [ ! -d "$LOGDIR" ] ; then
 51                echo -n "ERR: logging directory $LOGDIR does not exist"
 52                return 1
 53        else
 54                real_log_user=`stat -c %U $LOGDIR`
 55        # An alternative way is to check if the the user can create
 56        # a file there...
 57                if [ "$real_log_user" != "$USER" ] ; then
 58                        echo -n "ERR: logging directory $LOGDIR does not belong
 59to the user $USER"
 60                        return 1
 61                fi
 62        fi
 63        return 0
 64}
 65
 66check_interfaces() {
 67# Check the interface status, abort with error if a configured one is not
 68# available
 69	[ -z "$INTERFACES" ] && return 0
 70	{ echo $INTERFACES | awk -F , '{ for(i=1;i<=NF;i++) print $i }' |
 71	while read iface ; do
 72		if ! ifconfig "$iface" | grep -w UP >/dev/null; then
 73			echo "ERR: interface $iface is DOWN..."
 74			return 1
 75		fi	
 76	done
 77	return 0
 78	} 
 79	return $?
 80}
 81
 82
 83
 84case "$1" in
 85start)
 86  if pidof $DAEMON > /dev/null ; then
 87     echo "Not starting $DESC, it has already been started."
 88     exit 0
 89  fi
 90  echo -n "Starting $DESC: "
 91  if ! sanity_check || ! check_log_dir || ! check_interfaces; then
 92  	echo " will not start $DESC!"
 93	exit 1
 94  fi
 95  start-stop-daemon --start --quiet --name $NAME --exec $DAEMON -- \
 96  -d -L -u $USER -P $HOMEDIR \
 97  --access-log-file=$LOGDIR/access.log -i "$INTERFACES" \
 98  -p /etc/ntop/protocol.list \
 99  -O $LOGDIR $GETOPT
100  if pidof $DAEMON > /dev/null ; then
101      echo ntop
102  else
103  # WARNING: This might introduce a race condition in some (fast) systems
104  # Wait for a sec an try again
105      sleep 1
106      if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
107      	echo ntop
108      else
109        echo "ntop not started. Read /usr/share/doc/ntop/README.Debian."
110	exit 1
111      fi
112  fi
113  ;;
114stop)
115  echo -n "Stopping $DESC: "
116  start-stop-daemon --stop --oknodo --name $NAME --user $USER --retry 9
117  if pidof $DAEMON > /dev/null ; then
118  # WARNING: This might introduce a race condition in some (fast) systems
119  # Wait for a sec an try again
120      sleep 1 
121      if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
122         echo "Ntop not stopped. Need to kill manually."
123	 exit 1
124      else 
125         echo ntop
126      fi
127  else
128      echo ntop
129  fi
130  ;;
131restart | force-reload)
132  $0 stop
133  sleep 2
134  $0 start
135  ;;
136reload)
137  echo "reload option not implemented"
138  exit 3
139  ;;
140*)
141  N=/etc/init.d/$NAME
142  echo "Usage: $N {start|stop|restart|force-reload|reload}" >&2
143  exit 1
144  ;;
145esac
146exit 0