Code

-bin.pnp_gearman_worker.{default,init}: Init script for Gearman worker.
[pkg-pnp4nagios.git] / debian / pnp4nagios-bin.pnp_gearman_worker.init
1 #!/bin/sh
2 #
3 # init.d script for the Gearman worker daemon of PNP4Nagios
4 #
5 # Based on an example script for NPCD of Javier Fernandez-Sanguino
6 # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
7 # Copyright (c) 2011 Sebastian Harl <tokkee@debian.org>
8 #
9 ### BEGIN INIT INFO
10 # Provides:          pnp_gearman_worker
11 # Required-Start:    $network $local_fs $remote_fs
12 # Required-Stop:     $network $local_fs $remote_fs
13 # Should-Start:
14 # Should-Stop:
15 # Default-Start:     2 3 4 5
16 # Default-Stop:      0 1 6
17 # Short-Description: PNP4Nagios Gearman worker daemon
18 # Description:       Processing of Nagios' perf data in distributed setups
19 ### END INIT INFO
21 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23 DAEMON=/usr/lib/pnp4nagios/libexec/process_perfdata.pl
24 NAME=pnp_gearman_worker
25 DESC="PNP4Nagios Gearman worker daemon"
27 PIDFILE=/var/run/pnp4nagios/$NAME.pid
29 test -x $DAEMON || exit 0
31 . /lib/lsb/init-functions
33 # Default options, these can be overriden by the information
34 # at /etc/default/$NAME
35 DAEMON_OPTS=""
36 DIETIME=10
37 STARTTIME=2
38 USER=nagios
40 if [ -f /etc/default/pnp_gearman_worker ] ; then
41         . /etc/default/pnp_gearman_worker
42 fi
44 if [ "x$RUN" != "xyes" ] ; then
45         echo "$NAME has been disabled in /etc/default/pnp_gearman_worker."
46         exit 0
47 fi
49 set -e
51 # Check if a given process pid's cmdline matches a given name
52 running_pid() {
53         pid=$1
54         name=$2
55         [ -z "$pid" ] && return 1
56         [ ! -d /proc/$pid ] &&  return 1
57         cmd=`cat /proc/$pid/cmdline | tr "\000" "\n" | head -n 1 | cut -d : -f 1`
58         # Is this the expected server
59         [ "$cmd" != "$name" ] &&  return 1
60         return 0
61 }
63 # Check if the process is running looking at /proc
64 # (works for all users)
65 running() {
66         # No pidfile, probably no daemon present
67         [ ! -f "$PIDFILE" ] && return 1
68         pid=`cat $PIDFILE`
69         running_pid "$pid" $DAEMON \
70                 || running_pid "$pid" "/usr/bin/perl" || return 1
71         return 0
72 }
74 # Start the process using the wrapper
75 start_server() {
76         if ! perl -MGearman::Worker -e '1;' > /dev/null 2>&1 \
77                         || ! perl -MMIME::Base64 -e '1;' > /dev/null 2>&1 \
78                         || ! perl -MCrypt::Rijndael -e '1;' > /dev/null 2>&1; then
79                 echo "$NAME requires the Gearman::Worker, MIME::Base64 and" >&2
80                 echo "Crypt::Rijndael Perl modules. Make sure that the packages" >&2
81                 echo "perl, libgearman-client-perl and libcrypt-rijndael-perl" >&2
82                 echo "are installed." >&2
83                 exit 1
84         fi
86         if [ -d /var/run/pnp4nagios ]; then
87                 chmod 755 /var/run/pnp4nagios
88                 chown nagios.nagios /var/run/pnp4nagios
89         else
90                 install -d -m 755 -o nagios -g nagios /var/run/pnp4nagios
91         fi
93         start_opts="--start --quiet --oknodo --pidfile $PIDFILE"
94         if [ -n "$USER" ]; then
95                 start_opts="$start_opts --chuid $USER"
96         fi
97         start-stop-daemon $start_opts --exec $DAEMON -- \
98                 --pidfile=$PIDFILE --gearman --daemon $DAEMON_OPTS
99         errcode=$?
100         return $errcode
103 # Stop the process using the wrapper
104 stop_server() {
105         killproc -p $PIDFILE $DAEMON
106         errcode=$?
107         return $errcode
110 # Force the process to die killing it manually
111 force_stop() {
112         [ ! -e "$PIDFILE" ] && return
113         if running ; then
114                 kill -15 $pid
115                 # Is it really dead?
116                 sleep "$DIETIME"s
117                 if running ; then
118                         kill -9 $pid
119                         sleep "$DIETIME"s
120                         if running ; then
121                                 echo "Cannot kill $NAME (pid=$pid)!"
122                                 exit 1
123                         fi
124                 fi
125         fi
126         rm -f $PIDFILE
129 case "$1" in
130         start)
131                 log_daemon_msg "Starting $DESC" "$NAME"
132                 # Check if it's running first
133                 if running ;  then
134                         log_progress_msg "apparently already running"
135                         log_end_msg 0
136                         exit 0
137                 fi
138                 if start_server ; then
139                         # NOTE: Some servers might die some time after they start,
140                         # this code will detect this issue if STARTTIME is set
141                         # to a reasonable value
142                         [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time 
143                         if  running ;  then
144                                 # It's ok, the server started and is running
145                                 log_end_msg 0
146                         else
147                                 # It is not running after we did start
148                                 log_end_msg 1
149                         fi
150                 else
151                         # Either we could not start it
152                         log_end_msg 1
153                 fi
154                 ;;
155         stop)
156                 log_daemon_msg "Stopping $DESC" "$NAME"
157                 if running ; then
158                         # Only stop the server if we see it running
159                         errcode=0
160                         stop_server || errcode=$?
161                         log_end_msg $errcode
162                 else
163                         # If it's not running don't do anything
164                         log_progress_msg "apparently not running"
165                         log_end_msg 0
166                         exit 0
167                 fi
168                 ;;
169         force-stop)
170                 # First try to stop gracefully the program
171                 $0 stop
172                 if running; then
173                         # If it's still running try to kill it more forcefully
174                         log_daemon_msg "Stopping (force) $DESC" "$NAME"
175                         errcode=0
176                         force_stop || errcode=$?
177                         log_end_msg $errcode
178                 fi
179                 ;;
180         restart|force-reload)
181                 log_daemon_msg "Restarting $DESC" "$NAME"
182                 errcode=0
183                 stop_server || errcode=$?
184                 # Wait some sensible amount, some server need this
185                 [ -n "$DIETIME" ] && sleep $DIETIME
186                 start_server || errcode=$?
187                 [ -n "$STARTTIME" ] && sleep $STARTTIME
188                 running || errcode=$?
189                 log_end_msg $errcode
190                 ;;
191         status)
193                 log_daemon_msg "Checking status of $DESC" "$NAME"
194                 if running ;  then
195                         log_progress_msg "running"
196                         log_end_msg 0
197                 else
198                         log_progress_msg "apparently not running"
199                         log_end_msg 1
200                         exit 1
201                 fi
202                 ;;
203         *)
204                 N=/etc/init.d/$NAME
205                 echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
206                 exit 1
207                 ;;
208 esac
210 exit 0