debian: Bring Debian packaging in-line with new file locations
[sliver-openvswitch.git] / debian / openvswitch-monitor.init
1 #!/bin/sh 
2 #
3 # Example init.d script with LSB support.
4 #
5 # Please read this init.d carefully and modify the sections to
6 # adjust it to the program you want to run.
7 #
8 # Copyright (c) 2007, 2009 Javier Fernandez-Sanguino <jfs@debian.org>
9 #
10 # This is free software; you may redistribute it and/or modify
11 # it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2,
13 # or (at your option) any later version.
14 #
15 # This is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License with
21 # the Debian operating system, in /usr/share/common-licenses/GPL;  if
22 # not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
24 #
25 ### BEGIN INIT INFO
26 # Provides:          openvswitch-monitor
27 # Required-Start:    $network $local_fs
28 # Required-Stop:     
29 # Should-Start:      $named $syslog openvswitch-switch
30 # Should-Stop:       
31 # Default-Start:     2 3 4 5
32 # Default-Stop:      0 1 6
33 # Short-Description: Open vSwitch switch monitor
34 ### END INIT INFO
35
36 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
37
38 DAEMON=/usr/sbin/ovs-monitor
39 NAME=ovs-monitor
40 DESC="Open vSwitch switch monitor"
41
42 PIDFILE=/var/run/openvswitch/$NAME.pid 
43
44 test -x $DAEMON || exit 0
45
46 . /lib/lsb/init-functions
47
48 # Default options, these can be overriden by the information
49 # at /etc/default/$NAME
50 DAEMON_OPTS=""          # Additional options given to the daemon 
51
52 DODTIME=10              # Time to wait for the daemon to die, in seconds
53                         # If this value is set too low you might not
54                         # let some daemons to die gracefully and
55                         # 'restart' will not work
56                         
57 # Include defaults if available
58 if [ -f /etc/default/$NAME ] ; then
59     . /etc/default/$NAME
60 fi
61
62 set -e
63
64 running_pid() {
65 # Check if a given process pid's cmdline matches a given name
66     pid=$1
67     name=$2
68     [ -z "$pid" ] && return 1 
69     [ ! -d /proc/$pid ] &&  return 1
70     return 0
71 }
72
73 running() {
74 # Check if the process is running looking at /proc
75 # (works for all users)
76
77     # No pidfile, probably no daemon present
78     [ ! -f "$PIDFILE" ] && return 1
79     pid=`cat $PIDFILE`
80     running_pid $pid $DAEMON || return 1
81     return 0
82 }
83
84 start_daemon() {
85 # Start the process using the wrapper
86     if test $THRESHOLD != 0; then
87         start-stop-daemon --start --quiet -m --background --pidfile $PIDFILE \
88             --exec $DAEMON -- -c $THRESHOLD -i $INTERVAL -l $LOG_FILE \
89             -s $SWITCH_VCONN $DAEMON_OPTS 
90     fi
91
92     # Wait up to 3 seconds for the daemon to start.
93     for i in 1 2 3; do
94         if running; then
95             break
96         fi
97         sleep 1
98     done
99 }
100
101 stop_daemon() {
102     start-stop-daemon -o --stop --pidfile $PIDFILE
103     rm $PIDFILE
104 }
105
106 case "$1" in
107   start)
108         log_daemon_msg "Starting $DESC " "$NAME"
109         # Check if it's running first
110         if running ;  then
111             log_progress_msg "apparently already running"
112             log_end_msg 0
113             exit 0
114         fi
115         if start_daemon && running ;  then
116             # It's ok, the daemon started and is running
117             log_end_msg 0
118         else
119             # Either we could not start it or it is not running
120             # after we did
121             # NOTE: Some daemons might die some time after they start,
122             # this code does not try to detect this and might give
123             # a false positive (use 'status' for that)
124             log_end_msg 1
125         fi
126         ;;
127   stop)
128         log_daemon_msg "Stopping $DESC" "$NAME"
129         if running ; then
130             # Only stop the daemon if we see it running
131             stop_daemon
132             log_end_msg $?
133         else
134             # If it's not running don't do anything
135             log_progress_msg "apparently not running"
136             log_end_msg 0
137             exit 0
138         fi
139         ;;
140   restart|force-reload)
141         log_daemon_msg "Restarting $DESC" "$NAME"
142         if running ;  then
143             stop_daemon
144             # Wait some sensible amount, some daemons need this
145             [ -n "$DIETIME" ] && sleep $DIETIME
146         fi
147         start_daemon
148         running
149         log_end_msg $?
150         ;;
151   status)
152         log_daemon_msg "Checking status of $DESC" "$NAME"
153         if running ;  then
154             log_progress_msg "running"
155             log_end_msg 0
156         else
157             log_progress_msg "apparently not running"
158             log_end_msg 1
159             exit 1
160         fi
161         ;;
162   # Use this if the daemon cannot reload
163   reload)
164         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
165         log_warning_msg "cannot re-read the config file (use restart)."
166         ;;
167   *)
168         N=/etc/init.d/$NAME
169         echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
170         exit 1
171         ;;
172 esac
173
174 exit 0