166daced4bd37afd6904f7520c794659b8c4874f
[debian/amanda] / server-src / amcleanup.sh
1 #!@SHELL@
2 #
3 # Amanda, The Advanced Maryland Automatic Network Disk Archiver
4 # Copyright (c) 1991-1998 University of Maryland at College Park
5 # All Rights Reserved.
6 #
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.
16 #
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.
23 #
24 # Author: James da Silva, Systems Design and Analysis Group
25 #                          Computer Science Department
26 #                          University of Maryland at College Park
27 #
28
29 #
30 # amcleanup.sh - clean up and generate a report after a crash.
31
32 prefix="@prefix@"
33 exec_prefix="@exec_prefix@"
34 sbindir="@sbindir@"
35 amlibexecdir="@amlibexecdir@"
36 . "${amlibexecdir}/amanda-sh-lib.sh"
37
38 confdir=@CONFIG_DIR@
39
40 # add sbin and ucb dirs
41 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
42 export PATH
43
44 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
45 if test "$USE_VERSION_SUFFIXES" = yes; then
46         SUF=-@VERSION@
47 else
48         SUF=
49 fi
50
51 #
52 #Function to :
53 #   parse process tree and get the children of a given process ID
54 #
55
56
57 find_children() {
58
59 #sample ps -ef output
60 #500      20810  4938  0 10:21 pts/2    00:00:00 /bin/sh /usr/sbin/amdump tapebackup
61
62         for pid in $(ps -ef | awk "{if ( \$3 == $1 ) { print \$2 }}")
63         do
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}"`
66                 pidname[$i]=$pid
67                 i=`expr $i + 1`
68                 find_children $pid
69         done
70 }
71
72 #
73 #Function to :
74 #   send SIGTERM signal to kill given process ID and check if process still alive 
75 #   after receiving SIGTERM,  if yes send SIGKILL  
76 #
77
78 function killpid() {
79
80 killPID=$1
81 SIGTERM=15
82
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
89                         sleep 1
90                 else
91                         return 0
92                 fi
93         done
94         SIGKILL=9
95         echo `_ '%s: Sending process %s the %s signal.' "$0" "${killPID}" "SIGKILL"`
96         `kill -${SIGKILL} ${killPID} 2>/dev/null`
97         sleep 2
98         pid_status=`ps -e|grep -w ${killPID}|grep -v grep |wc -l`
99         if [ ${pid_status} -ne 0 ] ; then
100                 return 1
101         else
102                 return 0
103         fi
104 }
105
106
107 # process arguments
108 KILL_ENABLE=0
109 VERBOSE=0
110 while test $# -ge 2; do
111     case "$1" in
112         -k) KILL_ENABLE=1;;
113         -v) VERBOSE=1;;
114         *)
115             echo `_ 'Usage: amcleanup [-k] [-v] conf'`
116             exit 1 ;;
117     esac
118     shift
119 done
120 conf="$1"
121 shift
122
123 if test ! -d $confdir/$conf ; then
124         echo `_ '%s: could not cd into %s' "amcleanup" "$confdir/$conf"`
125         exit 1
126 fi
127
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'`
136                         exit 0
137                 fi
138         done
139 fi
140
141 cd $confdir/$conf
142
143 logdir=`amgetconf$SUF $conf logdir "$@"`
144 rc=$?
145 if test $rc -ne 0 ; then
146         echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF logdir" "$rc"` 1>&2
147         exit 1
148 fi
149 logfile=$logdir/log
150 errfile=$logdir/amdump
151 erramflush=$logdir/amflush
152 tapecycle=`amgetconf$SUF $conf tapecycle "$@"`
153 rc=$?
154 if test $rc -ne 0 ; then
155         echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF tapecycle" "$rc"` 1>&2
156         exit 1
157 fi
158 dumpuser=`amgetconf$SUF $conf dumpuser "$@"`
159 rc=$?
160 if test $rc -ne 0 ; then
161         echo `_ '%s: "%s" exited with status: %s' "amcleanup" "amgetconf$SUF $conf dumpuser" "$rc"` 1>&2
162         exit 1
163 fi
164 if test ${KILL_ENABLE} -eq 1 ; then
165
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
168                 unset pidname
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}
173                 fi
174
175                 KILL_FAILURES=0
176                 i=0
177
178                 while test ${#pidname[@]} -gt $i ; do
179
180                         mypid=`ps -e|grep -w ${pidname[$i]}|grep -v grep|wc -l`
181                         if [ ${mypid} -ne 0 ] ; then
182                                 killpid ${pidname[$i]}
183                                 rc=$?
184                                 if test $rc -ne 0 ; then
185                                         KILL_FAILURES=`expr ${KILL_FAILURES} + 1`
186                                 fi
187                         else
188                                 echo `_ '%s: Process %s no longer running.  Skipping...' "$0" "${pidname[$i]}"`
189                         fi
190                         i=`expr $i + 1`
191                 done
192
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}"`
196         fi
197
198         done
199 fi
200
201 retstatus=0
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 "$@"
206         rc=$?
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`
210         fi
211
212         # Roll the log file to its datestamped name.
213         amlogroll$SUF $conf "$@"
214         rc=$?
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`
218         fi
219
220         # Trim the index file to those for dumps that still exist.
221         amtrmidx$SUF $conf "$@"
222         rc=$?
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`
226         fi
227
228 else
229         echo `_ '%s: no unprocessed logfile to clean up.' "$0"`
230 fi
231
232 if test -f $errfile ; then
233     # if log was found, this will have been directed to /dev/null,
234     # which is fine.
235     echo `_ '%s: %s exists, renaming it.' "$0" "$errfile"`
236
237     # Keep debug log through the tapecycle plus a couple days
238     maxdays=`expr $tapecycle + 2`
239
240     days=1
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`
245     done
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
250         days=$ndays
251     done
252     mv $errfile $errfile.1
253 fi
254
255 if test -f $erramflush ; then
256     # if log was found, this will have been directed to /dev/null,
257     # which is fine.
258     echo `_ '%s: %s exists, renaming it.' "$0" "$erramflush"`
259
260     # Keep debug log through the tapecycle plus a couple days
261     maxdays=`expr $tapecycle + 2`
262
263     days=1
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`
268     done
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
273         days=$ndays
274     done
275     mv $erramflush $erramflush.1
276 fi
277
278 v=''
279 if test $VERBOSE -eq 1; then
280     v='-v'
281 fi
282 $amlibexecdir/amcleanupdisk $v $conf "$@"
283 rc=$?
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`
287 fi
288
289 exit $retstatus