Imported Upstream version 2.5.1p3
[debian/amanda] / server-src / amtoc.pl.in
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   $host = $2;
166   $disk = $3;
167   $date = $4;
168   $chunk = $5;
169   $level = $6;
170   switch: {
171     /START taper/ && do {
172       $tocfilename=&tfn($chunk) if ($#subs >= 0);
173       if (!$tocfilename || ($tocfilename eq '-')) {$OF=STDOUT;}
174       else {
175           die ("Cannot open tocfile $tocfilename") unless open(OF,">$tocfilename");
176           $OF=OF;
177         }
178
179         print $OF "\f" if ($vwspace && $filenumber);
180         if (defined($info)) {
181           print $OF "AMANDA: To restore:\n";
182           print $OF "    position tape at start of file and run:\n";
183           print $OF "        dd if=<tape> bs=32k skip=1 [ | zcat ] | restore -...f\n";
184           print $OF "    or run: amrestore -p <tape> [host [partition]] | restore -...f\n";
185           print $OF "\n";
186         }
187
188
189
190       $filenumber=0;
191       &pr("#","Server","/partition","date", "level","size[Kb]","part");
192       &pr("$filenumber","$chunk","","$disk","-","-","-");
193       last switch; };
194     /^(?:SUCCESS|CHUNK) taper/ && do {
195       if(/SUCCESS/){
196         $level = $chunk;
197         $chunk = "-";
198       }
199       $mysize = 0;
200       if(/ kb (\d+) /){
201         $mysize = $1;
202       }
203       if ( $fail{$host}{$disk} ) {
204         &pr("$filenumber","${host}","${disk}","${date}","${level}","FAIL","${chunk}");
205       } else {
206         if (defined($flash_mode)) {
207           &pr("$filenumber","${host}","${disk}","${date}","${level}","$mysize","${chunk}");
208         } else {
209           if (defined($osize{$host}{$disk}) && !/^CHUNK/) {
210             &pr("$filenumber","${host}","${disk}","${date}","${level}","$osize{$host}{$disk}","${chunk}");
211           } else {
212             $note = "";
213             if(!/^CHUNK/){
214                 # this case should never happend: 
215             $strange=1;
216               $note = "*";
217             }
218             &pr("$filenumber","${host}","${disk}","${date}","${level}","$note$mysize","${chunk}");
219           }
220         }
221       }
222       last switch;};
223     /INFO taper retrying/ && do {
224       --$filenumber;
225       last switch; };
226     /INFO taper tape .* \[OK\]/ && do {
227       $line =~ / kb (\d+) /;
228       $size = $1;
229       $line =~ / fm (\d+) /;
230       print "\n\n" if ($vwspace);
231       &pr("$1","total","on_tape","-","-","$size","-");
232       if (defined($flash_mode)) {
233         &pr("$1","total","origin","-","not","available","-");
234       } else {
235         &pr("$1","total","origin","-","-","$tot_or_size","-");
236       }
237       if (defined($strange)) {
238         &pr("*","size","on_tape","-","-","-","-");
239       }
240       last switch; };
241     /FAIL taper/ && do { next; };
242   }
243   $filenumber += 1;
244 }
245 close $IF;
246 close OF;
247
248
249 format OF =
250 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>>
251 $fnbr,$hstprt,$dt,$lvl,$sz
252 .
253
254 format STDOUT =
255 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>> @>>>
256 $fnbr,$hstprt,$dt,$lvl,$sz,$ch
257 .