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