Imported Upstream version 2.5.1p3
[debian/amanda] / server-src / amstatus.pl.in
index f95d33c4a24bed99ccee4e4c93fad9087eebaf68..7edfb1065297e8033bd0fc842cfaeb7b182cc0d2 100644 (file)
@@ -9,6 +9,9 @@ eval '(exit $?0)' && eval 'exec @PERL@ -S $0 ${1+"$@"}'
 require "newgetopt.pl";
 use Time::Local;
 
+delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
+$ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin";
+
 $confdir="@CONFIG_DIR@";
 $prefix='@prefix@';
 $prefix=$prefix;               # avoid warnings about possible typo
@@ -16,6 +19,12 @@ $exec_prefix="@exec_prefix@";
 $exec_prefix=$exec_prefix;     # ditto
 $sbindir="@sbindir@";
 
+#$STATUS_STRANGE =  2;
+$STATUS_FAILED  =  4;
+$STATUS_MISSING =  8;
+$STATUS_TAPE    = 16;
+$exit_status    =  0;
+
 $USE_VERSION_SUFFIXES='@USE_VERSION_SUFFIXES@';
 $suf = '';
 if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
@@ -62,13 +71,15 @@ if ($conf =~ /^([\w.-]+)$/) {          # $1 is untainted
 }
 
 
+if ( ! -e "$confdir/$conf" ) {
+    print "Configuration directory '" . $confdir/$conf . "' doesn't exist\n";
+    exit 1;
+}
 if ( ! -d "$confdir/$conf" ) {
-    die "amstatus$suf: could not find directory $confdir/$conf";
+    print "Configuration directory '" . $confdir/$conf . "' is not a directory\n";
+    exit 1;
 }
 
-$oldPATH = $ENV{'PATH'};
-$ENV{'PATH'} = "/bin:/usr/bin:/usr/sbin:/sbin";       # force known path
-
 $pwd = `pwd`;
 chomp $pwd;
 chdir "$confdir/$conf";
@@ -105,7 +116,6 @@ if($nb_options == 0 ) {
 }
 
 $unit=`$sbindir/amgetconf$suf displayunit`;
-$ENV{'PATH'} = $oldPATH;
 chomp($unit);
 $unitdivisor=1;
 if($unit eq 'K') {
@@ -165,19 +175,23 @@ $ntpartition{$nb_tape} = 0;
 $ntsize{$nb_tape} = 0;
 $ntesize{$nb_tape} = 0;
 $tape_size = 0;
+$driver_finished = 0;
 
 while(<AMDUMP>) {
        chomp;
        if(/(amdump|amflush): start at (.*)/) {
                print " from $2\n";
-               $starttime=&unctime(split(/[    ]+/,$2));
        }
-       elsif(/amdump: datestamp (\S+)/) {
-               $gdatestamp = $1;
+       elsif(/(amdump|amflush): datestamp (\S+)/) {
+               $gdatestamp = $2;
                if(!defined $datestamp{$gdatestamp}) {
                        $datestamp{$gdatestamp} = 1;
                        push @datestamp, $gdatestamp;
                }
+               $starttime=&set_starttime($2);
+       }
+       elsif(/(amdump|amflush): starttime (\S+)/) {
+               $starttime=&set_starttime($2);
        }
        elsif(/planner: timestamp (\S+)/) {
                $gdatestamp = $1;
@@ -599,6 +613,7 @@ while(<AMDUMP>) {
                $taper_started{$hostpart}=1;
                $taper_finished{$hostpart}=0;
                $taper_time{$hostpart}=$1;
+               $ntchunk_size = 0;
        }
        #features (maybe missing features)
        elsif(/driver: send-cmd time (\S+) to taper: FILE-WRITE (\d+-\d+) (\S+) (\S+) (\S*) (\S+) (\d*) (\S+)/){
@@ -622,6 +637,7 @@ while(<AMDUMP>) {
                $taper_started{$hostpart}=1;
                $taper_finished{$hostpart}=0;
                $taper_time{$hostpart}=$1;
+               $ntchunk_size = 0;
        }
        elsif(/driver: send-cmd time (\S+) to taper: PORT-WRITE (\d+-\d+) (\S+) (\S+) \d+( \d+|)/){
                $current_time=$1;
@@ -633,6 +649,7 @@ while(<AMDUMP>) {
                $taper_started{$hostpart}=1;
                $taper_finished{$hostpart}=0;
                $taper_time{$hostpart}=$1;
+               $ntchunk_size = 0;
        }
        elsif(/driver: send-cmd time (\S+) to taper: PORT-WRITE (\d+-\d+) (\S+) (\S+) (\S+) \d+ \d+/){
                $current_time=$1;
@@ -645,6 +662,7 @@ while(<AMDUMP>) {
                $taper_started{$hostpart}=1;
                $taper_finished{$hostpart}=0;
                $taper_time{$hostpart}=$1;
+               $ntchunk_size = 0;
        }
        elsif(/driver: result time (\S+) from taper: (DONE|PARTIAL) (\d+-\d+) (\S+) (\d+) "?\[sec (\S+) kb (\d+) kps/) {
                $current_time=$1;
@@ -659,12 +677,12 @@ while(<AMDUMP>) {
                        $size{$hostpart}=$size;
                }
                $ntpartition{$nb_tape}++;
-               $ntsize{$nb_tape} += $size{$hostpart};
+               $ntsize{$nb_tape} += $size{$hostpart} - $ntchunk_size;
                if(defined $esize{$hostpart} && $esize{$hostpart} > 1) {
-                       $ntesize{$nb_tape} += $esize{$hostpart};
+                       $ntesize{$nb_tape} += $esize{$hostpart} - $ntchunk_size;
                }
                else {
-                       $ntesize{$nb_tape} += $size{$hostpart};
+                       $ntesize{$nb_tape} += $size{$hostpart} - $ntchunk_size;
                }
                if ($3 eq "PARTIAL") {
                        $partial{$hostpart} = 1;
@@ -672,6 +690,9 @@ while(<AMDUMP>) {
                else {
                        $partial{$hostpart} = 0;
                }
+               if($ntchunk_size > 0) {
+                       $ntchunk{$nb_tape}++;
+               }
        }
        elsif(/driver: result time (\S+) from taper: (TRY-AGAIN|TAPE-ERROR) (\d+-\d+) (.+)/) {
                $current_time=$1;
@@ -738,6 +759,16 @@ while(<AMDUMP>) {
                $ntpartition{$nb_tape} = 0;
                $ntsize{$nb_tape} = 0;
                $ntesize{$nb_tape} = 0;
+               $ntchunk{$nb_tape} = 0;
+       }
+       elsif(/splitting chunk that started at \d*kb after (\d*)kb/) {
+               $ntchunk{$nb_tape}++;
+               $ntsize{$nb_tape} += $1;
+               $ntesize{$nb_tape} += $1;
+               $ntchunk_size += $1;
+       }
+       elsif(/driver: FINISHED/) {
+               $driver_finished = 1;
        }
        else {
                #print "Ignoring: $_\n";
@@ -856,7 +887,9 @@ foreach $host (sort @hosts) {
                                                        printf "%-${maxnamelength}s%2d", "$host:$partition", $level{$hostpart};
                                                        printf "           no estimate\n";
                                                }
+                                               $exit_status |= $STATUS_FAILED;
                                                $fpartition++;
+                                               $fsize+=$esize{$hostpart};
                                        }
                                }
                                else {
@@ -877,7 +910,21 @@ foreach $host (sort @hosts) {
                                                $desize += $esize{$hostpart};
                                        }
                                        if(defined $dump_started{$hostpart} &&
-                                               $dump_started{$hostpart} == 1 &&
+                                                       $dump_started{$hostpart} == 1 &&
+                                                       $dump_finished{$hostpart} == -1) {
+                                               if(defined $opt_failed) {
+                                                       printf "%8s ", $datestamp if defined $opt_date;
+                                                       printf "%-${maxnamelength}s%2d", "$host:$partition", $level{$hostpart};
+                                                       printf "%9d$unit", $esize{$hostpart};
+                                                       print " dump to tape failed: " . $error{$hostpart};
+                                                       print "\n";
+                                               }
+                                               $exit_status |= $STATUS_FAILED;
+                                               $fpartition++;
+                                               $fsize+=$esize{$hostpart};
+                                       }
+                                       elsif(defined $dump_started{$hostpart} &&
+                                                       $dump_started{$hostpart} == 1 &&
                                                        $dump_finished{$hostpart} == 0 &&
                                                        $taper_started{$hostpart} == 1) {
                                                if( defined $opt_dumpingtape ) {
@@ -954,6 +1001,7 @@ foreach $host (sort @hosts) {
                                                        }
                                                        print "\n";
                                                }
+                                               $exit_status |= $STATUS_TAPE;
 
                                                $tfpartition++;
                                                $tfsize += $xsize;
@@ -983,8 +1031,10 @@ foreach $host (sort @hosts) {
                                                        if( defined $starttime ) {
                                                                print " (", &showtime($taper_time{$hostpart}), ")";
                                                        }
-                                                       print ", PARTIAL" if defined $partial{$hostpart} &&
-                                                                                                          $partial{$hostpart} == 1;
+                                                       if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
+                                                               print ", PARTIAL";
+                                                               $exit_status |= $STATUS_FAILED;
+                                                       }
                                                        print "\n";
                                                }
                                                $tpartition++;
@@ -1009,6 +1059,8 @@ foreach $host (sort @hosts) {
                                                        printf "%-${maxnamelength}s%2d", "$host:$partition", $level{$hostpart};
                                                        printf " " . $error{$hostpart} . "\n";
                                                }
+                                               $exit_status |= $STATUS_FAILED;
+                                               
                                                $fpartition++;
                                                $fsize+=$esize{$hostpart};
                                        }
@@ -1020,6 +1072,9 @@ foreach $host (sort @hosts) {
                                                                printf "%9d$unit", $esize{$hostpart};
                                                                print " wait for dumping $error{$hostpart}\n";
                                                        }
+                                                       if($driver_finished == 1) {
+                                                               $exit_status |= $STATUS_MISSING;
+                                                       }
                                                        $wpartition++;
                                                        $wsize += $esize{$hostpart};
                                                }
@@ -1035,6 +1090,7 @@ foreach $host (sort @hosts) {
                                                        }
                                                        print "\n";
                                                }
+                                               $exit_status |= $STATUS_FAILED;
                                                $fpartition++;
                                                $fsize+=$esize{$hostpart};
                                        }
@@ -1071,8 +1127,10 @@ foreach $host (sort @hosts) {
                                                                print " (", &showtime($dump_time{$hostpart}), ")";
                                                        }
                                                        print ", wait for writing to tape";
-                                                  print ", PARTIAL" if defined $partial{$hostpart} &&
-                                                                                                               $partial{$hostpart} == 1;;
+                                                       if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
+                                                               print ", PARTIAL";
+                                                               $exit_status |= $STATUS_FAILED;
+                                                       }
                                                        print "\n";
                                                }
                                                $dpartition++;
@@ -1094,8 +1152,10 @@ foreach $host (sort @hosts) {
                                                printf "%-${maxnamelength}s%2d", "$host:$partition", $level{$hostpart};
                                                printf "%9d$unit", $size{$hostpart};
                                                print " waiting to flush";
-                                               print ", PARTIAL" if defined $partial{$hostpart} &&
-                                                                                                  $partial{$hostpart} == 1;
+                                               if(defined $partial{$hostpart} && $partial{$hostpart} == 1) {
+                                                       print ", PARTIAL";
+                                                       $exit_status |= $STATUS_FAILED;
+                                               }
                                                print "\n";
                                        }
                                        $wfpartition++;
@@ -1156,8 +1216,12 @@ if (defined $opt_summary) {
        if($nb_tape > 1 || $tape_size != 0) {
                for($i=1; $i <= $nb_tape; $i++) {
                        if($tape_size != 0) {
-                               printf "  tape %-3d      : %3d %9d$unit %9d$unit (%6.2f%%) %s\n",
+                               printf "  tape %-3d      : %3d %9d$unit %9d$unit (%6.2f%%) %s",
                                        $i, $ntpartition{$i}, $ntsize{$i}, $ntesize{$i}, 100*$ntsize{$i}/$tape_size, $ntlabel{$i};
+                               if($ntchunk{$i} > 0) {
+                                       printf " (%d chunks)", $ntchunk{$i};
+                               }
+                               print "\n";
                        }
                        else {
                                printf "  tape %-3d      : %3d %9d$unit %9d$unit %s\n",
@@ -1227,6 +1291,8 @@ if(defined $opt_stats) {
        }
 }
 
+exit $exit_status;
+
 sub make_hostpart() {
        local($host,$partition,$datestamp) = @_;
 
@@ -1347,6 +1413,30 @@ sub unctime() {
        return $time;
 }
 
+sub set_starttime() {
+       my (@tl);
+       my ($time);
+       my ($date);
+
+       # Preset an array of values in case some parts are not passed as
+       # arguments.  This lets the date, etc, be omitted and default to
+       # today.
+
+       ($date)=@_;
+       @tl = localtime;
+
+       $tl[5] = substr($date,  0, 4)   if(length($date) >= 4);
+       $tl[4] = substr($date,  4, 2)-1 if(length($date) >= 6);
+       $tl[3] = substr($date,  6, 2)   if(length($date) >= 8);
+       $tl[2] = substr($date,  8, 2)   if(length($date) >= 10);
+       $tl[1] = substr($date, 10, 2)   if(length($date) >= 12);
+       $tl[0] = substr($date, 12, 2)   if(length($date) >= 14);
+
+       $time = &timelocal (@tl);
+
+       return $time;
+}
+
 sub showtime() {
        my($delta)=shift;
        my($oneday)=24*60*60;