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 # log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
49 # log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
50 exit 0
51 fi
53 set -e
55 running_pid() {
56 # Check if a given process pid's cmdline matches a given name
57 pid=$1
58 name=$2
59 [ -z "$pid" ] && return 1
60 [ ! -d /proc/$pid ] && return 1
61 cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
62 # Is this the expected server
63 [ "$cmd" != "$name" ] && return 1
64 return 0
65 }
67 running() {
68 # Check if the process is running looking at /proc
69 # (works for all users)
71 # No pidfile, probably no daemon present
72 [ ! -f "$PIDFILE" ] && return 1
73 pid=`cat $PIDFILE`
74 running_pid $pid $DAEMON || return 1
75 return 0
76 }
78 start_server() {
79 # Start the process using the wrapper
80 start_daemon -p $PIDFILE $DAEMON $DAEMON_OPTS
81 errcode=$?
82 return $errcode
83 }
85 stop_server() {
86 # Stop the process using the wrapper
87 killproc -p $PIDFILE $DAEMON
88 errcode=$?
89 return $errcode
90 }
92 reload_server() {
93 [ ! -f "$PIDFILE" ] && return 1
94 pid=pidofproc $PIDFILE # This is the daemon's pid
95 # Send a SIGHUP
96 kill -1 $pid
97 return $?
98 }
100 force_stop() {
101 # Force the process to die killing it manually
102 [ ! -e "$PIDFILE" ] && return
103 if running ; then
104 kill -15 $pid
105 # Is it really dead?
106 sleep "$DIETIME"s
107 if running ; then
108 kill -9 $pid
109 sleep "$DIETIME"s
110 if running ; then
111 echo "Cannot kill $NAME (pid=$pid)!"
112 exit 1
113 fi
114 fi
115 fi
116 rm -f $PIDFILE
117 }
120 case "$1" in
121 start)
122 log_daemon_msg "Starting $DESC " "$NAME"
123 # Check if it's running first
124 if running ; then
125 log_progress_msg "apparently already running"
126 log_end_msg 0
127 exit 0
128 fi
129 if start_server ; then
130 # NOTE: Some servers might die some time after they start,
131 # this code will detect this issue if STARTTIME is set
132 # to a reasonable value
133 [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
134 if running ; then
135 # It's ok, the server started and is running
136 log_end_msg 0
137 else
138 # It is not running after we did start
139 log_end_msg 1
140 fi
141 else
142 # Either we could not start it
143 log_end_msg 1
144 fi
145 ;;
146 stop)
147 log_daemon_msg "Stopping $DESC" "$NAME"
148 if running ; then
149 # Only stop the server if we see it running
150 errcode=0
151 stop_server || errcode=$?
152 log_end_msg $errcode
153 else
154 # If it's not running don't do anything
155 log_progress_msg "apparently not running"
156 log_end_msg 0
157 exit 0
158 fi
159 ;;
160 force-stop)
161 # First try to stop gracefully the program
162 $0 stop
163 if running; then
164 # If it's still running try to kill it more forcefully
165 log_daemon_msg "Stopping (force) $DESC" "$NAME"
166 errcode=0
167 force_stop || errcode=$?
168 log_end_msg $errcode
169 fi
170 ;;
171 restart|force-reload)
172 log_daemon_msg "Restarting $DESC" "$NAME"
173 errcode=0
174 stop_server || errcode=$?
175 # Wait some sensible amount, some server need this
176 [ -n "$DIETIME" ] && sleep $DIETIME
177 start_server || errcode=$?
178 [ -n "$STARTTIME" ] && sleep $STARTTIME
179 running || errcode=$?
180 log_end_msg $errcode
181 ;;
182 status)
184 log_daemon_msg "Checking status of $DESC" "$NAME"
185 if running ; then
186 log_progress_msg "running"
187 log_end_msg 0
188 else
189 log_progress_msg "apparently not running"
190 log_end_msg 1
191 exit 1
192 fi
193 ;;
194 # Use this if the daemon cannot reload
195 reload)
196 log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
197 log_warning_msg "cannot re-read the config file (use restart)."
198 ;;
199 # And this if it cann
200 #reload)
201 #
202 # If the daemon can reload its config files on the fly
203 # for example by sending it SIGHUP, do it here.
204 #
205 # If the daemon responds to changes in its config file
206 # directly anyway, make this a do-nothing entry.
207 #
208 # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
209 # if running ; then
210 # reload_server
211 # if ! running ; then
212 # Process died after we tried to reload
213 # log_progress_msg "died on reload"
214 # log_end_msg 1
215 # exit 1
216 # fi
217 # else
218 # log_progress_msg "server is not running"
219 # log_end_msg 1
220 # exit 1
221 # fi
222 #;;
224 *)
225 N=/etc/init.d/$NAME
226 echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
227 exit 1
228 ;;
229 esac
231 exit 0