7192e181e1605b63b5e8b0ccd2ba381851708385
[debian/amanda] / server-src / amstatus.pl
1 #!@PERL@
2 #
3
4 # Run perl.
5 eval '(exit $?0)' && eval 'exec @PERL@ -S $0 ${1+"$@"}'
6          & eval 'exec @PERL@ -S $0 $argv:q'
7                 if 0;
8
9 use warnings;
10 use lib '@amperldir@';
11 use Time::Local;
12 use Text::ParseWords;
13 use Amanda::Util;
14 use Amanda::Process;
15 use Getopt::Long;
16
17 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
18 $ENV{'PATH'} = "/bin:/usr/bin:/usr/sbin:/sbin";       # force known path
19
20 $confdir="@CONFIG_DIR@";
21 $prefix='@prefix@';
22 $prefix=$prefix;                # avoid warnings about possible typo
23 $exec_prefix="@exec_prefix@";
24 $exec_prefix=$exec_prefix;      # ditto
25 $sbindir="@sbindir@";
26
27 my $Amanda_process = Amanda::Process->new(0);
28 $Amanda_process->load_ps_table();
29
30 #$STATUS_STRANGE =  2;
31 $STATUS_FAILED  =  4;
32 $STATUS_MISSING =  8;
33 $STATUS_TAPE    = 16;
34 $exit_status    =  0;
35
36 my $opt_summary;
37 my $opt_stats;
38 my $opt_dumping;
39 my $opt_waitdumping;
40 my $opt_waittaper;
41 my $opt_dumpingtape;
42 my $opt_writingtape;
43 my $opt_finished;
44 my $opt_failed;
45 my $opt_estimate;
46 my $opt_gestimate;
47 my $opt_date;
48 my $opt_config;
49 my $opt_file;
50 my $opt_locale_independent_date_format;
51
52 sub usage() {
53         print "amstatus [--file amdump_file]\n";
54         print "         [--summary] [--dumping] [--waitdumping] [--waittaper]\n";
55         print "         [--dumpingtape] [--writingtape] [--finished] [--failed]\n";
56         print "         [--estimate] [--gestimate] [--stats] [--date]\n";
57         print "         [--locale-independent-date-format]\n";
58         print "         [--config] <config>\n";
59         exit 0;
60 }
61
62 Getopt::Long::Configure(qw{ bundling });
63 GetOptions(
64     'summary'                        => \$opt_summary,
65     'stats|statistics'               => \$opt_stats,
66     'dumping|d'                      => \$opt_dumping,
67     'waitdumping|wdumping'           => \$opt_waitdumping,
68     'waittaper|wtaper'               => \$opt_waittaper,
69     'dumpingtape|dtape'              => \$opt_dumpingtape,
70     'writingtape|wtape'              => \$opt_writingtape,
71     'finished'                       => \$opt_finished,
72     'failed|error'                   => \$opt_failed,
73     'estimate'                       => \$opt_estimate,
74     'gestimate|gettingestimate'      => \$opt_gestimate,
75     'date'                           => \$opt_date,
76     'config|c:s'                     => \$opt_config,
77     'file:s'                         => \$opt_file,
78     'locale-independent-date-format' => \$opt_locale_independent_date_format,
79     ) or usage();
80
81
82 if( defined $opt_config ) {
83         $conf = $opt_config;
84 }
85 else {
86         if($#ARGV == 0 ) {
87                 $conf=$ARGV[0];
88         }
89         else {
90                 &usage();
91         }
92 }
93
94 #untaint user input $ARGV[0]
95
96 if ($conf =~ /^([\w.-]+)$/) {          # $1 is untainted
97    $conf = $1;
98 } else {
99     die "filename '$conf' has invalid characters.\n";
100 }
101
102 if ( ! -e "$confdir/$conf" ) {
103     print "Configuration directory '$confdir/$conf' doesn't exist\n";
104     exit 1;
105 }
106 if ( ! -d "$confdir/$conf" ) {
107     print "Configuration directory '$confdir/$conf' is not a directory\n";
108     exit 1;
109  }
110
111
112 $pwd = `pwd`;
113 chomp $pwd;
114 chdir "$confdir/$conf";
115
116 $logdir=`$sbindir/amgetconf logdir`;
117 exit 1 if $? != 0;
118 chomp $logdir;
119 $errfile="$logdir/amdump";
120
121 $nb_options = defined( $opt_summary ) +
122                                   defined( $opt_stats ) +
123                                   defined( $opt_dumping ) +
124                                   defined( $opt_waitdumping ) +
125                                   defined( $opt_waittaper ) +
126                                   defined( $opt_dumpingtape ) +
127                                   defined( $opt_writingtape ) +
128                                   defined( $opt_finished ) +
129                                   defined( $opt_estimate ) +
130                                   defined( $opt_gestimate ) +
131                                   defined( $opt_failed );
132
133 if($nb_options == 0 ) {
134         $opt_summary     = 1;
135         $opt_stats       = 1; 
136         $opt_dumping     = 1;
137         $opt_waitdumping = 1;
138         $opt_waittaper   = 1;
139         $opt_dumpingtape = 1;
140         $opt_writingtape = 1;
141         $opt_finished    = 1;
142         $opt_failed      = 1;
143         $opt_gestimate   = 1;
144         $opt_estimate    = 1;
145 }
146
147 $unit=`$sbindir/amgetconf displayunit`;
148 chomp($unit);
149 $unit =~ tr/A-Z/a-z/;
150 $unitdivisor=1;
151 if($unit eq 'k') {
152   $unitdivisor = 1;
153 }
154 elsif($unit eq 'm') {
155   $unitdivisor = 1024;
156 }
157 elsif($unit eq 'g') {
158   $unitdivisor = 1024*1024;
159 }
160 elsif($unit eq 't') {
161   $unitdivisor = 1024*1024*1024;
162 }
163 else {
164   $unit = 'k';
165   $unitdivisor = 1;
166 }
167
168
169 my $dead_run = 0;
170 if( defined $opt_file) {
171         if( $opt_file =~ m,^/, ) {
172                 $errfile = $opt_file;
173         } else {
174                 $errfile = "$pwd/$opt_file";
175                 $errfile = "$logdir/$opt_file" if ( ! (-f $errfile ));
176         }
177 }
178 else {
179         $errfile="$logdir/amflush" if(! (-f $errfile));
180         if (! -f $errfile) {
181                 if (-f "$logdir/amflush.1" && -f "$logdir/amdump.1" &&
182                     -M "$logdir/amflush.1"  < -M "$logdir/amdump.1") {
183                         $errfile="$logdir/amflush.1";
184                 } else {
185                         $errfile="$logdir/amdump.1";
186                 }
187                 $dead_run = 1;
188         }
189 }
190
191 open(AMDUMP,"<$errfile") || die("$errfile: $!");
192 print "Using $errfile\n";
193
194 my $taper_status_file;
195
196 $start_degraded_mode = 0;
197
198 $label = "";                                    # -w fodder
199 $origsize = 0;                                  # -w fodder
200 $idle_dumpers = 0;
201 $status_driver = "";
202 $status_taper = "Searching for a new tape";
203 $estimate_done = 0;
204 $holding_space = 0;
205 $start_time = 0;
206 @dumpers_active = ();
207 $nb_tape = 0;
208 $ntpartition{$nb_tape} = 0;
209 $ntsize{$nb_tape} = 0;
210 $ntesize{$nb_tape} = 0;
211 $tape_size = 0;
212 $driver_finished = 0;
213 $generating_schedule = 0;
214
215 while($lineX = <AMDUMP>) {
216         chomp $lineX;
217         $lineX =~ s/[:\s]+$//g; #remove separator at end of line
218         next if $lineX eq "";
219         @line = &quotewords('[:\s]+', 0, $lineX);
220         next if !defined $line[0];
221
222         if($line[0] eq "amdump" || $line[0] eq "amflush") {
223                 if ($line[1] eq "start" && $line[2] eq "at") {
224                         $datestr = $lineX;
225                         $datestr =~ s/.*start at //g;
226                         if (!defined $opt_locale_independent_date_format) {
227                                 print "From " . $datestr . "\n";
228                         }
229                 } elsif($line[1] eq "datestamp") {
230                         $gdatestamp = $line[2];
231                         if(!defined $datestamp{$gdatestamp}) {
232                                 $datestamp{$gdatestamp} = 1;
233                                 push @datestamp, $gdatestamp;
234                         }
235                 } elsif($line[1] eq "starttime") {
236                         $starttime=&set_starttime($line[2]);
237                 } elsif($line[1] eq "starttime-locale-independent") {
238                         if (defined $opt_locale_independent_date_format) {
239                                 printf "From " . $line[2] . " " . $line[3] . ":" . $line[4] . ":" . $line[5] . " " . $line[6] . "\n";
240                         }
241                 }
242                 if($line[0] eq "amflush") {
243                         $estimate_done=1;
244                 }
245         } elsif($line[0] eq "planner") {
246                 if($line[1] eq "timestamp") {
247                         $gdatestamp = $line[2];
248                         if(!defined $datestamp{$gdatestamp}) {
249                                 $datestamp{$gdatestamp} = 1;
250                                 push @datestamp, $gdatestamp;
251                         }
252                 }
253                 elsif($line[1] eq "FAILED") {
254                         #2:host 3:disk 4:datestamp 5:level 6:errmsg
255                         $host=$line[2];
256                         $partition=$line[3];
257                         $datestamp=$line[4];
258                         $hostpart=&make_hostpart($host,$partition,$datestamp);
259                         $dump_started{$hostpart}=-1;
260                         $level{$hostpart}=$line[5];
261                         $error{$hostpart}="planner: " . $line[6];
262         } elsif($line[1] eq "time") {
263                 if($line[3] eq "got") {
264                                 if($line[4] eq "result") {
265                                         $host = $line[7];
266                                         $partition = $line[9];
267                                         $hostpart=&make_hostpart($host,$partition,$gdatestamp);
268                                         $estimate{$hostpart}=1;
269                                         $level{$hostpart}=$line[10];
270                                         $line[12] =~ /(\d+)K/;
271                                         $esize{$hostpart}=$1 / $unitdivisor;
272                                         $partialestimate{$hostpart}=0;
273                                         $getest{$hostpart} = "";
274                                 } elsif($line[4] eq "partial") {
275                                         $host = $line[8];
276                                         $partition = $line[10];
277                                         $hostpart=&make_hostpart($host,$partition,$gdatestamp);
278                                         $level1 = $line[11];
279                                         $line[13] =~ /(-?\d+)K/;
280                                         $size1 = $1;
281                                         $level2 = $line[14];
282                                         $line[16] =~ /(-?\d+)K/;
283                                         $size2 = $1;
284                                         $level3 = $line[17];
285                                         $line[19] =~ /(-?\d+)K/;
286                                         $size3 = $1;
287                                         if($size1 > 0 || $size2 > 0 || $size3 > 0) {
288                                                 $estimate{$hostpart}=1;
289                                                 $level{$hostpart}=$line[11];
290                                                 $esize{$hostpart}=$size1 / $unitdivisor;
291                                                 $partialestimate{$hostpart}=1;
292                                                 if($size1 > 0) { $getest{$hostpart} =~ s/:$level1://; }
293                                                 if($size2 > 0) { $getest{$hostpart} =~ s/:$level2://; }
294                                                 if($size3 > 0) { $getest{$hostpart} =~ s/:$level3://; }
295                                                 if($getest{$hostpart} eq "") {$partialestimate{$hostpart}=0;}
296                                         }
297                                 }
298                         } elsif($line[3] eq "getting" &&
299                                           $line[4] eq "estimates" &&
300                                           $line[5] eq "took") {
301                                 $estimate_done=1;
302                         }
303                 }
304         } elsif($line[0] eq "setup_estimate") {
305                 $host = $line[1];
306                 $partition = $line[2];
307                 $hostpart=&make_hostpart($host,$partition,$gdatestamp);
308                 $estimate{$hostpart}=0;
309                 $level{$hostpart}=0;
310                 $degr_level{$hostpart}=-1;
311                 $esize{$hostpart}=0;
312                 $dump_started{$hostpart}=0;
313                 $dump_finished{$hostpart}=0;
314                 $taper_started{$hostpart}=0;
315                 $taper_finished{$hostpart}=0;
316                 $partialestimate{$hostpart}=0;
317                 $error{$hostpart}="";
318                 if($line[7] eq "last_level") {
319                         $getest{$hostpart}="";
320                         $level1 = $line[15];
321                         $level2 = $line[17];
322                         $level3 = $line[19];
323                         if($level1 != -1) { $getest{$hostpart} .= ":$level1:" };
324                         if($level2 != -1) { $getest{$hostpart} .= ":$level2:" };
325                         if($level3 != -1) { $getest{$hostpart} .= ":$level3:" };
326                 }
327         } elsif($line[0] eq "GENERATING" &&
328                                 $line[1] eq "SCHEDULE") {
329                 $generating_schedule=1;
330         } elsif($line[0] eq "--------") {
331                 if ($generating_schedule == 1) {
332                         $generating_schedule = 2;
333                 } elsif ($generating_schedule == 2) {
334                         $generating_schedule = 3;
335                 }
336         } elsif($line[0] eq "DUMP") {
337                 if($generating_schedule == 2 ) {
338                         $host = $line[1];
339                         $partition = $line[3];
340                         $datestamp = $line[4];
341                         $hostpart=&make_hostpart($host,$partition,$datestamp);
342                         $level{$hostpart}=$line[6];
343                         $esize=$line[14];       #compressed size
344                         $esize=32 if $esize<32;
345                         $esize{$hostpart}=$esize / $unitdivisor;
346                         if(!defined($line[25])) {
347                                 $degr_level{$hostpart}=-1;
348                         } else {
349                                 $degr_level{$hostpart}=$line[17];
350                                 $esize=$line[25];       #compressed size
351                                 $esize=32 if $esize<32;
352                                 $degr_size{$hostpart}=$esize / $unitdivisor;
353                         }
354                 }
355         } elsif($line[0] eq "FLUSH") {
356                 $host = $line[1];
357                 $partition = $line[2];
358                 $datestamp = $line[3];
359                 $level = $line[4];
360                 $holding_file = $line[5];
361                 $hostpart=&make_hostpart($host,$partition,$datestamp);
362                 $flush{$hostpart}=0;
363                 $dump_finished{$hostpart}=0;
364                 $holding_file{$hostpart}=$holding_file;
365                 $level{$hostpart}=$level;
366         } elsif($line[0] eq "driver") {
367                 if($line[1] eq "pid") {
368                         $pid = $line[2];
369                         if (! $Amanda_process->process_alive($pid, "driver")) {
370                                 $dead_run = 1;
371                         }
372                 }
373                 elsif($line[1] eq "start" && $line[2] eq "time") {
374                         $start_time=$line[3];
375                         $current_time=$line[3];
376                         $dumpers_active[0]=0;
377                         $dumpers_held[0]={};
378                         $dumpers_active=0;
379                 }
380                 elsif($line[1] eq "tape" && $line[2] eq "size") {
381                         $lineX =~ /^driver: start time (\S+)/;
382                         $tape_size = $line[3] / $unitdivisor;
383                 }
384                 elsif($line[1] eq "adding" &&
385                            $line[2] eq "holding" &&
386                                 $line[3] eq "disk") {
387                         $holding_space += $line[8];
388                 }
389                 elsif($line[1] eq "send-cmd" && $line[2] eq "time") {
390                         #print "send-cmd: " , $line[5] . " " . $line[6] . " " . $line[7] . "\n" if defined $line[5] && defined $line[6] && defined $line[7];
391                         $current_time = $line[3];
392                         if($line[5] =~ /dumper\d*/) {
393                                 $dumper = $line[5];
394                                 if($line[6] eq "PORT-DUMP") {
395                                         #7:handle 8:port 9:host 10:amfeatures 11:disk 12:device 13:level ...
396                                         $host = $line[9];
397                                         $partition = $line[11];
398                                         $hostpart=&make_hostpart($host,$partition,$gdatestamp);
399                                         $serial=$line[7];
400                                         $dump_started{$hostpart}=1;
401                                         $dump_time{$hostpart}=$current_time;
402                                         $dump_finished{$hostpart}=0;
403                                         if(     $level{$hostpart} != $line[13] &&
404                                            $degr_level{$hostpart} == $line[13]) {
405                                                 $level{$hostpart}=$degr_level{$hostpart};
406                                                 $esize{$hostpart}=$degr_size{$hostpart};
407                                         }
408                                         if(! defined($busy_time{$dumper})) {
409                                                 $busy_time{$dumper}=0;
410                                         }
411                                         $running_dumper{$dumper} = $hostpart;
412                                         $error{$hostpart}="";
413                                         $size{$hostpart} = 0;
414                                         $dumpers_active++;
415                                         if(! defined($dumpers_active[$dumpers_active])) {
416                                                 $dumpers_active[$dumpers_active]=0;
417                                         }
418                                         if(! defined($dumpers_held[$dumpers_active])) {
419                                                 $dumpers_held[$dumpers_active]={};
420                                         }
421                                 }
422                         }
423                         elsif($line[5] =~ /chunker\d*/) {
424                                 if($line[6] eq "PORT-WRITE") {
425                                         $host=$line[9];
426                                         $partition=$line[11];
427                                         $hostpart=&make_hostpart($host,$partition,$gdatestamp);
428                                         $serial=$line[7];
429                                         $serial{$serial}=$hostpart;
430                                         $holding_file{$hostpart}=$line[8];
431                                         #$chunk_started{$hostpart}=1;
432                                         $chunk_time{$hostpart}=$current_time;
433                                         #$chunk_finished{$hostpart}=0;
434                                         $size{$hostpart} = 0;
435                                 }
436                                 elsif($line[6] eq "CONTINUE") {
437                                         #7:handle 8:filename 9:chunksize 10:use
438                                         $serial=$line[7];
439                                         $hostpart=$serial{$serial};
440                                         if($hostpart ne "") {
441                                                 $dump_roomq{$hostpart}=undef;
442                                                 $error{$hostpart}="";
443                                         }
444                                 }
445                         }
446                         elsif($line[5] =~ /taper/) {
447                                 if($line[6] eq "START-TAPER") {
448                                         #7:name 8:timestamp
449                                         $gdatestamp=$line[8];
450                                         if(!defined $datestamp{$gdatestamp}) {
451                                                 $datestamp{$gdatestamp} = 1;
452                                                 push @datestamp, $gdatestamp;
453                                         }
454                                         $status_taper = "Searching for a new tape";
455                                 }
456                                 elsif($line[6] eq "NEW-TAPE") {
457                                         #7:name 8:handle
458                                         $status_taper = "Searching for a new tape";
459                                 }
460                                 elsif($line[6] eq "NO-NEW-TAPE") {
461                                         #7:name 8:handle 9:errmsg
462                                         $serial=$line[8];
463                                         $error=$line[9];
464                                         $status_taper = $error;
465                                 }
466                                 elsif($line[6] eq "FILE-WRITE") {
467                                         #7:name 8:handle 9:filename 10:host 11:disk 12:level 13:datestamp 14:splitsize
468                                         $serial=$line[8];
469                                         $host=$line[10];
470                                         $partition=$line[11];
471                                         $level=$line[12];
472                                         $ldatestamp=$line[13];
473                                         $status_taper = "Writing $host:$partition";
474                                         if(!defined $datestamp{$ldatestamp}) {
475                                                 $datestamp{$ldatestamp} = 1;
476                                                 push @datestamp, $ldatestamp;
477                                         }
478                                         $hostpart=&make_hostpart($host,$partition,$ldatestamp);
479                                         $serial{$serial}=$hostpart;
480                                         if(!defined $level{$hostpart}) {
481                                                 $level{$hostpart} = $level;
482                                         }
483                                         $taper_started{$hostpart}=1;
484                                         $taper_finished{$hostpart}=0;
485                                         $taper_time{$hostpart}=$current_time;
486                                         $ntchunk_size = 0;
487                                 }
488                                 elsif($line[6] eq "PORT-WRITE") {
489                                         #7:name 8:handle 9:host 10:disk 11:level 12:datestamp 13:splitsize 14:diskbuffer 15:fallback_splitsize
490                                         $serial=$line[8];
491                                         $host=$line[9];
492                                         $partition=$line[10];
493                                         $level=$line[11];
494                                         $ldatestamp=$line[12];
495                                         $status_taper = "Writing $host:$partition";
496                                         $hostpart=&make_hostpart($host,$partition,$ldatestamp);
497                                         $serial{$serial}=$hostpart;
498                                         $taper_started{$hostpart}=1;
499                                         $taper_finished{$hostpart}=0;
500                                         $taper_time{$hostpart}=$current_time;
501                                         $size{$hostpart} = 0;
502                                         $ntchunk_size = 0;
503                                 }
504                         }
505                 }
506                 elsif($line[1] eq "result" && $line[2] eq "time") {
507                         #print "result: " , $line[5] . " " . $line[6] . " " . $line[7] . "\n" if defined $line[5] && defined $line[6] && defined $line[7];
508                         $current_time = $line[3];
509                         if($line[5] =~ /dumper\d+/) {
510                                 if($line[6] eq "FAILED" || $line[6] eq "TRY-AGAIN") {
511                                         #7:handle 8:message
512                                         $serial = $line[7];
513                                         $error = $line[8];
514                                         $hostpart=$serial{$serial};
515                                         if ($taper_started{$hostpart} == 1) {
516                                                 $dump_finished{$hostpart}=-1;
517                                         } else {
518                                                 $dump_finished{$hostpart}=-3;
519                                         }
520                                         $busy_time{$line[5]}+=($current_time-$dump_time{$hostpart});
521                               $running_dumper{$line[5]} = "0";
522                               $dump_time{$hostpart}=$current_time;
523                               $error{$hostpart}="dumper: $error";
524                               $dumpers_active--;
525
526                                 }
527                                 elsif($line[6] eq "DONE") {
528                                         #7:handle 8:origsize 9:size ...
529                                         $serial=$line[7];
530                                         $origsize=$line[8] / $unitdivisor;
531                                         $outputsize=$line[9] / $unitdivisor;
532                                         $hostpart=$serial{$serial};
533                                         $size{$hostpart}=$outputsize;
534                                         $dump_finished{$hostpart}=1;
535                                         $busy_time{$line[5]}+=($current_time-$dump_time{$hostpart});
536                                         $running_dumper{$line[5]} = "0";
537                                         $dump_time{$hostpart}=$current_time;
538                                         $error{$hostpart}="";
539                                         $dumpers_active--;
540                                 }
541                                 elsif($line[6] eq "ABORT-FINISHED") {
542                                         #7:handle
543                                         $serial=$line[7];
544                                         $hostpart=$serial{$serial};
545                                         $dump_started{$hostpart}=0;
546                                         if ($taper_started{$hostpart} == 1) {
547                                                 $dump_finished{$hostpart}=-1;
548                                         } else {
549                                                 $dump_finished{$hostpart}=-3;
550                                         }
551                                         $busy_time{$line[5]}+=($current_time-$dump_time{$hostpart});
552                                         $running_dumper{$line[5]} = "0";
553                                         $dump_time{$hostpart}=$current_time;
554                                         $error{$hostpart}="dumper: (aborted)";
555                                         $dumpers_active--;
556                                 }
557                         }
558                         elsif($line[5] =~ /chunker\d+/) {
559                                 if($line[6] eq "DONE" || $line[6] eq "PARTIAL") {
560                                         #7:handle 8:size
561                                         $serial=$line[7];
562                                         $outputsize=$line[8] / $unitdivisor;
563                                         $hostpart=$serial{$serial};
564                                         $size{$hostpart}=$outputsize;
565                                         if ($line[6] eq "DONE") {
566                                                 $dump_finished{$hostpart}=1;
567                                         } else {
568                                                 $dump_finished{$hostpart}=-3;
569                                         }
570                                         $busy_time{$line[5]}+=($current_time-$chunk_time{$hostpart});
571                                         $running_dumper{$line[5]} = "0";
572                                         $chunk_time{$hostpart}=$current_time;
573                                         if ($line[6] eq "PARTIAL") {
574                                                 $partial{$hostpart} = 1;
575                                         }
576                                         else {
577                                                 $partial{$hostpart} = 0;
578                                                 $error{$hostpart}="";
579                                         }
580                                 }
581                                 elsif($line[6] eq "FAILED") {
582                                         $serial=$line[7];
583                                         $hostpart=$serial{$serial};
584                                         $dump_finished{$hostpart}=-1;
585                                         $busy_time{$line[5]}+=($current_time-$chunk_time{$hostpart});
586                                         $running_dumper{$line[5]} = "0";
587                                         $chunk_time{$hostpart}=$current_time;
588                                         $error{$hostpart}="chunker: " .$line[8] if $error{$hostpart} eq "";
589                                 }
590                                 elsif($line[6] eq "RQ-MORE-DISK") {
591                                         #7:handle
592                                         $serial=$line[7];
593                                         $hostpart=$serial{$serial};
594                                         $dump_roomq{$hostpart}=1;
595                                         $error{$hostpart}="(waiting for holding disk space)";
596                                 }
597                         }
598                         elsif($line[5] eq "taper") {
599                                 if($line[6] eq "DONE" || $line[6] eq "PARTIAL") {
600                                         #7:handle 8:label 9:filenum 10:errstr
601                                         $serial=$line[7];
602                                         $label=$line[8];
603                                         $status_taper = "Idle";
604                                         $hostpart=$serial{$serial};
605                                         $line[10] =~ /sec (\S+) (kb|bytes) (\d+) kps/;
606                                         if ($2 eq 'kb') {
607                                                 $size=$3 / $unitdivisor;
608                                    } else {
609                                                 $size=$3 / ( $unitdivisor * 1024);
610                                         }
611                                         $taper_finished{$hostpart}=1;
612                                         $busy_time{"taper"}+=($current_time-$taper_time{$hostpart});
613                                         $taper_time{$hostpart}=$current_time;
614                                         if(!defined $size{$hostpart}) {
615                                                 $size{$hostpart}=$size;
616                                         }
617                                         $ntpartition{$nb_tape}++;
618                                         $ntsize{$nb_tape} += $size{$hostpart} - $ntchunk_size;
619                                         if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
620                                                 $ntesize{$nb_tape} += $esize{$hostpart} - $ntchunk_size;
621                                         }
622                                         else {
623                                                 $ntesize{$nb_tape} += $size{$hostpart} - $ntchunk_size;
624                                         }
625                                         if ($line[6] eq "PARTIAL") {
626                                                 $partial{$hostpart} = 1;
627                                         }
628                                         else {
629                                                 $partial{$hostpart} = 0;
630                                         }
631                                         if ($ntchunk_size > 0) {
632                                                 $ntchunk{$nb_tape}++;
633                                         }
634                                         undef $taper_status_file;
635                                 }
636                                 elsif($line[6] eq "PARTDONE") {
637                                         #7:handle 8:label 9:filenum 10:ksize 11:errstr
638                                         $serial=$line[7];
639                                         $hostpart=$serial{$serial};
640                                         #$line[11] =~ /.*kb (\d*) kps/;
641                                         #$size=$1 / $unitdivisor;
642                                         $size=$line[10] / $unitdivisor;
643                                         $tapedsize{$hostpart} += $size;
644                                         $ntchunk{$nb_tape}++;
645                                         $ntsize{$nb_tape} += $size;
646                                         $ntesize{$nb_tape} += $size;
647                                         $ntchunk_size += $size;
648                                 }
649                                 elsif($line[6] eq "REQUEST-NEW-TAPE") {
650                                         #7:serial
651                                         $serial=$line[7];
652                                         $old_status_taper = $status_taper;
653                                         $status_taper = "Asking for a new tape";
654                                         $hostpart=$serial{$serial};
655                                         if (defined $hostpart) {
656                                                 $olderror{$hostpart} = $error{$hostpart};
657                                                 $error{$hostpart} = "waiting for a new tape";
658                                         }
659                                 }
660                                 elsif($line[6] eq "NEW-TAPE") {
661                                         #7:serial #8:label
662                                         $serial=$line[7];
663                                         $status_taper = $old_status_taper;
664                                         $hostpart=$serial{$serial};
665                                         if (defined $hostpart) {
666                                                 $error{$hostpart} = $olderror{$hostpart};
667                                         }
668                                 }
669                                 elsif($line[6] eq "TAPER-OK") {
670                                         #7:name #8:label
671                                         $status_taper = "Idle";
672                                 }
673                                 elsif($line[6] eq "TAPE-ERROR") {
674                                         #7:name 8:errstr
675                                         $error=$line[8];
676                                         $status_taper = $error;
677                                         $exit_status |= $STATUS_TAPE;
678                                         undef $taper_status_file;
679                                 }
680                                 elsif($line[6] eq "FAILED") {
681                                         #7:handle 8:INPUT- 9:TAPE- 10:input_message 11:tape_message
682                                    $serial=$line[7];
683                                         $hostpart=$serial{$serial};
684                                         if(defined $hostpart) {
685                                                 if($line[9] eq "TAPE-ERROR") {
686                                                         $error=$line[11];
687                                                         $taper_finished{$hostpart} = -2;
688                                                         $status_taper = $error;
689                                                 }
690                                                 else {
691                                                         $error=$line[10];
692                                                         $taper_finished{$hostpart} = -1;
693                                                         $status_taper = "Idle";
694                                                 }
695                                                 $busy_time{"taper"}+=($current_time-$taper_time{$hostpart});
696                                                 $taper_time{$hostpart}=$current_time;
697                                                 $error{$hostpart}="$error";
698                                         }
699                                         undef $taper_status_file;
700                                 }
701                         }
702                 }
703                 elsif($line[1] eq "finished-cmd" && $line[2] eq "time") {
704                         $current_time=$line[3];
705                         if($line[4] =~ /dumper\d+/) {
706                         }
707                 }
708                 elsif($line[1] eq "dump" && $line[2] eq "failed") {
709                         #3:handle 4: 5: 6:"too many dumper retry"
710                         $serial=$line[3];
711                         $hostpart=$serial{$serial};
712                         $dump_started{$hostpart}=-1;
713                         $dump_finished{$hostpart}=-2;
714                         $error{$hostpart} .= "(" . $line[6] . ")";
715                 }
716                 elsif($line[1] eq "tape" && $line[2] eq "failed") {
717                         #3:handle 4: 5: 6:"too many dumper retry"
718                         $serial=$line[3];
719                         $hostpart=$serial{$serial};
720                         $taper_started{$hostpart}=-1;
721                         $taper_finished{$hostpart}=-2;
722                         $error{$hostpart} .= "(" . $line[6] . ")";
723                 }
724                 elsif($line[1] eq "state" && $line[2] eq "time") {
725                         #3:time 4:"free" 5:"kps" 6:free 7:"space" 8:space 9:"taper" 10:taper 11:"idle-dumpers" 12:idle-dumpers 13:"qlen" 14:"tapeq" 15:tapeq 16:"runq" 17:runq 18:"roomq" 19:roomq 20:"wakeup" 21:wakeup 22:"driver-idle" 23:driver-idle
726                         $current_time=$line[3];
727                         $idle_dumpers=$line[12];
728
729                         $free{"kps"} = $line[6];
730                         $free{"space"} = $line[8];
731                         $qlen{"tapeq"} = $line[15];
732                         $qlen{"runq"} = $line[17];
733                         $qlen{"roomq"} = $line[19];
734
735                         if(defined($dumpers_active)) {
736                                 if($status_driver ne "") {
737                                         $dumpers_active[$dumpers_active_prev]
738                                                 +=$current_time-$state_time_prev;
739                                         $dumpers_held[$dumpers_active_prev]{$status_driver}
740                                                 +=$current_time-$state_time_prev;
741                                 }
742                                 $state_time_prev=$current_time;
743                                 $dumpers_active_prev=$dumpers_active;
744                                 $status_driver=$line[23];
745                                 if(! defined($dumpers_held[$dumpers_active]{$status_driver})) {
746                                         $dumpers_held[$dumpers_active]{$status_driver}=0;
747                                 }
748                         }
749                 }
750            elsif($line[1] eq "FINISHED") {
751                         $driver_finished = 1;
752                 }
753         }
754         elsif($line[0] eq "dump") {
755                 if($line[1] eq "of" &&
756                         $line[2] eq "driver" &&
757                         $line[3] eq "schedule" &&
758                         $line[4] eq "after" &&
759                         $line[5] eq "start" &&
760                         $line[6] eq "degraded" &&
761                         $line[7] eq "mode") {
762                         $start_degraded_mode=1;
763                 }
764         }
765         elsif($line[0] eq "taper") {
766                 if($line[1] eq "wrote") {
767                         #1:"wrote" 2:"label" 3:label
768                         $nb_tape++;
769                         $label = $line[3];
770                         $ntlabel{$nb_tape} = $label;
771                         $ntpartition{$nb_tape} = 0;
772                         $ntsize{$nb_tape} = 0;
773                         $ntesize{$nb_tape} = 0;
774                 }
775                 elsif($line[1] eq "status" && $line[2] eq "file") {
776                         #1:"status" #2:"file:" #3:hostname #4:diskname #5:filename
777                         $taper_status_file = $line[5];
778                 }
779         }
780         elsif($line[0] eq "splitting" &&
781                          $line[1] eq "chunk" &&
782                          $line[2] eq "that" &&
783                          $line[3] eq "started" &&
784                          $line[4] eq "at" &&
785                          $line[6] eq "after") {
786                 $line[7] =~ /(\d*)kb/;
787                 $size = $1;
788                 $ntchunk{$nb_tape}++;
789                 $ntsize{$nb_tape} += $size / $unitdivisor;
790                 $ntesize{$nb_tape} += $size / $unitdivisor;
791                 $ntchunk_size += $size / $unitdivisor;
792         }
793         else {
794                 #print "Ignoring: $lineX\n";
795         }
796 }
797
798 close(AMDUMP);
799
800 if(defined $current_time) {
801         for ($d = 0; $d < $#dumpers_active; $d++) {
802                 $the_dumper = "dumper$d";
803                 if(defined($running_dumper{$the_dumper}) &&
804                    $running_dumper{$the_dumper} ne "0") {
805                         $busy_time{$the_dumper}+=($current_time-$dump_time{$running_dumper{$the_dumper}});
806                 }
807         }
808 }
809
810 print "\n";
811
812 $nb_partition = 0;
813
814 $epartition = 0;
815 $estsize = 0;
816 $fpartition = 0;
817 $fsize = 0;
818 $wpartition = 0;
819 $wsize = 0;
820
821 $flpartition = 0;
822 $flsize = 0;
823 $wfpartition = 0;
824 $wfsize = 0;
825
826 $dtpartition = 0;
827 $dtesize = 0;
828 $dupartition = 0;
829 $dusize = 0;
830 $duesize = 0;
831 $dpartition = 0;
832 $dsize = 0;
833 $desize = 0;
834
835 $twpartition = 0;
836 $twsize = 0;
837 $twesize = 0;
838 $tapartition = 0;
839 $tasize = 0;
840 $taesize = 0;
841 $tfpartition = 0;
842 $tfsize = 0;
843 $tfesize = 0;
844 $tpartition = 0;
845 $tsize = 0;
846 $tesize = 0;
847
848 $maxnamelength = 10;
849 foreach $host (sort @hosts) {
850         foreach $partition (sort @$host) {
851                 foreach $datestamp (sort @datestamp) {
852                         $hostpart=&make_hostpart($host,$partition,$datestamp);
853                         next if(!defined $estimate{$hostpart} && !defined $flush{$hostpart});
854                         if(length("$host:$partition") > $maxnamelength) {
855                                 $maxnamelength = length("$host:$partition");
856                         }
857                 }
858         }
859 }
860
861 foreach $host (sort @hosts) {
862         foreach $partition (sort @$host) {
863            $qpartition = Amanda::Util::quote_string($partition);
864            foreach $datestamp (sort @datestamp) {
865                         $hostpart=&make_hostpart($host,$partition,$datestamp);
866                         next if(!defined $estimate{$hostpart} && !defined $flush{$hostpart});
867                         $nb_partition++;
868                         if( (!defined $size{$hostpart} || $size{$hostpart} == 0) &&
869                                  defined $holding_file{$hostpart}) {
870                                 $size{$hostpart} = &dump_size($holding_file{$hostpart}) / (1024 * $unitdivisor);
871                         }
872                         $in_flush=0;
873                         if($estimate_done != 1 && !defined $flush{$hostpart}) {
874                                 if(defined $estimate{$hostpart}) {
875                                         if($estimate{$hostpart} != 1) {
876                                                 if( defined $opt_gestimate ||
877                                                          defined $opt_failed && $dead_run != 0) {
878                                                         printf "%8s ", $datestamp if defined $opt_date;
879                                                         printf "%-${maxnamelength}s", "$host:$qpartition";
880                                                         print "             ";
881                                                         if ($dead_run) {
882                                                                 print " failed: killed while";
883                                                                 $exit_status |= $STATUS_FAILED;
884                                                         }
885                                                         print " getting estimate\n";
886                                                 }
887                                         }
888                                         else {
889                                                 if(defined $opt_estimate ||
890                                                         (defined $opt_gestimate && $partialestimate{$hostpart} == 1) ||
891                                                         (defined $opt_failed && $dead_run != 0 && $partialestimate{$hostpart} == 1)) {
892                                                         printf "%8s ", $datestamp if defined $opt_date;
893                                                         printf "%-${maxnamelength}s", "$host:$qpartition";
894                                                         printf "%2d ",  $level{$hostpart};
895                                                         printf "%9d$unit", $esize{$hostpart};
896                                                         if($partialestimate{$hostpart} == 1) {
897                                                                 if ($dead_run) {
898                                                                         print " failed: killed while";
899                                                                         $exit_status |= $STATUS_FAILED;
900                                                                 }
901                                                                 print " partial";
902                                                         }
903                                                         print " estimate done\n";
904                                                 }
905                                                 $epartition++;
906                                                 $estsize += $esize{$hostpart};
907                                         }
908                                 }
909                         }
910                         else {
911                                 if(defined $estimate{$hostpart}) {
912                                         if($estimate{$hostpart} == 1) {
913                                                 $epartition++;
914                                                 $estsize += $esize{$hostpart};
915                                         }
916                                         elsif (!defined $dump_started{$hostpart} || $dump_started{$hostpart} == 0) {
917                                                 if( defined $opt_failed) {
918                                                         printf "%8s ", $datestamp if defined $opt_date;
919                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
920                                                         printf "           no estimate\n";
921                                                 }
922                                                 $exit_status |= $STATUS_FAILED;
923                                                 $fpartition++;
924                                                 $fsize+=$esize{$hostpart};
925                                         }
926                                 }
927                                 else {
928                                         $flpartition++;
929                                         $flsize += $size{$hostpart};
930                                         $in_flush=1;
931                                 }
932                                 if(defined $taper_started{$hostpart} &&
933                                                 $taper_started{$hostpart}==1 &&
934                                                 $dump_finished{$hostpart}!=-3) {
935                                         if(defined $dump_started{$hostpart} &&
936                                                 $dump_started{$hostpart} == 1 &&
937                                                         $dump_finished{$hostpart} == -1) {
938                                                 if(defined $opt_failed) {
939                                                         printf "%8s ", $datestamp if defined $opt_date;
940                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
941                                                         printf "%9d$unit", $esize{$hostpart};
942                                                         print " dump to tape failed: " . $error{$hostpart};
943                                                         print "\n";
944                                                 }
945                                                 $exit_status |= $STATUS_FAILED;
946                                                 $fpartition++;
947                                                 $fsize+=$esize{$hostpart};
948                                         } elsif(defined $dump_started{$hostpart} &&
949                                                 $dump_started{$hostpart} == 1 &&
950                                                         $dump_finished{$hostpart} == 0 &&
951                                                         $taper_started{$hostpart} == 1) {
952                                                 if( defined $opt_dumpingtape ||
953                                                          defined $opt_failed && $dead_run != 0) {
954                                                         printf "%8s ", $datestamp if defined $opt_date;
955                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
956                                                         printf "%9d$unit", $esize{$hostpart};
957                                                         if ($dead_run) {
958                                                                 print " failed: killed while";
959                                                                 $exit_status |= $STATUS_FAILED;
960                                                         }
961                                                         print " dumping to tape";
962                                                         $size = $tapedsize{$hostpart};
963                                                         if ($taper_status_file && -f $taper_status_file &&
964                                                                 open FF, "<$taper_status_file") {
965                                                                 $line = <FF>;
966                                                                 if (defined $line) {
967                                                                         chomp $line;
968                                                                         $value = $line / ($unitdivisor * 1024);
969                                                                         if ($value) {
970                                                                                 $size = $value if (!defined($size) || $value > $size);
971                                                                         }
972                                                                 }
973                                                                 close FF;
974                                                         }
975                                                         if(defined($size)) {
976                                                                 printf " (%d$unit done (%0.2f%%))", $size, 100.0 * $size/$esize{$hostpart};
977                                                                 $dtsize += $size;
978                                                         }
979                                                         if( defined $starttime ) {
980                                                                 print " (", &showtime($taper_time{$hostpart}), ")";
981                                                         }
982                                                         print "\n";
983                                                 }
984                                                 $dtpartition++;
985                                                 $dtesize += $esize{$hostpart};
986                                         }
987                                         elsif($taper_finished{$hostpart} == 0) {
988                                                 if( defined $opt_writingtape ||
989                                                          defined $opt_failed && $dead_run != 0) {
990                                                         printf "%8s ", $datestamp if defined $opt_date;
991                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
992                                                         printf "%9d$unit", $size{$hostpart};
993                                                         if ($dead_run) {
994                                                                 print " failed: killed while";
995                                                                 $exit_status |= $STATUS_FAILED;
996                                                         }
997                                                         if($in_flush == 0) {
998                                                                 print " writing to tape";
999                                                         }
1000                                                         else {
1001                                                                 print " flushing to tape";
1002                                                         }
1003                                                         $size = $tapedsize{$hostpart};
1004                                                         if ($taper_status_file &&  -f $taper_status_file &&
1005                                                                 open FF, "<$taper_status_file") {
1006                                                                 $line = <FF>;
1007                                                                 if (defined $line) {
1008                                                                         chomp $line;
1009                                                                         $value = $line / ($unitdivisor * 1024);
1010                                                                         if ($value) {
1011                                                                                 $size = $value if (!defined($size) || $value > $size);
1012                                                                         }
1013                                                                 }
1014                                                                 close FF;
1015                                                         }
1016                                                         if(defined($size)) {
1017                                                                 printf " (%d$unit done (%0.2f%%))", $size, 100.0 * $size/$size{$hostpart};
1018                                                         }
1019                                                         if( defined $starttime ) {
1020                                                                 print " (", &showtime($taper_time{$hostpart}), ")";
1021                                                         }
1022                                                         print ", ", $error{$hostpart} if (defined($error{$hostpart}) &&
1023                                                                                                                                             $error{$hostpart} ne "");
1024                                                         print "\n";
1025                                                 }
1026                                                 $tapartition++;
1027                                                 $tasize += $size{$hostpart};
1028                                                 if(defined $esize{$hostpart}) {
1029                                                         $taesize += $esize{$hostpart};
1030                                                 }
1031                                                 else {
1032                                                         $taesize += $size{$hostpart};
1033                                                 }
1034                                                 if (defined $dump_finished{$hostpart} && $dump_finished{$hostpart} == 1) {
1035                                                         $dpartition++;
1036                                                         $dsize += $size{$hostpart};
1037                                                         if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
1038                                                                 $desize += $esize{$hostpart};
1039                                                         } else {
1040                                                                 $desize += $size{$hostpart};
1041                                                         }
1042                                                 }
1043                                         }
1044                                         elsif($taper_finished{$hostpart} < 0) {
1045
1046                                                 if(defined $size{$hostpart}) {
1047                                                         $xsize = $size{$hostpart};
1048                                                 }
1049                                                 elsif(defined $esize{$hostpart}) {
1050                                                         $xsize = $esize{$hostpart};
1051                                                 }
1052                                                 else {
1053                                                         $xsize = 0;
1054                                                 }
1055
1056                                                 if(defined $esize{$hostpart}) {
1057                                                         $exsize += $esize{$hostpart};
1058                                                 }
1059                                                 else {
1060                                                         $exsize += $xsize;
1061                                                 }
1062
1063                                                 if( defined $opt_failed  ||
1064                                                          (defined $opt_waittaper && ($taper_finished{$hostpart} == -1))) {
1065                                                         printf "%8s ", $datestamp if defined $opt_date;
1066                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1067                                                         printf "%9d$unit", $xsize;
1068                                                         if($in_flush == 0) {
1069                                                                 print " failed to tape";
1070                                                         }
1071                                                         else {
1072                                                                 print " failed to flush";
1073                                                         }
1074                                                         print ": ",$error{$hostpart} if defined $error{$hostpart};
1075                                                 
1076                                                         print " (will retry)" unless $taper_finished{$hostpart} < -1;
1077                                                         if( defined $starttime ) {
1078                                                                 print " (", &showtime($taper_time{$hostpart}), ")";
1079                                                         }
1080                                                         print "\n";
1081                                                 }
1082                                                 $exit_status |= $STATUS_TAPE;
1083
1084                                                 $tfpartition++;
1085                                                 $tfsize += $xsize;
1086                                                 $tfesize += $exsize;
1087
1088                                                 if($in_flush == 0) {
1089                                                         $twpartition++;
1090                                                         $twsize += $xsize;
1091                                                         $twesize += $exsize;
1092                                                 }
1093                                                 else {
1094                                                         $wfpartition++;
1095                                                         $wfsize += $xsize;
1096                                                 }
1097                                                 if (defined $dump_finished{$hostpart} && $dump_finished{$hostpart} == 1) {
1098                                                         $dpartition++;
1099                                                         $dsize += $size{$hostpart};
1100                                                         if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
1101                                                                 $desize += $esize{$hostpart};
1102                                                         } else {
1103                                                                 $desize += $size{$hostpart};
1104                                                         }
1105                                                 }
1106                                         }
1107                                         elsif($taper_finished{$hostpart} == 1) {
1108                                                 if( defined $opt_finished ) {
1109                                                         printf "%8s ", $datestamp if defined $opt_date;
1110                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1111                                                         printf "%9d$unit", $size{$hostpart};
1112                                                         if($in_flush == 0) {
1113                                                                 print " finished";
1114                                                         }
1115                                                         else {
1116                                                                 print " flushed";
1117                                                         }
1118                                                         if( defined $starttime ) {
1119                                                                 print " (", &showtime($taper_time{$hostpart}), ")";
1120                                                         }
1121                                                         if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
1122                                                                 print ", PARTIAL";
1123                                                                 $exit_status |= $STATUS_FAILED;
1124                                                         }
1125                                                         print "\n";
1126                                                 }
1127                                                 if (defined $dump_finished{$hostpart} && $dump_finished{$hostpart} == 1) {
1128                                                         $dpartition++;
1129                                                         $dsize += $size{$hostpart};
1130                                                         if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
1131                                                                 $desize += $esize{$hostpart};
1132                                                         } else {
1133                                                                 $desize += $size{$hostpart};
1134                                                         }
1135                                                 }
1136                                                 $tpartition++;
1137                                                 $tsize += $size{$hostpart};
1138                                                 if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
1139                                                         $tesize += $esize{$hostpart};
1140                                                 }
1141                                                 else {
1142                                                         $tesize += $size{$hostpart};
1143                                                 }
1144                                         }
1145                                         else {
1146                                                 printf "%8s ", $datestamp if defined $opt_date;
1147                                                 printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1148                                                 print " unknown state TAPER\n";
1149                                         }
1150                                 }
1151                                 elsif(defined $dump_started{$hostpart}) {
1152                                         if($dump_started{$hostpart} == -1) {
1153                                                 if( defined $opt_failed ) {
1154                                                         printf "%8s ", $datestamp if defined $opt_date;
1155                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1156                                                         printf "failed: " . $error{$hostpart} . "\n";
1157                                                 }
1158                                                 $exit_status |= $STATUS_FAILED;
1159
1160                                                 $fpartition++;
1161                                                 $fsize+=$esize{$hostpart};
1162                                         }
1163                                         elsif($dump_started{$hostpart} == 0) {
1164                                                 if($estimate{$hostpart} == 1) {
1165                                                         if( defined $opt_waitdumping ) {
1166                                                                 printf "%8s ", $datestamp if defined $opt_date;
1167                                                                 printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1168                                                                 printf "%9d$unit", $esize{$hostpart};
1169                                                                 if ($dead_run) {
1170                                                                         print " failed: process terminated while";
1171                                                                         $exit_status |= $STATUS_FAILED;
1172                                                                 }
1173                                                                 print " waiting for dumping $error{$hostpart}\n";
1174                                                         }
1175                                                         if($driver_finished == 1) {
1176                                                                 $exit_status |= $STATUS_MISSING;
1177                                                         }
1178                                                         $wpartition++;
1179                                                         $wsize += $esize{$hostpart};
1180                                                 }
1181                                         }
1182                                         elsif($dump_started{$hostpart} == 1 &&
1183                                                         ($dump_finished{$hostpart} == -1 ||
1184                                                     $dump_finished{$hostpart} == -3)) {
1185                                                 if( defined $opt_failed ) {
1186                                                         printf "%8s ", $datestamp if defined $opt_date;
1187                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1188                                                         print "backup failed: ", $error{$hostpart};
1189                                                         if( defined $starttime ) {
1190                                                                 print " (", &showtime($dump_time{$hostpart}), ")";
1191                                                         }
1192                                                         print "\n";
1193                                                 }
1194                                                 $exit_status |= $STATUS_FAILED;
1195                                                 $fpartition++;
1196                                                 $fsize+=$esize{$hostpart};
1197                                         }
1198                                         elsif($dump_started{$hostpart} == 1 &&
1199                                                         $dump_finished{$hostpart} == 0) {
1200                                                 if( defined $opt_dumping ||
1201                                                          defined $opt_failed && $dead_run != 0) {
1202                                                         printf "%8s ", $datestamp if defined $opt_date;
1203                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1204                                                         printf "%9d$unit", $esize{$hostpart};
1205                                                         if ($dead_run) {
1206                                                                 print " failed: killed while";
1207                                                                 $exit_status |= $STATUS_FAILED;
1208                                                         }
1209                                                         printf " dumping %8d$unit", $size{$hostpart};
1210                                                         if($size{$hostpart} != 0) {
1211                                                                 printf " (%6.2f%%)", (100.0*$size{$hostpart})/$esize{$hostpart};
1212                                                         }
1213                                                         if( defined $starttime ) {
1214                                                                 print " (", &showtime($dump_time{$hostpart}), ")";
1215                                                         }
1216                                                         if(defined $dump_roomq{$hostpart}) {
1217                                                                 print " " . $error{$hostpart};
1218                                                         }
1219                                                         print "\n";
1220                                                 }
1221                                                 $dupartition++;
1222                                                 $dusize += $size{$hostpart};
1223                                                 $duesize += $esize{$hostpart};
1224                                         }
1225                                         elsif($dump_finished{$hostpart} == 1 &&
1226                                                         $taper_started{$hostpart} != 1) {
1227                                                 if( defined $opt_waittaper ) {
1228                                                         printf "%8s ", $datestamp if defined $opt_date;
1229                                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1230                                                         printf "%9d$unit", $size{$hostpart};
1231                                                         print " dump done";
1232                                                         if( defined $starttime ) {
1233                                                                 print " (", &showtime($dump_time{$hostpart}), ")";
1234                                                         }
1235                                                         print ",";
1236                                                         if ($dead_run) {
1237                                                                 print " process terminated while";
1238                                                         }
1239                                                         print " waiting for writing to tape";
1240                                                         if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
1241                                                                 print ", PARTIAL";
1242                                                                 $exit_status |= $STATUS_FAILED;
1243                                                         }
1244                                                         print "\n";
1245                                                 }
1246                                                 $dpartition++;
1247                                                 $dsize += $size{$hostpart};
1248                                                 $desize += $esize{$hostpart};
1249                                                 $twpartition++;
1250                                                 $twsize += $size{$hostpart};
1251                                                 $twesize += $esize{$hostpart};
1252                                         }
1253                                         else {
1254                                                 printf "%8s ", $datestamp if defined $opt_date;
1255                                                 printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1256                                                 print " unknown state DUMPER\n";
1257                                         }
1258                                 }
1259                                 elsif(defined $flush{$hostpart}) {
1260                                         if( defined $opt_waittaper ) {
1261                                                 printf "%8s ", $datestamp if defined $opt_date;
1262                                                 printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1263                                                 printf "%9d$unit", $size{$hostpart};
1264                                                 if ($dead_run) {
1265                                                         print " process terminated while";
1266                                                 }
1267                                                 print " waiting to flush";
1268                                                 if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
1269                                                         print ", PARTIAL";
1270                                                         $exit_status |= $STATUS_FAILED;
1271                                                 }
1272                                                 print "\n";
1273                                         }
1274                                         $wfpartition++;
1275                                         $wfsize += $size{$hostpart};
1276                                 }
1277                                 elsif(defined $level{$hostpart}) {
1278                                         printf "%8s ", $datestamp if defined $opt_date;
1279                                         printf "%-${maxnamelength}s%2d ", "$host:$qpartition", $level{$hostpart};
1280                                         print " unknown state\n";
1281                                 }
1282                         }
1283                 }
1284         }
1285 }
1286
1287 if (defined $opt_summary) {
1288         print "\n";
1289         print  "SUMMARY          part      real  estimated\n";
1290         print  "                           size       size\n";
1291         printf "partition       : %3d\n", $nb_partition;
1292         printf "estimated       : %3d %20d$unit\n", $epartition , $estsize;
1293         printf "flush           : %3d %9d$unit\n", $flpartition, $flsize;
1294         printf "failed          : %3d %20d$unit           (%6.2f%%)\n",
1295                 $fpartition , $fsize,
1296                 $estsize ? ($fsize * 1.0 / $estsize) * 100 : 0.0;
1297         printf "wait for dumping: %3d %20d$unit           (%6.2f%%)\n",
1298                 $wpartition , $wsize,
1299                 $estsize ? ($wsize * 1.0 / $estsize) * 100 : 0.0;
1300         if(defined($dtsize)) {
1301                 printf "dumping to tape : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1302                         $dtpartition, $dtsize, $dtesize,
1303                         $dtsize ? ($dtsize * 1.0 / $dtesize) * 100 : 0.0,
1304                         $estsize ? ($dtesize * 1.0 / $estsize) * 100 : 0.0;
1305         } else {
1306                 printf "dumping to tape : %3d %20d$unit           (%6.2f%%)\n",
1307                         $dtpartition, $dtesize,
1308                         $estsize ? ($dtesize * 1.0 / $estsize) * 100 : 0.0;
1309         }
1310         printf "dumping         : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1311                 $dupartition, $dusize, $duesize,
1312                 $duesize ? ($dusize * 1.0 / $duesize) * 100 : 0.0,
1313                 $estsize ? ($dusize * 1.0 / $estsize) * 100 : 0.0;
1314         printf "dumped          : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1315                 $dpartition , $dsize , $desize,
1316                 $desize ? ($dsize * 1.0 / $desize) * 100 : 0.0,
1317                 $estsize ? ($dsize * 1.0 / $estsize) * 100 : 0.0;
1318         printf "wait for writing: %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1319                 $twpartition, $twsize, $twesize,
1320                 $twesize ? ($twsize * 1.0 / $twesize) * 100 : 0.0,
1321                 $estsize ? ($twsize * 1.0 / $estsize) * 100 : 0.0;
1322         printf "wait to flush   : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1323                 $wfpartition, $wfsize, $wfsize, 100, 0;
1324         printf "writing to tape : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1325                 $tapartition, $tasize, $taesize,
1326                 $taesize ? ($tasize * 1.0 / $taesize) * 100 : 0.0,
1327                 $estsize ? ($tasize * 1.0 / $estsize) * 100 : 0.0;
1328         printf "failed to tape  : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1329                 $tfpartition, $tfsize, $tfesize,
1330                 $tfesize ? ($tfsize * 1.0 / $tfesize) * 100 : 0.0,
1331                 $estsize ? ($tfsize * 1.0 / $estsize) * 100 : 0.0;
1332         printf "taped           : %3d %9d$unit %9d$unit (%6.2f%%) (%6.2f%%)\n",
1333                 $tpartition , $tsize , $tesize,
1334                 $tesize ? ($tsize * 1.0 / $tesize) * 100 : 0.0,
1335                 ($estsize+$flsize) ? ($tsize * 1.0 / ($estsize + $flsize)) * 100 : 0.0;
1336         if($nb_tape > 1 || $tape_size != 0) {
1337                 for($i=1; $i <= $nb_tape; $i++) {
1338                         if($tape_size != 0) {
1339                                 printf "  tape %-3d      : %3d %9d$unit %9d$unit (%6.2f%%) %s",
1340                                         $i, $ntpartition{$i}, $ntsize{$i}, $ntesize{$i}, 100*$ntsize{$i}/$tape_size, $ntlabel{$i};
1341                         }
1342                         else {
1343                                 printf "  tape %-3d      : %3d %9d$unit %9d$unit %s",
1344                                         $i, $ntpartition{$i}, $ntsize{$i}, $ntesize{$i}, $ntlabel{$i};
1345                         }
1346                         if(defined($ntchunk{$i}) && $ntchunk{$i} > 0) {
1347                                 printf " (%d chunks)", $ntchunk{$i};
1348                         }
1349                         print "\n";
1350                 }
1351         }
1352         if($idle_dumpers == 0) {
1353                 printf "all dumpers active\n";
1354         }
1355         else {
1356                 $c1 = ($idle_dumpers == 1) ? "" : "s";
1357                 $c2 = ($idle_dumpers < 10) ? " " : "";
1358                 $c3 = ($idle_dumpers == 1) ? " " : "";
1359                 printf "%d dumper%s idle%s %s: %s\n", $idle_dumpers, $c1, $c2, $c3, $status_driver;
1360         }
1361
1362         printf "taper status: $status_taper\n";
1363         if (defined $qlen{"tapeq"}) {
1364                 printf "taper qlen: %d\n", $qlen{"tapeq"};
1365         }
1366         if (defined ($free{"kps"})) {
1367                 printf "network free kps: %9d\n", $free{"kps"};
1368         }
1369         if (defined ($free{"space"})) {
1370                 if ($holding_space) {
1371                         $hs = ($free{"space"} * 1.0 / $holding_space) * 100;
1372                 } else {
1373                         $hs = 0.0;
1374                 }
1375                 printf "holding space   : %9d$unit (%6.2f%%)\n", ($free{"space"}/$unitdivisor), $hs;
1376         }
1377 }
1378
1379 if(defined $opt_stats) {
1380         if(defined($current_time) && $current_time != $start_time) {
1381                 $total_time=$current_time-$start_time;
1382                 foreach $key (sort byprocess keys %busy_time) {
1383                         printf "%8s busy   : %8s  (%6.2f%%)\n",
1384                                 $key, &busytime($busy_time{$key}),
1385                                 ($busy_time{$key} * 1.0 / $total_time) * 100;
1386                 }
1387                 for ($d = 0; $d <= $#dumpers_active; $d++) {
1388                         $l = sprintf "%2d dumper%s busy%s : %8s  (%6.2f%%)",
1389                                 $d, ($d == 1) ? "" : "s", ($d == 1) ? " " : "",
1390                                 &busytime($dumpers_active[$d]),
1391                                 ($dumpers_active[$d] * 1.0 / $total_time) * 100;
1392                         print $l;
1393                         $s1 = "";
1394                         $s2 = " " x length($l);
1395                         $r = $dumpers_held[$d];
1396                         foreach $key (sort valuesort keys %$r) {
1397                                 next
1398                                   unless $dumpers_held[$d]{$key} >= 1;
1399                                 printf "%s%20s: %8s  (%6.2f%%)\n",
1400                                         $s1,
1401                                         $key,
1402                                         &busytime($dumpers_held[$d]{$key}),
1403                                         ($dumpers_held[$d]{$key} * 1.0 / $dumpers_active[$d]) * 100;
1404                                 $s1 = $s2;
1405                         }
1406                         if ($s1 eq "") {
1407                                 print "\n";
1408                         }
1409                 }
1410         }
1411 }
1412
1413 exit $exit_status;
1414
1415 sub make_hostpart() {
1416         local($host,$partition,$datestamp) = @_;
1417
1418         if(! defined($hosts{$host})) {
1419                 push @hosts, $host;
1420                 $hosts{$host}=1;
1421         }
1422         my($new_part) = 1;
1423         foreach $pp (sort @$host) {
1424                 $new_part = 0 if ($pp eq $partition);
1425         }
1426         push @$host, $partition if $new_part==1;
1427
1428         my($hostpart) = "$host$partition$datestamp";
1429         if(!defined $datestamp{$datestamp}) {
1430                 $datestamp{$datestamp} = 1;
1431                 push @datestamp, $datestamp;
1432         }
1433
1434         return $hostpart;
1435 }
1436
1437 sub byprocess() {
1438         my(@tmp_a) = split(/(\d*)$/, $a, 2);
1439         my(@tmp_b) = split(/(\d*)$/, $b, 2);
1440         return ($tmp_a[0] cmp $tmp_b[0]) || ($tmp_a[1] <=> $tmp_b[1]);
1441 }                               
1442  
1443 sub valuesort() {
1444         $r->{$b} <=> $r->{$a};
1445 }
1446
1447 sub dump_size() {
1448         local($filename) = @_;
1449         local($size);
1450         local($dsize) = 0;
1451         local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
1452                    $atime,$mtime,$ctime,$blksize,$blocks);
1453         while ($filename ne "") {
1454                 $filename = "$filename.tmp" if (!(-e "$filename"));
1455                 $filename = "/dev/null" if (!(-e "$filename"));
1456                 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
1457                                 $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
1458                 $size=$size-32768 if $size > 32768;
1459                 $dsize += $size;
1460                 open(DUMP,$filename);
1461                 $filename = "";
1462                 while(<DUMP>) {
1463                         if(/^CONT_FILENAME=(.*)$/) { $filename = $1; last }
1464                         last if /^To restore, position tape at start of file and run/;
1465                 }
1466                 close(DUMP);
1467         }
1468         return $dsize;
1469 }
1470
1471 sub unctime() {
1472         my (@MoY);
1473         my (@tl);
1474         my ($a);
1475         my ($m);
1476         my ($month);
1477         my ($time);
1478
1479         @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
1480                 'Jul','Aug','Sep','Oct','Nov','Dec');
1481
1482         # Preset an array of values in case some parts are not passed as
1483         # arguments.  This lets the date, etc, be omitted and default to
1484         # today.
1485
1486         @tl = localtime;
1487
1488         foreach $a (@_) {
1489                 next
1490                   if ($a eq '');
1491
1492                 # See if this argument looks like a month name.
1493
1494                 $month = 0;
1495                 foreach $m (@MoY) {
1496                         last
1497                           if ($m eq $a);
1498                         $month = $month + 1;
1499                 }
1500                 if ($month < 12) {
1501                         $tl[4] = $month;
1502                         next;
1503                 }
1504
1505                 # See if this is a day of the month.
1506
1507                 if ($a =~ /^\d+$/ && $a >= 1 && $a <= 32) {
1508                         $tl[3] = $a;
1509                         next;
1510                 }
1511
1512                 # See if the next argument looks like a time.
1513
1514                 if ($a =~ /^(\d+):(\d+)/) {
1515                         $tl[2] = $1;
1516                         $tl[1] = $2;
1517                         if ($a =~ /^(\d+):(\d+):(\d+)/) {
1518                                 $tl[0] = $3;
1519                         }
1520                         next;
1521                 }
1522
1523                 # See if this is a year.
1524
1525                 if ($a =~ /^\d\d\d\d$/ && $a >= 1900) {
1526                         $tl[5] = $a;
1527                         next;
1528                 }
1529         }
1530
1531         $time = &timelocal (@tl);
1532
1533         return $time;
1534 }
1535
1536 sub set_starttime() {
1537         my (@tl);
1538         my ($time);
1539         my ($date);
1540
1541         # Preset an array of values in case some parts are not passed as
1542         # arguments.  This lets the date, etc, be omitted and default to
1543         # today.
1544
1545         ($date)=@_;
1546         @tl = localtime;
1547
1548         $tl[5] = substr($date,  0, 4)   if(length($date) >= 4);
1549         $tl[4] = substr($date,  4, 2)-1 if(length($date) >= 6);
1550         $tl[3] = substr($date,  6, 2)   if(length($date) >= 8);
1551         $tl[2] = substr($date,  8, 2)   if(length($date) >= 10);
1552         $tl[1] = substr($date, 10, 2)   if(length($date) >= 12);
1553         $tl[0] = substr($date, 12, 2)   if(length($date) >= 14);
1554
1555         $time = &timelocal (@tl);
1556
1557         return $time;
1558 }
1559
1560
1561 sub showtime() {
1562         my($delta)=shift;
1563         my($oneday)=24*60*60;
1564
1565         @now=localtime($starttime+$delta);
1566         if($delta > $oneday) {
1567                 $result=sprintf("%d+",$delta/$oneday);
1568         } else {
1569                 $result="";
1570         }
1571         $result.=sprintf("%d:%02d:%02d",$now[2],$now[1],$now[0]);
1572         return $result;
1573 }
1574
1575 sub busytime() {
1576         my($busy)=shift;
1577         my($oneday)=24*60*60;
1578
1579         if($busy > $oneday) {
1580                 $days=int($busy/$oneday);
1581                 $result=sprintf("%d+",$busy/$oneday);
1582                 $busy-=$days*$oneday;
1583         } else {
1584                 $result="";
1585         }
1586         $hours=int($busy/60/60);
1587         $busy-=$hours*60*60;
1588         $minutes=int($busy/60);
1589         $busy-=$minutes*60;
1590         $seconds=$busy;
1591         $result.=sprintf("%d:%02d:%02d",$hours,$minutes,$seconds);
1592         return $result;
1593 }
1594