Imported Upstream version 2.6.1
[debian/amanda] / changer-src / chg-manual.sh
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8
9 prefix=@prefix@
10 exec_prefix=@exec_prefix@
11 amlibexecdir=@amlibexecdir@
12 . ${amlibexecdir}/chg-lib.sh
13
14 #
15 #       Changer config file (changerfile)
16 #
17 #       resend_mail=900         # 15 minutes
18 #       timeout_mail=604800     # 7 days
19 #       request="tty"           # Use the tty to ask the user to change tape.
20 #                               # Can't be use by cron
21 #       request="email"         # Send an email to ask the user to change tape.
22 #       request="tty_email"     # Use the tty if it exist or send an email.
23 #                       #Default is "tty_email"
24 #       mtx_binary="/path/to/mtx" # path of 'mtx'; default is value discovered by
25 #                               # configure
26 #
27 #
28
29 if [ -d "@AMANDA_DBGDIR@" ]; then
30         logfile=@AMANDA_DBGDIR@/changer.debug
31 else
32         logfile=/dev/null
33 fi
34
35 myname=`basename $0`
36
37 EGREP='@EGREP@'
38
39 if ! error=`try_find_mt`; then
40     echo <none> $error
41     exit 2
42 fi
43
44 ONLINEREGEX="ONLINE|READY|sense[_ ]key[(]0x0[)]|sense key error = 0|^er=0$|, mt_erreg: 0x0|^Current Driver State: at rest$"
45 REPORTTO=`amgetconf mailto`
46 MAILER=`amgetconf mailer`
47 tape=`amgetconf tapedev`
48
49 if [ -z "$tape" ]; then
50   echo `_ '<none> tapedev not specified in amanda.conf.'`
51   exit 2
52 fi
53
54 ORG=`amgetconf ORG`
55
56 firstslot=1
57 lastslot=99
58 resend_mail=900         # 15 minutes
59 timeout_mail=604800     # 7 days
60 abort_file="chg-manual.abort"
61 abort_dir=`pwd`
62
63 changerfile=`amgetconf changerfile`
64
65 conf_match=`expr "$changerfile" : .\*\.conf\$`
66 if [ $conf_match -ge 6 ]; then
67         configfile=$changerfile
68         changerfile=`echo $changerfile | sed 's/.conf$//g'`
69 else
70         configfile=$changerfile.conf
71 fi
72
73 cleanfile=$changerfile-clean
74 accessfile=$changerfile-access
75 slotfile=$changerfile-slot
76 [ ! -f $cleanfile ] && echo 0 > $cleanfile
77 [ ! -f $accessfile ] && echo 0 > $accessfile
78 [ ! -f $slotfile ] && echo $firstslot > $slotfile
79 cleancount=`cat $cleanfile`
80 accesscount=`cat $accessfile`
81 slot=`cat $slotfile`
82
83 # define these functions early so that they can be overridden in changerfile.conf
84
85 request_tty() {
86         if > /dev/tty; then
87                 echo "$amdevcheck_message" >> /dev/tty
88                 # message parsed by ZMC:
89                 echo `_ 'Insert tape into slot %s and press return' "$1"` > /dev/tty
90                 echo `_ ' or type "NONE" to abort'` > /dev/tty
91                 read ANSWER < /dev/tty
92                 if [ X"$ANSWER" = X"NONE" ]; then
93                         echo `_ 'Aborting request'` > /dev/tty
94                         answer=`_ '<none> Aborting request'`
95                         echo `_ 'Exit ->'` $answer >> $logfile
96                         echo $answer
97                         exit 2
98                 fi
99         else
100                 answer=`_ '<none> no /dev/tty to ask to change tape'`
101                 echo `_ 'Exit ->'` $answer >> $logfile
102                 echo $answer
103                 exit 2
104         fi
105 }
106
107 ###
108 # If $changerfile exists, source it into this script.  One reason is to
109 # override the request() function above which gets called to request
110 # that a tape be mounted.  Here is an alternate versions of request()
111 # that does things more asynchronous:
112 #
113 request_email() {
114         # Send E-mail about the mount request and wait for the drive
115         # to go ready by checking the status once a minute.  Repeat
116         # the E-mail once an hour in case it gets lost.
117         timeout=0
118         gtimeout=$timeout_mail
119         rm -f $abort_filename
120         while true;do
121             if [ $gtimeout -le 0 ]; then
122                 answer=`_ '%s %s: timeout waiting for tape online' "$load" "$myname"`
123                 echo `_ 'Exit ->'` $answer >> $logfile
124                 echo $answer
125                 exit 2;
126             fi
127             if [ -f $abort_filename ]; then
128                 rm -f $abort_filename
129                 answer=`_ '<none> Aborting request'`
130                 echo `_ 'Exit ->'` $answer >> $logfile
131                 echo $answer
132                 exit 2
133             fi
134             if [ $timeout -le 0 ]; then
135                 msg=`_ '%s\nInsert Amanda tape into slot %s (%s)\nor \`touch %s\` to abort.' "$amdevcheck_message" "$1" "$tape" "$abort_filename"` 
136                 subject=`_ '%s AMANDA TAPE MOUNT REQUEST FOR SLOT %s' "$ORG" "$1"`
137                 echo "$msg" | $MAILER -s "$subject" $REPORTTO
138                 timeout=$resend_mail
139             fi
140             echo `_ '     -> status %s' "$tape"` >> $logfile
141             if amdevcheck_status $tape; then
142                 break
143             fi
144             sleep 60
145             timeout=`expr $timeout - 60`
146             gtimeout=`expr $gtimeout - 60`
147         done
148 }
149
150 request_tty_email() {
151         if > /dev/tty; then
152                 request_tty "$1"
153         else
154                 request_email "$1"
155         fi
156 }
157
158 request() {
159         if [ X"$request" = X"tty" ]; then
160                 request_tty "$1"
161         else if [ X"$request" = X"email" ]; then
162                 request_email "$1"
163         else
164                 request_tty_email "$1"
165         fi
166         fi
167 }
168
169 # source the changer configuration file (see description, top of file)
170 if [ -f $configfile ]; then
171         . $configfile
172 fi
173
174 # adjust MTX, if necessary
175 test -n "${mtx_binary}" && MTX="${mtx_binary}"
176
177 # check that MAILER is defined
178 if test -z "$MAILER"; then
179     if test x"$request" = x"email" || test x"$request" = x"tty-email"; then
180         answer=`_ "<none> %s: Can't send email because MAILER is not defined" "$myname"`
181         echo `_ 'Exit ->'` $answer >> $logfile
182         echo $answer
183         exit 2
184     fi
185 fi
186
187 #
188
189 eject() { 
190         echo `_ '     -> status %s' "$tape"` >> $logfile
191         if amdevcheck_status $tape; then
192             echo `_ '     -> offline %s' "$tape"` >> $logfile
193             try_eject_device $tape
194             answer="$slot $tape"
195             code=0
196         else
197             answer=`_ '<none> %s: %s' "$myname" "$amdevcheck_message"`
198             code=1
199         fi
200         echo `_ 'Exit ->'` $answer >> $logfile
201         echo $answer
202         exit $code
203 }
204
205 abort_filename="$abort_dir/$abort_file"
206
207 #
208
209 reset() {
210         echo `_ '     -> status %s' "$tape"` >> $logfile
211         if amdevcheck_status $tape; then
212                 answer="$slot $tape"
213         else
214                 answer="0 $tape $amdevcheck_message"
215         fi
216         echo `_ 'Exit ->'` $answer >> $logfile
217         echo $answer
218         exit 0
219 }
220
221 # load #
222
223 loadslot() {
224         echo `_ '     -> status %s' "$tape"` >> $logfile
225         # amdevcheck returns zero if the tape exists.
226         amdevcheck_status $tape;
227         tape_status=$?
228
229         whichslot=$1
230         case $whichslot in
231         current)
232                 load=$slot
233                 ;;
234         next|advance)
235                 load=`expr $slot + 1`
236                 [ $load -gt $lastslot ] && load=$firstslot
237                 ;;
238         prev)
239                 load=`expr $slot - 1`
240                 [ $load -lt $firstslot ] && load=$lastslot
241                 ;;
242         first)
243                 load=$firstslot
244                 ;;
245         last)
246                 load=$lastslot
247                 ;;
248         [0-9]|[0-9][0-9])
249                 if [ $1 -lt $firstslot -o $1 -gt $lastslot ]; then
250                         answer=`_ '<none> %s: slot must be %s .. %s' "$myname" "firstslot" "$lastslot"`
251                         echo `_ 'Exit ->'` $answer >> $logfile
252                         echo $answer
253                         exit 1
254                 fi
255                 load=$1
256                 ;;
257         *)
258                 answer=`_ '<none> %s: illegal slot: %s' "$myname" "$1"`
259                 echo `_ 'Exit ->'` $answer >> $logfile
260                 echo $answer
261                 exit 1
262                 ;;
263         esac
264         #
265         if [ $tape_status -eq 0 -a $load = $slot ];then
266                 # already loaded
267                 answer="$slot $tape"
268                 echo `_ 'Exit ->'` $answer >> $logfile
269                 echo $answer
270                 exit 0
271         fi
272
273         if [ X"$whichslot" = X"current" ]; then
274                 answer="<none> Current slot not loaded"
275                 echo `_ 'Exit ->'` $answer>> $logfile
276                 echo $answer
277                 exit 1
278         fi
279
280         expr $accesscount + 1 > $accessfile
281
282         if [ $tape_status -eq 0 ]; then
283                 echo `_ "     -> offline %s" "$tape"` >> $logfile
284                 try_eject_device $tape
285                 tape_status=1
286         fi
287         if [ $whichslot = advance ]; then
288                 tape=/dev/null
289         else
290                 echo `_ '     -> load   %s' "$load"` >> $logfile
291                 while true; do
292                         request $load
293                         echo `_ '     -> status %s' "$tape"` >> $logfile
294                         if amdevcheck_status $tape; then
295                             break;
296                         fi
297                 done
298         fi
299         echo $load > $slotfile
300         answer="$load $tape"
301         echo `_ 'Exit ->'` $answer >> $logfile
302         echo $answer
303         exit 0
304 }
305
306 #
307
308 info() {
309         echo `_ '     -> status %s' "$tape"` >> $logfile
310         if amdevcheck_status $tape; then
311                 answer="$slot $lastslot 1"
312         else
313                 answer="0 $lastslot 1"
314         fi
315         echo `_ 'Exit ->'` $answer >> $logfile
316         echo $answer
317         exit 0
318 }
319
320 #
321 # main part
322 #
323
324 echo `gettext "args ->"` "$@" >> $logfile
325 while [ $# -ge 1 ];do
326         case $1 in
327         -slot)
328                 shift
329                 loadslot $*
330                 ;;
331         -info)
332                 shift
333                 info
334                 ;;
335         -reset)
336                 shift
337                 reset
338                 ;;
339         -eject)
340                 shift
341                 eject
342                 ;;
343         *)
344                 fmt=`gettext "<none> %s: Unknown option %s\n"`
345                 printf $fmt $myname $1
346                 exit 2
347                 ;;
348         esac
349 done
350
351 exit 0