pmdabind2: merge, review changes and adding some initial QA
[pcp.git] / src / pmmgr / rc_pmmgr
blob75289c6fce469b3dbce6a62f6a570ac28aeb1d22
1 #! /bin/sh
3 # Copyright (c) 2013-2015 Red Hat.
4 # Copyright (c) 2005 Silicon Graphics, Inc. All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2 of the License, or (at your
9 # option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # for more details.
16 # Start or Stop the Performance Co-Pilot (PCP) daemon manager
18 # The following is for chkconfig on RedHat based systems
19 # chkconfig: 2345 95 05
20 # description: pmmgr is a daemon manager for the Performance Co-Pilot (PCP)
22 # The following is for insserv(1) based systems,
23 # e.g. SuSE, where chkconfig is a perl script.
24 ### BEGIN INIT INFO
25 # Provides: pmmgr
26 # Required-Start: $remote_fs
27 # Should-Start: $local_fs $network $syslog $time $pmcd
28 # Required-Stop: $remote_fs
29 # Should-Stop: $local_fs $network $syslog $pmcd
30 # Default-Start: 2 3 4 5
31 # Default-Stop: 0 1 6
32 # Short-Description: Control pmmgr (daemon manager for PCP)
33 # Description: Configure and control pmmgr (a daemon manager for the Performance Co-Pilot)
34 ### END INIT INFO
36 . $PCP_DIR/etc/pcp.env
37 . $PCP_SHARE_DIR/lib/rc-proc.sh
39 PMMGR=$PCP_BINADM_DIR/pmmgr
40 PMMGROPTS=$PCP_PMMGROPTIONS_PATH
41 RUNDIR=$PCP_LOG_DIR/pmmgr
42 pmprog=$PCP_RC_DIR/pmmgr
43 prog=$PCP_RC_DIR/`basename $0`
45 tmp=`mktemp -d /var/tmp/pcp.XXXXXXXXX` || exit 1
46 status=1
47 trap "rm -rf $tmp; exit \$status" 0 1 2 3 15
49 if [ $pmprog = $prog ]
50 then
51 VERBOSE_CTL=on
52 else
53 VERBOSE_CTL=off
56 case "$PCP_PLATFORM"
58 mingw)
59 # nothing we can usefully do here, skip the test
64 # standard Unix/Linux style test
66 ID=id
67 test -f /usr/xpg4/bin/id && ID=/usr/xpg4/bin/id
69 IAM=`$ID -u 2>/dev/null`
70 if [ -z "$IAM" ]
71 then
72 # do it the hardway
74 IAM=`$ID | sed -e 's/.*uid=//' -e 's/(.*//'`
77 esac
79 _shutdown()
81 # Is pmmgr running?
83 _get_pids_by_name pmmgr >$tmp/tmp
84 if [ ! -s $tmp/tmp ]
85 then
86 [ "$1" = verbose ] && echo "$pmprog: pmmgr not running"
87 return 0
90 # Send pmmgr a SIGTERM, which is noted as a pending shutdown.
91 # When finished the currently active request, pmmgr will close any
92 # connections and then exit.
93 # Wait for pmmgr to terminate.
95 pmsignal -p -s TERM pmmgr > /dev/null 2>&1
96 $ECHO $PCP_ECHO_N "Waiting for pmmgr to terminate ...""$PCP_ECHO_C"
97 gone=0
98 for i in 1 2 3 4 5 6
100 sleep 3
101 _get_pids_by_name pmmgr >$tmp/tmp
102 if [ ! -s $tmp/tmp ]
103 then
104 gone=1
105 break
108 # If pmmgr doesn't go in 15 seconds, SIGKILL and sleep 1 more time
109 # to allow any clients reading from pmmgr sockets to fail so that
110 # socket doesn't end up in TIME_WAIT or somesuch.
112 if [ $i = 5 ]
113 then
114 $ECHO
115 echo "Process ..."
116 $PCP_PS_PROG $PCP_PS_ALL_FLAGS >$tmp/ps
117 sed 1q $tmp/ps
118 for pid in `cat $tmp/tmp`
120 $PCP_AWK_PROG <$tmp/ps "\$2 == $pid { print }"
121 done
122 echo "$prog: Warning: Forcing pmmgr to terminate!"
123 pmsignal -a -s KILL pmmgr > /dev/null 2>&1
124 else
125 $ECHO $PCP_ECHO_N ".""$PCP_ECHO_C"
127 done
128 if [ $gone != 1 ] # It just WON'T DIE, give up.
129 then
130 echo "Process ..."
131 cat $tmp/tmp
132 echo "$prog: Warning: pmmgr won't die!"
133 exit
135 $RC_STATUS -v
136 pmpost "stop pmmgr from $pmprog"
139 _usage()
141 echo "Usage: $pmprog [-v] {start|restart|condrestart|stop|status|reload|force-reload}"
144 while getopts v c
146 case $c
148 v) # force verbose
149 VERBOSE_CTL=on
153 _usage
154 exit 1
156 esac
157 done
158 shift `expr $OPTIND - 1`
160 if [ $VERBOSE_CTL = on ]
161 then # For a verbose startup and shutdown
162 ECHO=$PCP_ECHO_PROG
163 else # For a quiet startup and shutdown
164 ECHO=:
167 if [ "$IAM" != 0 -a "$1" != "status" ]
168 then
169 if [ -n "$PCP_DIR" ]
170 then
171 : running in a non-default installation, do not need to be root
172 else
173 echo "$prog:"'
174 Error: You must be root (uid 0) to start or stop the PCP pmmgr daemon.'
175 exit
179 # First reset status of this service
180 $RC_RESET
182 # Return values acc. to LSB for all commands but status:
183 # 0 - success
184 # 1 - misc error
185 # 2 - invalid or excess args
186 # 3 - unimplemented feature (e.g. reload)
187 # 4 - insufficient privilege
188 # 5 - program not installed
189 # 6 - program not configured
191 # Note that starting an already running service, stopping
192 # or restarting a not-running service as well as the restart
193 # with force-reload (in case signalling is not supported) are
194 # considered a success.
195 case "$1" in
197 'start'|'restart'|'condrestart'|'reload'|'force-reload')
198 if [ "$1" = "condrestart" ] && ! is_chkconfig_on pmmgr
199 then
200 status=0
201 exit
204 _shutdown quietly
206 # pmmgr messages should go to stderr, not the GUI notifiers
208 unset PCP_STDERR
210 if [ -x $PMMGR ]
211 then
212 if [ ! -f $PMMGROPTS ]
213 then
214 echo "$prog:"'
215 Error: pmmgr control file "$PMMGROPTS" is missing, cannot start pmmgr.'
216 exit
218 # create directory housing daemon pid file
219 if [ ! -d "$PCP_RUN_DIR" ]
220 then
221 mkdir -p -m 775 "$PCP_RUN_DIR"
222 chown $PCP_USER:$PCP_GROUP "$PCP_RUN_DIR"
224 # create directory which will serve as cwd
225 if [ ! -d "$RUNDIR" ]
226 then
227 mkdir -p -m 775 "$RUNDIR"
228 chown $PCP_USER:$PCP_GROUP "$RUNDIR"
230 cd $RUNDIR
232 # salvage the previous versions of any pmmgr
234 if [ -f pmmgr.log ]
235 then
236 rm -f pmmgr.log.prev
237 mv pmmgr.log pmmgr.log.prev
240 $ECHO $PCP_ECHO_N "Starting pmmgr ..." "$PCP_ECHO_C"
241 # options file processing ...
242 # only consider lines which start with a hyphen
243 # get rid of the -f option
244 # ensure multiple lines concat onto 1 line
245 OPTS=`sed <$PMMGROPTS 2>/dev/null \
246 -e '/^[^-]/d' \
247 -e 's/^/ /' \
248 -e 's/$/ /' \
249 -e 's/ -f / /g' \
250 -e 's/^ //' \
251 -e 's/ $//' \
252 | tr '\012' ' ' `
254 # environment stuff
256 eval `sed -e 's/"/\\"/g' $PMMGROPTS \
257 | awk -F= '
258 BEGIN { exports="" }
259 /^[A-Z]/ && NF == 2 { exports=exports" "$1
260 printf "%s=${%s:-\"%s\"}\n", $1, $1, $2
262 END { if (exports != "") print "export", exports }'`
264 $PMMGR -l pmmgr.log $OPTS &
265 $RC_STATUS -v
267 pmpost "start pmmgr from $pmprog"
269 # finally, stop here if running in a container
270 [ -z "$PCP_CONTAINER_IMAGE" ] || exec $PCP_BINADM_DIR/pmpause
272 status=0
275 'stop')
276 _shutdown
277 status=0
280 'status')
281 # NOTE: $RC_CHECKPROC returns LSB compliant status values.
282 $ECHO $PCP_ECHO_N "Checking for pmmgr:" "$PCP_ECHO_C"
283 if [ -r /etc/rc.status ]
284 then
285 # SuSE
286 $RC_CHECKPROC $PMMGR
287 $RC_STATUS -v
288 status=$?
289 else
290 # not SuSE
291 $RC_CHECKPROC $PMMGR
292 status=$?
293 if [ $status -eq 0 ]
294 then
295 $ECHO running
296 else
297 $ECHO stopped
303 [ -z "$PCP_CONTAINER_IMAGE" ] || exec "$@"
304 _usage
306 esac