Imported Upstream version 2.5.1p1
[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 $dir=$logfile;
133 $dir =~ s/[^\/]*$//;
134
135
136 if ($logfile eq '-') {$IF=STDIN} else 
137   {die ("Cannot open logfile $logfile") unless open (IF,"$logfile");$IF=IF;}
138
139 $filenumber=0;
140 $tot_or_size=0;
141
142 while ( <$IF> ) {
143   $line = $_;
144   if ( /^FAIL dumper (\S+) (\S+)/ ) {
145     next;
146   }
147   if ( /^SUCCESS dumper (\S+) (\S+)/ ) {
148     $host = $1;
149     $disk = $2;
150     $line =~ /orig-kb (\d+)/;
151     $osize{$host}{$disk} = $1;
152     $tot_or_size += $osize{$host}{$disk};
153     $fail{$host}{$disk} = 0;
154     next;
155   }
156   if ( /^START amflush/ ) {
157     $flash_mode = 1;
158     next;
159   }
160   if ( ! /^([A-Z]+) taper (\S+) (\S+) (\S+) (\S+) (\S+)/) { next;}
161   # $_ = $1;
162   $host = $2;
163   $disk = $3;
164   $date = $4;
165   $chunk = $5;
166   $level = $6;
167   switch: {
168     /START taper/ && do {
169       $tocfilename=&tfn($chunk) if ($#subs >= 0);
170       if (!$tocfilename || ($tocfilename eq '-')) {$OF=STDOUT;}
171       else {
172           die ("Cannot open tocfile $tocfilename") unless open(OF,">$tocfilename");
173           $OF=OF;
174         }
175
176         print $OF "\f" if ($vwspace && $filenumber);
177         if (defined($info)) {
178           print $OF "AMANDA: To restore:\n";
179           print $OF "    position tape at start of file and run:\n";
180           print $OF "        dd if=<tape> bs=32k skip=1 [ | zcat ] | restore -...f\n";
181           print $OF "    or run: amrestore -p <tape> [host [partition]] | restore -...f\n";
182           print $OF "\n";
183         }
184
185
186
187       $filenumber=0;
188       &pr("#","Server","/partition","date", "level","size[Kb]","part");
189       &pr("$filenumber","$chunk","","$disk","-","-","-");
190       last switch; };
191     /^(?:SUCCESS|CHUNK) taper/ && do {
192       if(/SUCCESS/){
193         $level = $chunk;
194         $chunk = "-";
195       }
196       $mysize = 0;
197       if(/ kb (\d+) /){
198         $mysize = $1;
199       }
200       if ( $fail{$host}{$disk} ) {
201         &pr("$filenumber","${host}","${disk}","${date}","${level}","FAIL","${chunk}");
202       } else {
203         if (defined($flash_mode)) {
204           &pr("$filenumber","${host}","${disk}","${date}","${level}","$mysize","${chunk}");
205         } else {
206           if (defined($osize{$host}{$disk}) && !/^CHUNK/) {
207             &pr("$filenumber","${host}","${disk}","${date}","${level}","$osize{$host}{$disk}","${chunk}");
208           } else {
209             $note = "";
210             if(!/^CHUNK/){
211                 # this case should never happend: 
212             $strange=1;
213               $note = "*";
214             }
215             &pr("$filenumber","${host}","${disk}","${date}","${level}","$note$mysize","${chunk}");
216           }
217         }
218       }
219       last switch;};
220     /INFO taper retrying/ && do {
221       --$filenumber;
222       last switch; };
223     /INFO taper tape .* \[OK\]/ && do {
224       $line =~ / kb (\d+) /;
225       $size = $1;
226       $line =~ / fm (\d+) /;
227       print "\n\n" if ($vwspace);
228       &pr("$1","total","on_tape","-","-","$size","-");
229       if (defined($flash_mode)) {
230         &pr("$1","total","origin","-","not","available","-");
231       } else {
232         &pr("$1","total","origin","-","-","$tot_or_size","-");
233       }
234       if (defined($strange)) {
235         &pr("*","size","on_tape","-","-","-","-");
236       }
237       last switch; };
238     /FAIL taper/ && do { next; };
239   }
240   $filenumber += 1;
241 }
242 close $IF;
243 close OF;
244
245
246 format OF =
247 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>>
248 $fnbr,$hstprt,$dt,$lvl,$sz
249 .
250
251 format STDOUT =
252 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>> @>>>
253 $fnbr,$hstprt,$dt,$lvl,$sz,$ch
254 .