lintian doesn't like orphan packages with uploaders...
[debian/amanda] / server-src / amreport.pl
index b88ba7ceaa51cba2c6e7c3462c2fa45bb0e8d17a..b2913eb046580ba86b8d95f9cb0f6f9e687cf9eb 100755 (executable)
@@ -1,9 +1,10 @@
 #! @PERL@
-# Copyright (c) 2010 Zmanda, Inc.  All Rights Reserved.
+# Copyright (c) 2010-2012 Zmanda, Inc.  All Rights Reserved.
 #
-# This program is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 as published
-# by the Free Software Foundation.
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
@@ -67,7 +68,7 @@ my (@outputs, @output_queue);
 sub usage
 {
     print <<EOF;
-Usage: amreport conf [--version] [--help] [-o configoption]
+Usage: amreport [--version] [--help] [-o configoption] <conf>
   command-line mode options:
     [--log=logfile] [--ps=filename] [--text=filename] [--xml=filename]
     [--print=printer] [--mail-text=recipient]
@@ -276,38 +277,23 @@ sub legacy_send_amreport
     return 0 if ($cfg_send == $SEND_AMREPORT_NEVER);
 
     my $output_name = join(" ", @{ $output->[FORMAT] }, @{ $output->[OUTPUT] });
-    my ($send_amreport, $process_stranges, $process_fails) = (0, 0, 0);
+    my $send_amreport = 0;
 
     debug("testingamreport_send_on=$cfg_send, output:$output_name");
 
-    foreach my $dle ($report->get_dles()) {
-
-        my $dle_info = $report->get_dle_info(@$dle);
-        my $tries    = $dle_info->{tries};
-
-        foreach my $try (@$tries) {
-
-            foreach my $program (keys %$try) {
-
-                $process_stranges++ if $try->{$program}{status} eq 'strange';
-                $process_fails++    if $try->{$program}{status} eq 'fail';
-            }
-        }
-    }
-
     if ($cfg_send == $SEND_AMREPORT_STRANGE) {
 
         if (   !$report->get_flag("got_finish")
-            || ($report->get_flag("exit_status") != 0)
-            || $process_stranges
-            || $process_fails) {
+           || ($report->get_flag("dump_failed") != 0)
+           || ($report->get_flag("results_missing") != 0)
+           || ($report->get_flag("dump_strange") != 0)) {
 
-            debug("send_amreport_on=$cfg_send, condition filled for $output_name");
+            debug("send-amreport-on=$cfg_send, condition filled for $output_name");
             $send_amreport = 1;
 
         } else {
 
-            debug("send_amreport_on=$cfg_send, condition not filled for $output_name");
+            debug("send-amreport-on=$cfg_send, condition not filled for $output_name");
             $send_amreport = 0;
         }
 
@@ -315,14 +301,16 @@ sub legacy_send_amreport
 
         if (   !$report->get_flag("got_finish")
             || ($report->get_flag("exit_status") != 0)
-            || $process_fails) {
+            || ($report->get_flag("dump_failed") != 0)
+            || ($report->get_flag("results_missing") != 0)
+            || ($report->get_flag("dump_strange") != 0)) {
 
-            debug("send_amreport_on=$cfg_send, condition filled for $output_name");
+            debug("send-amreport-on=$cfg_send, condition filled for $output_name");
             $send_amreport = 1;
 
         } else {
 
-            debug("send_amreport_on=$cfg_send, condition not filled for $output_name");
+            debug("send-amreport-on=$cfg_send, condition not filled for $output_name");
             $send_amreport = 0;
         }
     }
@@ -360,8 +348,29 @@ sub open_printer_output
     debug("invoking printer: " . join(" ", @cmd));
 
     # redirect stdout/stderr to stderr, which is usually the amdump log
-    my $pid = open3( my $fh, ">&2", ">&2", @cmd)
-      or error("cannot start $cmd[0]: $!", 1);
+    my ($pid, $fh);
+    if (!-f $Amanda::Constants::LPR || !-x $Amanda::Constants::LPR) {
+       my $errstr = "error: the mailer '$Amanda::Constants::LPR' is not an executable program.";
+       print STDERR "$errstr\n";
+        if ($mode == MODE_SCRIPT) {
+            debug($errstr);
+        } else {
+            error($errstr, 1);
+        }
+    } else {
+       eval { $pid = open3($fh, ">&2", ">&2", @cmd); } or do {
+            ($pid, $fh) = (0, undef);
+            chomp $@;
+            my $errstr = "error: $@: $!";
+
+           print STDERR "$errstr\n";
+            if ($mode == MODE_SCRIPT) {
+               debug($errstr);
+            } else {
+               error($errstr, 1);
+            }
+        };
+    }
     return ($pid, $fh);
 }
 
@@ -371,12 +380,13 @@ sub open_mail_output
     my $mailto = $outputspec->[1];
 
     if ($mailto =~ /[*<>()\[\];:\\\/"!$|]/) {
-        error("mail address has invalid characters", 1);
+        error("mail addresses have invalid characters", 1);
     }
 
     my $datestamp =
       $report->get_program_info(
-        $report->get_flag("amflush_run") ? "amflush" : "planner", "start" );
+        $report->get_flag("amflush_run") ? "amflush" : 
+       $report->get_flag("amvault_run") ? "amvault" : "planner", "start" );
 
     $datestamp /= 1000000 if $datestamp > 99999999;
     $datestamp = int($datestamp);
@@ -386,51 +396,53 @@ sub open_mail_output
     my $date  = POSIX::strftime( '%B %e, %Y', 0, 0, 0, $day, $month, $year );
     $date =~ s/  / /g;
 
-    my $process_fails = 0;
-
-    foreach my $dle ($report->get_dles()) {
-       my $dle_info = $report->get_dle_info(@$dle);
-       my $tries    = $dle_info->{tries};
-
-       foreach my $try (@$tries) {
-           foreach my $program (keys %$try) {
-               $process_fails++    if $try->{$program}{status} eq 'fail';
-           }
-       }
-    }
-
     my $done = "";
     if (  !$report->get_flag("got_finish")
-       || ($report->get_flag("exit_status") != 0)
-       || $process_fails) {
+       || $report->get_flag("dump_failed") != 0) {
        $done = " FAIL:";
+    } elsif ($report->get_flag("results_missing") != 0) {
+       $done = " MISSING:";
+    } elsif ($report->get_flag("dump_strange") != 0) {
+       $done = " STRANGE:";
     }
 
     my $subj_str =
         getconf($CNF_ORG) . $done
-      . ( $report->get_flag("amflush_run") ? " AMFLUSH" : " AMANDA" )
+      . ( $report->get_flag("amflush_run") ? " AMFLUSH" :
+         $report->get_flag("amvault_run") ? " AMVAULT" : " AMANDA" )
       . " MAIL REPORT FOR "
       . $date;
 
     my $cfg_mailer = getconf($CNF_MAILER);
 
-    my @cmd = ("$cfg_mailer", "-s", $subj_str, $mailto);
+    my @cmd = ("$cfg_mailer", "-s", $subj_str, split(/ +/, $mailto));
     debug("invoking mail app: " . join(" ", @cmd));
 
 
     my ($pid, $fh);
-    eval { $pid = open3($fh, ">&2", ">&2", @cmd); 1; } or do {
-
-        ($pid, $fh) = (0, undef);
-        my $errstr =
-          "error: could not run command: " . join(" ", @cmd) . ": $@";
-
+    if (!-f $cfg_mailer || !-x $cfg_mailer) {
+       my $errstr = "error: the mailer '$cfg_mailer' is not an executable program.";
+       print STDERR "$errstr\n";
         if ($mode == MODE_SCRIPT) {
             debug($errstr);
         } else {
             error($errstr, 1);
         }
-    };
+       
+    } else {
+       eval { $pid = open3($fh, ">&2", ">&2", @cmd) } or do {
+            ($pid, $fh) = (0, undef);
+            chomp $@;
+            my $errstr = "error: $@: $!";
+
+           print STDERR "$errstr\n";
+            if ($mode == MODE_SCRIPT) {
+               debug($errstr);
+            } else {
+               error($errstr, 1);
+            }
+       };
+    }
 
     return ($pid, $fh);
 }
@@ -449,25 +461,26 @@ sub run_output {
        ($pid, $fh) = open_mail_output($report, $outputspec);
     }
 
-
     # TODO: add some generic error handling here.  must be compatible
     # with legacy behavior.
 
-    # TODO: modularize these better
-    if ($reportspec->[0] eq 'xml') {
-       print $fh $report->xml_output();
-    } elsif ($reportspec->[0] eq 'human') {
-       my $hr =
-         Amanda::Report::human->new( $report, $fh, $config_name, $opt_logfname );
-       $hr->print_human_amreport();
-    } elsif ($reportspec->[0] eq 'postscript') {
-       use Amanda::Report::postscript;
-       my $rep =
-         Amanda::Report::postscript->new( $report, $config_name, $opt_logfname );
-       $rep->write_report($fh);
-    }
+    if (defined $fh) {
+       # TODO: modularize these better
+       if ($reportspec->[0] eq 'xml') {
+           print $fh $report->xml_output("" . getconf($CNF_ORG), $config_name);
+       } elsif ($reportspec->[0] eq 'human') {
+           my $hr = Amanda::Report::human->new($report, $fh, $config_name,
+                                               $opt_logfname );
+           $hr->print_human_amreport();
+       } elsif ($reportspec->[0] eq 'postscript') {
+           use Amanda::Report::postscript;
+           my $rep = Amanda::Report::postscript->new($report, $config_name,
+                                                     $opt_logfname );
+           $rep->write_report($fh);
+       }
 
-    close $fh;
+       close $fh;
+    }
 
     # clean up any subprocess
     if (defined $pid) {
@@ -486,6 +499,7 @@ Amanda::Util::setup_application("amreport", "server", $CONTEXT_CMDLINE);
 
 my $config_overrides = new_config_overrides( scalar(@ARGV) + 1 );
 
+debug("Arguments: " . join(' ', @ARGV));
 Getopt::Long::Configure(qw/bundling/);
 GetOptions(
 
@@ -540,7 +554,7 @@ Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
 
 # read the tapelist
 my $tl_file = config_dir_relative(getconf($CNF_TAPELIST));
-my $tl = Amanda::Tapelist::read_tapelist($tl_file);
+my $tl = Amanda::Tapelist->new($tl_file);
 
 # read the disklist
 my $diskfile = config_dir_relative(getconf($CNF_DISKFILE));
@@ -567,11 +581,6 @@ if ($mode == MODE_CMDLINE) {
     calculate_legacy_outputs();
 }
 
-if (!@outputs) {
-    print "no output specified, nothing to do\n";
-    exit(0);
-}
-
 ## Parse the report & set output
 
 $report = Amanda::Report->new($logfile, $historical);