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