62c0ac8f7c671da6fe8ccd811c3777eec69810db
[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 $remote_fs
28 # Required-Stop:     $remote_fs
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/openvswitch-monitor
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 default=/etc/default/openvswitch-monitor
59 if [ -f $default ] ; then
60     . $default
61 fi
62
63 set -e
64
65 running_pid() {
66 # Check if a given process pid's cmdline matches a given name
67     pid=$1
68     name=$2
69     [ -z "$pid" ] && return 1 
70     [ ! -d /proc/$pid ] &&  return 1
71     return 0
72 }
73
74 running() {
75 # Check if the process is running looking at /proc
76 # (works for all users)
77
78     # No pidfile, probably no daemon present
79     [ ! -f "$PIDFILE" ] && return 1
80     pid=`cat $PIDFILE`
81     running_pid $pid $DAEMON || return 1
82     return 0
83 }
84
85 start_daemon() {
86 # Start the process using the wrapper
87     if test $THRESHOLD != 0; then
88         start-stop-daemon --start --quiet -m --background --pidfile $PIDFILE \
89             --exec $DAEMON -- -c $THRESHOLD -i $INTERVAL -l $LOG_FILE \
90             -s $SWITCH_VCONN $DAEMON_OPTS 
91     fi
92
93     # Wait up to 3 seconds for the daemon to start.
94     for i in 1 2 3; do
95         if running; then
96             break
97         fi
98         sleep 1
99     done
100 }
101
102 stop_daemon() {
103     start-stop-daemon -o --stop --pidfile $PIDFILE
104     rm $PIDFILE
105 }
106
107 case "$1" in
108   start)
109         log_daemon_msg "Starting $DESC " "$NAME"
110         # Check if it's running first
111         if running ;  then
112             log_progress_msg "apparently already running"
113             log_end_msg 0
114             exit 0
115         fi
116         if start_daemon && running ;  then
117             # It's ok, the daemon started and is running
118             log_end_msg 0
119         else
120             # Either we could not start it or it is not running
121             # after we did
122             # NOTE: Some daemons might die some time after they start,
123             # this code does not try to detect this and might give
124             # a false positive (use 'status' for that)
125             log_end_msg 1
126         fi
127         ;;
128   stop)
129         log_daemon_msg "Stopping $DESC" "$NAME"
130         if running ; then
131             # Only stop the daemon if we see it running
132             stop_daemon
133             log_end_msg $?
134         else
135             # If it's not running don't do anything
136             log_progress_msg "apparently not running"
137             log_end_msg 0
138             exit 0
139         fi
140         ;;
141   restart|force-reload)
142         log_daemon_msg "Restarting $DESC" "$NAME"
143         if running ;  then
144             stop_daemon
145             # Wait some sensible amount, some daemons need this
146             [ -n "$DIETIME" ] && sleep $DIETIME
147         fi
148         start_daemon
149         running
150         log_end_msg $?
151         ;;
152   status)
153         log_daemon_msg "Checking status of $DESC" "$NAME"
154         if running ;  then
155             log_progress_msg "running"
156             log_end_msg 0
157         else
158             log_progress_msg "apparently not running"
159             log_end_msg 1
160             exit 1
161         fi
162         ;;
163   # Use this if the daemon cannot reload
164   reload)
165         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
166         log_warning_msg "cannot re-read the config file (use restart)."
167         ;;
168   *)
169         N=/etc/init.d/openvswitch-monitor
170         echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
171         exit 1
172         ;;
173 esac
174
175 exit 0