Merge branch 'master' of mathias-kettner.de:omd
[omd.git] / packages / pnp4nagios / skel / etc / init.d / npcd
blob1cf01d8477140548f69129160849e51848daa1ec
1 #!/bin/sh
3 ### BEGIN INIT INFO
4 # Provides: npcd
5 # Required-Start:
6 # Required-Stop:
7 # Default-Start: 2 3 4 5
8 # Default-Stop: 0 1 6
9 # Short-Description: pnp4nagios NPCD Daemon Version 0.6.5
10 # Description: Nagios Performance Data C Daemon
11 ### END INIT INFO
13 # chkconfig: 345 99 01
15 # File : npcd
18 cd ###ROOT###
19 . lib/omd/init_profile
20 . etc/omd/site.conf
22 servicename=npcd
23 prefix=###ROOT###
24 exec_prefix=###ROOT###
25 NpcdBin=###ROOT###/bin/npcd
26 NpcdCfgFile=###ROOT###/etc/pnp4nagios/npcd.cfg
27 NpcdVarDir=###ROOT###/var/pnp4nagios
28 NpcdRunFile=###ROOT###/tmp/pnp4nagios/run/npcd.pid
29 NpcdLockDir=###ROOT###/tmp/pnp4nagios/lock
30 NpcdLockFile=npcd
31 NpcdUser=###SITE###
32 NpcdGroup=###SITE###
34 status_npcd (){
35 pid_npcd
36 if ps -p $NpcdPID > /dev/null 2>&1; then
37 return 0
38 else
39 if test -f $NpcdLockDir/$NpcdLockFile; then
40 return 2
41 else
42 return 1
45 return 1
48 printstatus_npcd(){
49 if status_npcd $1 $2; then
50 echo "$servicename (pid $NpcdPID) is running..."
51 exit 0
52 elif test $? = 2; then
53 echo "$servicename is not running but subsystem locked"
54 exit 2
55 else
56 echo "$servicename is not running"
57 exit 1
62 killproc_npcd (){
63 kill $2 $NpcdPID
67 pid_npcd (){
68 if [ -f "$NpcdRunFile" ]; then
69 # Handle stale pidfiles
70 TMPPID=`head -n 1 $NpcdRunFile`
71 if ps -p $TMPPID >/dev/null 2>&1; then
72 NpcdPID=$TMPPID
73 return 0
77 # It might happen that there is no pidfile or a stale pidfile but
78 # a process is running. As fallback check the process table for the
79 # oldest process executed by the site user
80 PID=$(pgrep -u $OMD_SITE -o -f $NpcdBin)
81 if [ -n "$PID" ]; then
82 NpcdPID=$PID
83 return 0
85 return 1
89 # Source function library
90 # Solaris doesn't have an rc.d directory, so do a test first
91 if [ -f /etc/rc.d/init.d/functions ]; then
92 . /etc/rc.d/init.d/functions
93 elif [ -f /etc/init.d/functions ]; then
94 . /etc/init.d/functions
96 #### OMD ###
97 # Start only if PNP is enabled
98 . ###ROOT###/etc/omd/site.conf
99 if ( [ "$CONFIG_PNP4NAGIOS" != on ] && [ "$CONFIG_PNP4NAGIOS" != npcd ] ) ; then
100 exit 5
103 # Check that npcd exists.
104 if [ ! -f $NpcdBin ]; then
105 echo "Executable file $NpcdBin not found. Exiting."
106 exit 1
109 # Check that npcd.cfg exists.
110 if [ ! -f $NpcdCfgFile ]; then
111 echo "Configuration file $NpcdCfgFile not found. Exiting."
112 exit 1
115 # See how we were called.
116 __init_hook $0 $1 pre
117 case "$1" in
118 start)
119 status_npcd
120 if [ $? -eq 0 ]; then
121 echo "$servicename already started..."
122 exit 1
124 echo -n "Starting $servicename..."
125 touch $NpcdRunFile
126 chown $NpcdUser:$NpcdGroup $NpcdRunFile
127 $NpcdBin -d -f $NpcdCfgFile
128 if [ -d $NpcdLockDir ]; then touch $NpcdLockDir/$NpcdLockFile; fi
129 echo "OK"
130 __init_hook $0 $1 post
131 exit 0
134 stop)
135 status_npcd
136 if ! [ $? -eq 0 ]; then
137 echo "$servicename was not running... could not stop"
138 exit 0
140 echo -n "Stopping $servicename..."
142 pid_npcd
143 killproc_npcd npcd
145 # now we have to wait for npcd to exit and remove its
146 # own NpcdRunFile, otherwise a following "start" could
147 # happen, and then the exiting npcd will remove the
148 # new NpcdRunFile, allowing multiple npcd daemons
149 # to (sooner or later) run - John Sellens
150 # echo -n 'Waiting for npcd to exit .'
151 for i in 1 2 3 4 5 6 7 8 9 10 ; do
152 if status_npcd > /dev/null; then
153 echo -n '.'
154 sleep 1
155 else
156 break
158 done
159 if status_npcd > /dev/null; then
160 echo ''
161 echo 'Warning - $servicename did not exit in a timely manner'
162 else
163 echo 'OK'
165 rm -f $NpcdLockDir/$NpcdLockFile
166 __init_hook $0 $1 post
167 exit 0
170 status)
171 printstatus_npcd
174 restart|reload)
175 $0 stop
176 $0 start
180 echo "Usage: $servicename {start|stop|restart|reload|status}"
181 exit 1
184 esac
186 # End of this script