Code

Cleaned up pnp4nagios-bin.init.
[pkg-pnp4nagios.git] / debian / pnp4nagios-bin.init
1 #!/bin/sh
2 #
3 # init.d script for NPCD of PNP4Nagios
4 #
5 # Based on a example script of Javier Fernandez-Sanguino
6 # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
7 # Copyright (c) 2009 Sven Velt <sven@velt.de>
8 #
9 ### BEGIN INIT INFO
10 # Provides:          pnp4nagios-bin
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: Nagios Performance C Daemon
18 # Description:       Speed up processing of Nagios' performance data
19 ### END INIT INFO
21 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23 DAEMON=/usr/sbin/npcd
24 NAME=npcd
25 DESC="Nagios Performance C Daemon (PNP4Nagios)"
26 LOGDIR=/var/log/pnp4nagios
28 PIDFILE=/var/run/$NAME.pid
30 test -x $DAEMON || exit 0
32 . /lib/lsb/init-functions
34 # Default options, these can be overriden by the information
35 # at /etc/default/$NAME
36 DAEMON_OPTS=""
37 DIETIME=10
38 STARTTIME=2
39 LOGFILE=$LOGDIR/$NAME.log
41 # Include defaults if available
42 if [ -f /etc/default/pnp4nagios ] ; then
43         . /etc/default/pnp4nagios
44 fi
46 # Check if RUN is set to "yes" in /etc/defaults/pnp4nagios
47 if [ "x$RUN" != "xyes" ] ; then
48         echo "$NAME has been disabled in /etc/default/pnp4nagios."
49         exit 0
50 fi
52 set -e
54 # Check if a given process pid's cmdline matches a given name
55 running_pid() {
56         pid=$1
57         name=$2
58         [ -z "$pid" ] && return 1
59         [ ! -d /proc/$pid ] &&  return 1
60         cmd=`cat /proc/$pid/cmdline | tr "\000" "\n" | head -n 1 | cut -d : -f 1`
61         # Is this the expected server
62         [ "$cmd" != "$name" ] &&  return 1
63         return 0
64 }
66 # Check if the process is running looking at /proc
67 # (works for all users)
68 running() {
69         # No pidfile, probably no daemon present
70         [ ! -f "$PIDFILE" ] && return 1
71         pid=`cat $PIDFILE`
72         running_pid $pid $DAEMON || return 1
73         return 0
74 }
76 # Start the process using the wrapper
77 start_server() {
78         start_daemon -p $PIDFILE $DAEMON $DAEMON_OPTS
79         errcode=$?
80         return $errcode
81 }
83 # Stop the process using the wrapper
84 stop_server() {
85         killproc -p $PIDFILE $DAEMON
86         errcode=$?
87         return $errcode
88 }
90 reload_server() {
91         [ ! -f "$PIDFILE" ] && return 1
92         pid=pidofproc $PIDFILE # This is the daemon's pid
93         # Send a SIGHUP
94         kill -1 $pid
95         return $?
96 }
98 # Force the process to die killing it manually
99 force_stop() {
100         [ ! -e "$PIDFILE" ] && return
101         if running ; then
102                 kill -15 $pid
103                 # Is it really dead?
104                 sleep "$DIETIME"s
105                 if running ; then
106                         kill -9 $pid
107                         sleep "$DIETIME"s
108                         if running ; then
109                                 echo "Cannot kill $NAME (pid=$pid)!"
110                                 exit 1
111                         fi
112                 fi
113         fi
114         rm -f $PIDFILE
117 case "$1" in
118         start)
119                 log_daemon_msg "Starting $DESC " "$NAME"
120                 # Check if it's running first
121                 if running ;  then
122                         log_progress_msg "apparently already running"
123                         log_end_msg 0
124                         exit 0
125                 fi
126                 if start_server ; then
127                         # NOTE: Some servers might die some time after they start,
128                         # this code will detect this issue if STARTTIME is set
129                         # to a reasonable value
130                         [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time 
131                         if  running ;  then
132                                 # It's ok, the server started and is running
133                                 log_end_msg 0
134                         else
135                                 # It is not running after we did start
136                                 log_end_msg 1
137                         fi
138                 else
139                         # Either we could not start it
140                         log_end_msg 1
141                 fi
142                 ;;
143         stop)
144                 log_daemon_msg "Stopping $DESC" "$NAME"
145                 if running ; then
146                         # Only stop the server if we see it running
147                         errcode=0
148                         stop_server || errcode=$?
149                         log_end_msg $errcode
150                 else
151                         # If it's not running don't do anything
152                         log_progress_msg "apparently not running"
153                         log_end_msg 0
154                         exit 0
155                 fi
156                 ;;
157         force-stop)
158                 # First try to stop gracefully the program
159                 $0 stop
160                 if running; then
161                         # If it's still running try to kill it more forcefully
162                         log_daemon_msg "Stopping (force) $DESC" "$NAME"
163                         errcode=0
164                         force_stop || errcode=$?
165                         log_end_msg $errcode
166                 fi
167                 ;;
168         restart|force-reload)
169                 log_daemon_msg "Restarting $DESC" "$NAME"
170                 errcode=0
171                 stop_server || errcode=$?
172                 # Wait some sensible amount, some server need this
173                 [ -n "$DIETIME" ] && sleep $DIETIME
174                 start_server || errcode=$?
175                 [ -n "$STARTTIME" ] && sleep $STARTTIME
176                 running || errcode=$?
177                 log_end_msg $errcode
178                 ;;
179         status)
181                 log_daemon_msg "Checking status of $DESC" "$NAME"
182                 if running ;  then
183                         log_progress_msg "running"
184                         log_end_msg 0
185                 else
186                         log_progress_msg "apparently not running"
187                         log_end_msg 1
188                         exit 1
189                 fi
190                 ;;
191                 # Use this if the daemon cannot reload
192                 reload)
193                 log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
194                 log_warning_msg "cannot re-read the config file (use restart)."
195                 exit 1
196                 ;;
197         *)
198                 N=/etc/init.d/$NAME
199                 echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
200                 exit 1
201                 ;;
202 esac
204 exit 0