Imported Upstream version 2.6.1
[debian/amanda] / server-src / amtoc.pl
1 #!@PERL@ -w
2
3 # create a TOC (Table Of Content) file for an amanda dump
4
5 # Author: Nicolas.Mayencourt@cui.unige.ch
6
7 # release 3.1.4
8
9 # HISTORY
10 # 1.0 19??-??-?? nicolas@cui.unige.ch
11 #       don't remember :-)
12 # 2.0 1996-??-?? nicolas@cui.unige.ch
13 #       amanda 2.2.6 support
14 # 3.0 1999-02-17 Nicolas.Mayencourt@cui.unige.ch
15 #       major rewrite, incompatible with release 2.0, amanda 2.4 support
16 # 3.0.1 1999-02-17 oliva@dcc.unicamp.br
17 #       minor fixes for multi-tape runs
18 # 3.0.2 1999-02-28 martineau@IRO.UMontreal.CA
19 #       output the datestamp of each dump
20 # 3.0.3 1999-09-01 jrj@purdue.edu
21 #       allow multiple -s entries
22 # 3.0.4 1999-09-15 jrj@purdue.edu
23 #       handle an image failing on one tape...
24 # 3.1.0 1999-10-06 Nicolas.Mayencourt@cui.unige.ch
25 #       add new options (-i -t)
26 # 3.1.1 1999-10-08 Nicolas.Mayencourt@cui.unige.ch
27 #       print original size, instead of size-on-tape
28 # 3.1.2 1999-10-11 Nicolas.Mayencourt@cui.unige.ch
29 #       really print original size, instead of size-on-tape
30 # 3.1.3 Nicolas.Mayencourt@cui.unige.ch
31 #       correct a bug for total report
32 # 3.1.4 2000-01-14 dhw@whistle.com
33 #       Add a flag (-w) for vertical whitespace
34
35
36 #--------------------------------------------------------
37 sub pr($$$$$$$) { 
38 # you can update these proc if you want another formating
39 # format: filenumber  host:part  date  level  size
40 # If you use tabular option, modifie the format at the end of the code
41   if (defined($tabular)) {
42         $fnbr=$_[0];
43         $hstprt=$_[1] . ":" . $_[2];
44         $dt=$_[3];
45         $lvl=$_[4];
46         $sz=$_[5];
47         $ch=$_[6];
48     write($OF);
49   } else {
50     print $OF "$_[0]  $_[1]:$_[2]  $_[3]  $_[4]  $_[5]  $_[6]\n";
51   }
52 }
53 #--------------------------------------------------------
54
55
56
57 #--------------------------------------------------------
58 sub tfn($) {
59   # calculate tocfilename
60   $_ = $_[0];
61   foreach $s (@subs) {
62     eval $s;
63   }
64   return $dir . $_ ;
65 }
66 #--------------------------------------------------------
67
68
69 #--------------------------------------------------------
70 sub usage($) {
71   print STDERR "@_\n\n";
72   print STDERR "usage: amtoc [-a] [-i] [-t] [-f file] [-s subs] [-w] [--] logfile\n";
73   print STDERR "         -a      : file output to `label`.toc\n";
74   print STDERR "         -i      : Start TOC with a small help message\n";
75   print STDERR "         -t      : tabular output\n";
76   print STDERR "         -f file : output to file\n";
77   print STDERR "         -s subs : output file name evaluated to `eval \$subs`\n";
78   print STDERR "         -w      : add vertical whitespace after each tape\n";
79   print STDERR "         --      : last option\n";
80   print STDERR "         logfile : input file ('-' for stdin)\n";
81   exit;
82 }
83 #--------------------------------------------------------
84
85 #--------------------------------------------------------
86 sub init() {
87   &usage("amtoc required at least 'logfile' parameter.") if ($#ARGV==-1) ;
88
89   @subs = ();
90   for ($i=0;$i<=$#ARGV;$i++) {
91     if ($ARGV[$i] eq '-a') {
92         push (@subs, "s/\$/.toc/");
93       }
94     elsif ($ARGV[$i] eq '-i') {
95         $info=1;
96       }
97     elsif ($ARGV[$i] eq '-t') {
98         $tabular=1;
99       }
100     elsif ($ARGV[$i] eq '-f') {
101         $i++;
102         &usage("'-f' option require 'file' parameter.")  if ($i > $#ARGV);
103         $tocfilename=$ARGV[$i];
104       }
105     elsif ($ARGV[$i] eq '-s') {
106         $i++;
107         &usage("'-s' option require 'subs' parameter.")  if ($i > $#ARGV);
108         push (@subs, $ARGV[$i]);
109       }
110     elsif ($ARGV[$i] eq '-w') {
111         $vwspace=1;
112       }
113     elsif ($ARGV[$i] eq '--') {
114       # no more options: next arg == logfile
115         $i++;
116         &usage("amtoc required at least 'logfile' parameter.") if ($i > $#ARGV);
117         $logfile=$ARGV[$i];
118         &usage("too many parameters.") unless ($i == $#ARGV);
119       }
120     else {
121         $logfile=$ARGV[$i];
122         &usage("too many parameters.") unless ($i == $#ARGV);
123       }
124   }
125   &usage("amtoc required at least 'logfile' parameter.") unless ($logfile);
126 }
127
128 #--------------------------------------------------------
129
130 &init;
131
132 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
133 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin";
134
135 $dir=$logfile;
136 $dir =~ s/[^\/]*$//;
137
138
139 if ($logfile eq '-') {$IF=STDIN} else 
140   {die ("Cannot open logfile $logfile") unless open (IF,"$logfile");$IF=IF;}
141
142 $filenumber=0;
143 $tot_or_size=0;
144
145 while ( <$IF> ) {
146   $line = $_;
147   if ( /^FAIL dumper (\S+) (\S+)/ ) {
148     next;
149   }
150   if ( /^SUCCESS dumper (\S+) (\S+)/ ) {
151     $host = $1;
152     $disk = $2;
153     $line =~ /orig-kb (\d+)/;
154     $osize{$host}{$disk} = $1;
155     $tot_or_size += $osize{$host}{$disk};
156     $fail{$host}{$disk} = 0;
157     next;
158   }
159   if ( /^START amflush/ ) {
160     $flash_mode = 1;
161     next;
162   }
163   if ( ! /^([A-Z]+) taper (\S+) (\S+) (\S+) (\S+) (\S+)/) { next;}
164   # $_ = $1;
165   if (/PART taper/) {
166     /^([A-Z]+) taper (\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (\S+)/;
167     $filenum = $3;
168     $host = $4;
169     $disk = $5;
170     $date = $6;
171     $chunk = $7;
172     $level = $8;
173     if ($filenum != $filenumber) {
174       # This should not be possible */
175       $filenumber = $filenum;
176     }
177   } else {
178     /^([A-Z]+) taper (\S+) (\S+) (\S+) (\S+) (\S+)/;
179     $host = $2;
180     $disk = $3;
181     $date = $4;
182     $chunk = $5;
183     $level = $6;
184   }
185   switch: {
186     /START taper/ && do {
187       $tocfilename=&tfn($chunk) if ($#subs >= 0);
188       if (!$tocfilename || ($tocfilename eq '-')) {$OF=STDOUT;}
189       else {
190           die ("Cannot open tocfile $tocfilename") unless open(OF,">$tocfilename");
191           $OF=OF;
192         }
193
194         print $OF "\f" if ($vwspace && $filenumber);
195         if (defined($info)) {
196           print $OF "AMANDA: To restore:\n";
197           print $OF "    position tape at start of file and run:\n";
198           print $OF "        dd if=<tape> bs=32k skip=1 [ | zcat ] | restore -...f\n";
199           print $OF "    or run: amrestore -p <tape> [host [partition]] | restore -...f\n";
200           print $OF "\n";
201         }
202
203
204
205       $filenumber=0;
206       &pr("#","Server","/partition","date", "level","size[Kb]","part");
207       &pr("$filenumber","$chunk","","$disk","-","-","-");
208       last switch; };
209     /^(?:SUCCESS|CHUNK|PART|DONE) taper/ && do {
210       if(/SUCCESS/){
211         $level = $chunk;
212         $chunk = "-";
213       }
214       $filenum = $filenumber;
215       if (/DONE/) {
216         $chunk = "-";
217         $filenumber--;
218         $filenum = " ";
219       }
220       $mysize = 0;
221       if(/ kb (\d+) /){
222         $mysize = $1;
223       }
224       if ( $fail{$host}{$disk} ) {
225         &pr("$filenum","${host}","${disk}","${date}","${level}","FAIL","${chunk}");
226       } else {
227         if (defined($flash_mode)) {
228           &pr("$filenum","${host}","${disk}","${date}","${level}","$mysize","${chunk}");
229         } else {
230           if (defined($osize{$host}{$disk}) && !/^CHUNK/ && !/^PART/) {
231             &pr("$filenum","${host}","${disk}","${date}","${level}","$osize{$host}{$disk}","${chunk}");
232           } else {
233             $note = "";
234             if(!/^CHUNK/ && !/^PART/){
235                 # this case should never happend: 
236             } else {
237               $note = "*";
238             }
239             &pr("$filenum","${host}","${disk}","${date}","${level}","$note$mysize","${chunk}");
240           }
241         }
242       }
243       last switch;};
244     /INFO taper retrying/ && do {
245       --$filenumber;
246       last switch; };
247     /INFO taper tape .* \[OK\]/ && do {
248       $line =~ / kb (\d+) /;
249       $size = $1;
250       $line =~ / fm (\d+) /;
251       print "\n\n" if ($vwspace);
252       &pr("$1","total","on_tape","-","-","$size","-");
253       last switch; };
254     /FAIL taper/ && do { next; };
255   }
256   $filenumber += 1;
257 }
258 if (defined($flash_mode)) {
259   &pr("-","total","origin","-","not","available","-");
260 } else {
261   &pr("-","total","origin","-","-","$tot_or_size","-");
262 }
263 close $IF;
264 close OF;
265
266
267 format OF =
268 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @>> @>>>>>>>>
269 $fnbr,$hstprt,$dt,$lvl,$sz
270 .
271
272 format STDOUT =
273 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @>> @>>>>>>>> @>>>
274 $fnbr,$hstprt,$dt,$lvl,$sz,$ch
275 .