File: init

package info (click to toggle)
ntop 3:3.3-11
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,772 kB
  • ctags: 7,534
  • sloc: ansic: 71,427; sh: 16,772; awk: 1,504; perl: 792; makefile: 782; php: 123; python: 23; sql: 13; sed: 11
file content (146 lines) | stat: -rw-r--r-- 3,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /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