3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 University of Maryland at College Park
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation, and that the name of U.M. not be used in advertising or
12 # publicity pertaining to distribution of the software without specific,
13 # written prior permission. U.M. makes no representations about the
14 # suitability of this software for any purpose. It is provided "as is"
15 # without express or implied warranty.
17 # U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 # Author: James da Silva, Systems Design and Analysis Group
25 # Computer Science Department
26 # University of Maryland at College Park
30 # amcleanup.sh - clean up and generate a report after a crash.
33 exec_prefix="@exec_prefix@"
35 amlibexecdir="@amlibexecdir@"
36 . "${amlibexecdir}/amanda-sh-lib.sh"
40 # add sbin and ucb dirs
41 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
44 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
45 if test "$USE_VERSION_SUFFIXES" = yes; then
53 # parse process tree and get the children of a given process ID
60 #500 20810 4938 0 10:21 pts/2 00:00:00 /bin/sh /usr/sbin/amdump tapebackup
62 for pid in $(ps -ef | awk "{if ( \$3 == $1 ) { print \$2 }}")
64 process_name=`ps -e|grep -w ${pid}|awk '{print $4}'`
65 echo `_ '%s: Process %s found running at pid #%s.' "amcleanup" "${process_name}" "${pid}"`
74 # send SIGTERM signal to kill given process ID and check if process still alive
75 # after receiving SIGTERM, if yes send SIGKILL
83 echo `_ '%s: Sending process %s the %s signal.' "$0" "${killPID}" "SIGTERM"`
84 `kill -${SIGTERM} ${killPID} 2>/dev/null`
85 for second in 0 1 2 3 4 ; do
86 pid_status=`ps -e|grep -w ${killPID}|grep -v grep |wc -l`
87 if [ ${pid_status} -ne 0 ] ; then
88 # process is still alive
95 echo `_ '%s: Sending process %s the %s signal.' "$0" "${killPID}" "SIGKILL"`
96 `kill -${SIGKILL} ${killPID} 2>/dev/null`
98 pid_status=`ps -e|grep -w ${killPID}|grep -v grep |wc -l`
99 if [ ${pid_status} -ne 0 ] ; then
110 while test $# -ge 2; do
115 echo `_ 'Usage: amcleanup [-k] [-v] conf'`
123 if test ! -d $confdir/$conf ; then
124 echo `_ '%s: could not cd into %s' "amcleanup" "$confdir/$conf"`
128 #check if amdump/amflush is running for given config
129 if test ${KILL_ENABLE} -eq 0 ; then
130 for am_process in amdump amflush ; do
131 am_pid=`ps -ef|grep -w ${am_process}|grep -w ${conf}|awk '{print $2}'`
132 if test ! -z "${am_pid}" ; then
133 echo `_ '%s: %s Process is running at PID %s for %s configuration.' "$0" "${am_process}" ${am_pid} ${conf}`
134 echo `_ '%s: Use -k option to stop all the process...' "$0"`
135 echo `_ 'Usage: amcleanup [-k] conf'`
143 logdir=`amgetconf$SUF $conf logdir "$@"`
145 if test $rc -ne 0 ; then
146 echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF logdir" "$rc"` 1>&2
150 errfile=$logdir/amdump
151 erramflush=$logdir/amflush
152 tapecycle=`amgetconf$SUF $conf tapecycle "$@"`
154 if test $rc -ne 0 ; then
155 echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF tapecycle" "$rc"` 1>&2
158 dumpuser=`amgetconf$SUF $conf dumpuser "$@"`
160 if test $rc -ne 0 ; then
161 echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF $conf dumpuser" "$rc"` 1>&2
164 if test ${KILL_ENABLE} -eq 1 ; then
166 #check if any one of the dumps are runing, if yes get the process tree and kill them
167 for am_process in amdump amflush ; do
169 am_pid=`ps -ef|grep -w ${am_process}|grep -w ${conf}|awk '{print $2}'`
170 #echo "checking children for ${am_pid}"
171 if test ! -z "${am_pid}" ; then
172 find_children ${am_pid}
178 while test ${#pidname[@]} -gt $i ; do
180 mypid=`ps -e|grep -w ${pidname[$i]}|grep -v grep|wc -l`
181 if [ ${mypid} -ne 0 ] ; then
182 killpid ${pidname[$i]}
184 if test $rc -ne 0 ; then
185 KILL_FAILURES=`expr ${KILL_FAILURES} + 1`
188 echo `_ '%s: Process %s no longer running. Skipping...' "$0" "${pidname[$i]}"`
193 if test ${#pidname[@]} -gt 0 ; then
194 echo `_ '%s: %s Amanda processes were found running.' "$0" "${#pidname[@]}"`
195 echo `_ '%s: %s processes failed to terminate.' "$0" "${KILL_FAILURES}"`
202 if test -f $logfile ; then
203 echo `_ '%s: processing outstanding log file.' "$0"`
204 exec </dev/null >/dev/null 2>&1
205 amreport$SUF $conf "$@"
207 if test $rc -ne 0 ; then
208 echo `_ '%s: "%s" exited with status: %s' "$0" "amreport" "$rc"` 1>&2
209 retstatus=`expr $retstatus + 1`
212 # Roll the log file to its datestamped name.
213 amlogroll$SUF $conf "$@"
215 if test $rc -ne 0 ; then
216 echo `_ '%s: "%s" exited with status: %s' "$0" "amlogroll" "$rc"` 1>&2
217 retstatus=`expr $retstatus + 2`
220 # Trim the index file to those for dumps that still exist.
221 amtrmidx$SUF $conf "$@"
223 if test $rc -ne 0 ; then
224 echo `_ '%s: "%s" exited with status: %s' "$0" "amtrmidx" "$rc"` 1>&2
225 retstatus=`expr $retstatus + 4`
229 echo `_ '%s: no unprocessed logfile to clean up.' "$0"`
232 if test -f $errfile ; then
233 # if log was found, this will have been directed to /dev/null,
235 echo `_ '%s: %s exists, renaming it.' "$0" "$errfile"`
237 # Keep debug log through the tapecycle plus a couple days
238 maxdays=`expr $tapecycle + 2`
241 # First, find out the last existing errfile,
242 # to avoid ``infinite'' loops if tapecycle is infinite
243 while test $days -lt $maxdays && test -f $errfile.$days ; do
244 days=`expr $days + 1`
246 # Now, renumber the existing log files
247 while test $days -ge 2 ; do
248 ndays=`expr $days - 1`
249 mv $errfile.$ndays $errfile.$days
252 mv $errfile $errfile.1
255 if test -f $erramflush ; then
256 # if log was found, this will have been directed to /dev/null,
258 echo `_ '%s: %s exists, renaming it.' "$0" "$erramflush"`
260 # Keep debug log through the tapecycle plus a couple days
261 maxdays=`expr $tapecycle + 2`
264 # First, find out the last existing erramflush,
265 # to avoid ``infinite'' loops if tapecycle is infinite
266 while test $days -lt $maxdays && test -f $erramflush.$days ; do
267 days=`expr $days + 1`
269 # Now, renumber the existing log files
270 while test $days -ge 2 ; do
271 ndays=`expr $days - 1`
272 mv $erramflush.$ndays $erramflush.$days
275 mv $erramflush $erramflush.1
279 if test $VERBOSE -eq 1; then
282 $amlibexecdir/amcleanupdisk $v $conf "$@"
284 if test $rc -ne 0 ; then
285 echo `_ '%s: "%s" exited with status: %s' "$0" "amcleanupdisk" "$rc"` 1>&2
286 retstatus=`expr $retstatus + 8`