0e6a8d7da340e425acb84e208e5cada365394c18
[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     write($OF);
48   } else {
49     print $OF "$_[0]  $_[1]:$_[2]  $_[3]  $_[4]  $_[5]\n";
50   }
51 }
52 #--------------------------------------------------------
53
54
55
56 #--------------------------------------------------------
57 sub tfn($) {
58   # calculate tocfilename
59   $_ = $_[0];
60   foreach $s (@subs) {
61     eval $s;
62   }
63   return $dir . $_ ;
64 }
65 #--------------------------------------------------------
66
67
68 #--------------------------------------------------------
69 sub usage($) {
70   print STDERR "@_\n\n";
71   print STDERR "usage: amtoc [-a] [-i] [-t] [-f file] [-s subs] [-w] [--] logfile\n";
72   print STDERR "         -a      : file output to `label`.toc\n";
73   print STDERR "         -i      : Start TOC with a small help message\n";
74   print STDERR "         -t      : tabular output\n";
75   print STDERR "         -f file : output to file\n";
76   print STDERR "         -s subs : output file name evaluated to `eval \$subs`\n";
77   print STDERR "         -w      : add vertical whitespace after each tape\n";
78   print STDERR "         --      : last option\n";
79   print STDERR "         logfile : input file ('-' for stdin)\n";
80   exit;
81 }
82 #--------------------------------------------------------
83
84 #--------------------------------------------------------
85 sub init() {
86   &usage("amtoc required at least 'logfile' parameter.") if ($#ARGV==-1) ;
87
88   @subs = ();
89   for ($i=0;$i<=$#ARGV;$i++) {
90     if ($ARGV[$i] eq '-a') {
91         push (@subs, "s/\$/.toc/");
92       }
93     elsif ($ARGV[$i] eq '-i') {
94         $info=1;
95       }
96     elsif ($ARGV[$i] eq '-t') {
97         $tabular=1;
98       }
99     elsif ($ARGV[$i] eq '-f') {
100         $i++;
101         &usage("'-f' option require 'file' parameter.")  if ($i > $#ARGV);
102         $tocfilename=$ARGV[$i];
103       }
104     elsif ($ARGV[$i] eq '-s') {
105         $i++;
106         &usage("'-s' option require 'subs' parameter.")  if ($i > $#ARGV);
107         push (@subs, $ARGV[$i]);
108       }
109     elsif ($ARGV[$i] eq '-w') {
110         $vwspace=1;
111       }
112     elsif ($ARGV[$i] eq '--') {
113       # no more options: next arg == logfile
114         $i++;
115         &usage("amtoc required at least 'logfile' parameter.") if ($i > $#ARGV);
116         $logfile=$ARGV[$i];
117         &usage("too many parameters.") unless ($i == $#ARGV);
118       }
119     else {
120         $logfile=$ARGV[$i];
121         &usage("too many parameters.") unless ($i == $#ARGV);
122       }
123   }
124   &usage("amtoc required at least 'logfile' parameter.") unless ($logfile);
125 }
126
127 #--------------------------------------------------------
128
129 &init;
130
131 $dir=$logfile;
132 $dir =~ s/[^\/]*$//;
133
134
135 if ($logfile eq '-') {$IF=STDIN} else 
136   {die ("Cannot open logfile $logfile") unless open (IF,"$logfile");$IF=IF;}
137
138 $filenumber=0;
139 $tot_or_size=0;
140
141 while ( <$IF> ) {
142   $line = $_;
143   if ( /^FAIL dumper (\S+) (\S+)/ ) {
144     next;
145   }
146   if ( /^SUCCESS dumper (\S+) (\S+)/ ) {
147     $host = $1;
148     $part = $2;
149     $line =~ /orig-kb (\d+)/;
150     $osize{$host}{$part} = $1;
151     $tot_or_size += $osize{$host}{$part};
152     $fail{$host}{$part} = 0;
153     next;
154   }
155   if ( /^START amflush/ ) {
156     $flash_mode = 1;
157     next;
158   }
159   if ( ! /^([A-Z]+) taper (\S+) (\S+) (\S+) (\S+)/) { next;}
160   # $_ = $1;
161   $host = $2;
162   $part = $3;
163   $date = $4;
164   $level = $5;
165   switch: {
166     /START taper/ && do {
167       $tocfilename=&tfn($level) if ($#subs >= 0);
168       if (!$tocfilename || ($tocfilename eq '-')) {$OF=STDOUT;}
169       else {
170           die ("Cannot open tocfile $tocfilename") unless open(OF,">$tocfilename");
171           $OF=OF;
172         }
173
174         print $OF "\f" if ($vwspace && $filenumber);
175         if (defined($info)) {
176           print $OF "AMANDA: To restore:\n";
177           print $OF "    position tape at start of file and run:\n";
178           print $OF "        dd if=<tape> bs=32k skip=1 [ | zcat ] | restore -...f\n";
179           print $OF "    or run: amrestore -p <tape> [host [partition]] | restore -...f\n";
180           print $OF "\n";
181         }
182
183
184
185       $filenumber=0;
186       &pr("#","Server","/partition","date", "level","size[Kb]");
187       &pr("$filenumber","$level","","$part","-","-");
188       last switch; };
189     /SUCCESS taper/ && do {
190       $line =~ / kb (\d+) /;
191       if ( $fail{$host}{$part} ) {
192         &pr("$filenumber","${host}","${part}","${date}","${level}","FAIL");
193       } else {
194         if (defined($flash_mode)) {
195           &pr("$filenumber","${host}","${part}","${date}","${level}","$1");
196         } else {
197           if (defined($osize{$host}{$part})) {
198             &pr("$filenumber","${host}","${part}","${date}","${level}","$osize{$host}{$part}");
199           } else {
200                 # this case should never happend: 
201             &pr("$filenumber","${host}","${part}","${date}","${level}","*$1");
202             $strange=1;
203           }
204         }
205       }
206       last switch;};
207     /INFO taper retrying/ && do {
208       --$filenumber;
209       last switch; };
210     /INFO taper tape .* \[OK\]/ && do {
211       $line =~ / kb (\d+) /;
212       $size = $1;
213       $line =~ / fm (\d+) /;
214       print "\n\n" if ($vwspace);
215       &pr("$1","total","on_tape","-","-","$size");
216       if (defined($flash_mode)) {
217         &pr("$1","total","origin","-","not","available");
218       } else {
219         &pr("$1","total","origin","-","-","$tot_or_size");
220       }
221       if (defined($strange)) {
222         &pr("*","size","on_tape","-","-","-");
223       }
224       last switch; };
225     /FAIL taper/ && do { next; };
226   }
227   $filenumber += 1;
228 }
229 close $IF;
230 close OF;
231
232
233 format OF =
234 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>>
235 $fnbr,$hstprt,$dt,$lvl,$sz
236 .
237
238 format STDOUT =
239 @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<< @>> @>>>>>>>>
240 $fnbr,$hstprt,$dt,$lvl,$sz
241 .