1 # Copyright (c) 2010 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19 package Amanda::Report::postscript;
23 Amanda::Report::postscript -- postscript output for amreport
27 This package implements the postscript output for amreport. See amreport(8)
35 use Amanda::Constants;
36 use Amanda::Config qw( :getconf config_dir_relative );
37 use Amanda::Debug qw( debug );
43 my ($report, $config_name, $logfname) = @_;
47 config_name => $config_name,
48 logfname => $logfname,
51 # get some other parameters we'll need
52 my $ttyp = getconf($CNF_TAPETYPE);
53 my $tt = lookup_tapetype($ttyp) if $ttyp;
54 my ($tapelen, $marksize, $template_filename);
58 # append null string to get the right context
59 $tapelen = "" . tapetype_getconf($tt, $TAPETYPE_LENGTH);
60 $marksize = "" . tapetype_getconf($tt, $TAPETYPE_FILEMARK);
61 $template_filename = "" . tapetype_getconf($tt, $TAPETYPE_LBL_TEMPL);
64 # these values should never be zero, so assign defaults
65 $self->{'tapelen'} = $tapelen || 100 * 1024 * 1024;
66 $self->{'marksize'} = $marksize || 1 * 1024 * 1024;
68 # TODO: this should be a shared method somewhere
69 my $timestamp = $report->get_timestamp();
70 my ($year, $month, $day) = ($timestamp =~ m/^(\d\d\d\d)(\d\d)(\d\d)/);
71 my $date = POSIX::strftime('%B %e, %Y', 0, 0, 0, $day, $month - 1, $year - 1900);
72 $date =~ s/ / /g; # get rid of intervening space
73 $self->{'datestr'} = $date;
76 $self->{'template'} = $self->_get_template($template_filename);
86 my $tape_labels = $self->{'report'}->get_program_info("taper", "tape_labels", []);
87 my $tapes = $self->{'report'}->get_program_info("taper", "tapes", {});
89 for my $label (@$tape_labels) {
90 my $tape = $tapes->{$label};
91 $self->_write_report_tape($fh, $label, $tape);
95 sub _write_report_tape
98 my ($fh, $label, $tape) = @_;
100 # function to quote string literals
103 $str =~ s/([()\\])/\\$1/g;
107 ## include the template once for each tape (might be overkill, but oh well)
108 print $fh $self->{'template'};
111 print $fh psstr($self->{'datestr'}), " DrawDate\n\n";
112 print $fh psstr("Amanda Version $Amanda::Constants::VERSION"), " DrawVers\n";
113 print $fh psstr($label), " DrawTitle\n";
115 ## pre-calculate everything
117 # make a list of the first part in each dumpfile, and at the same
118 # time count the origsize and outsize of every file beginning on this
119 # tape, and separate sums only of the compressed dumps
121 my $total_outsize = 0;
122 my $total_origsize = 0;
123 my $comp_outsize = 0;
124 my $comp_origsize = 0;
125 foreach my $dle ($self->{'report'}->get_dles()) {
126 my ($host, $disk) = @$dle;
127 my $dle_info = $self->{'report'}->get_dle_info($host, $disk);
129 # run once for each try for this DLE
130 foreach my $try (@{$dle_info->{'tries'}}) {
132 next unless exists $try->{taper};
133 my $taper = $try->{taper};
135 my $parts = $taper->{parts};
136 next unless @$parts > 0;
138 my $first_part = $parts->[0];
139 next unless $first_part->{label} eq $label;
141 my $filenum = $first_part->{file};
143 # sum the part sizes on this label to get the outsize. Note that
144 # the postscript output does not contain a row for each part, but
147 for my $part (@$parts) {
148 next unless $part->{'label'} eq $label;
149 $outsize += $part->{'kb'};
152 # Get origsize for this try.
156 # TODO: this is complex and should probably be in a parent-class method
157 if (exists $try->{dumper} and ($try->{dumper}{status} ne 'fail')) {
158 my $try_dumper = $try->{dumper};
159 $level = $try_dumper->{level};
160 $origsize = $try_dumper->{orig_kb};
161 } else { # we already know a taper run exists in this try
162 $level = $taper->{level};
163 $origsize = $taper->{orig_kb} if $taper->{orig_kb};
166 $total_outsize += $outsize;
167 $total_origsize += $origsize;
169 if ($outsize != $origsize) {
170 $comp_outsize += $outsize;
171 $comp_origsize += $origsize;
174 push @first_parts, [$host, $disk, $level, $filenum, $origsize, $outsize];
177 # count filemarks in the tapeused assessment
178 my $tapeused = $tape->{'kb'};
179 $tapeused += $self->{'marksize'} * (1 + $tape->{'files'});
181 # sort @first_parts by filenum
182 @first_parts = sort { $a->[3] <=> $b->[3] } @first_parts;
186 print $fh psstr(sprintf('Total Size: %6.1f MB', $tape->{kb} / 1024)),
188 print $fh psstr(sprintf('Tape Used (%%) %4s %%',
189 $self->divzero($tapeused * 100, $self->{'tapelen'}))),
191 print $fh psstr(sprintf('Number of files: %5s', $tape->{'files'})),
193 print $fh psstr(sprintf('Filesystems Taped: %4d', $tape->{dle})),
196 my $header = ["-", $label, "-", 0, 32, 32];
197 for my $ff ($header, @first_parts) {
198 my ($host, $name, $level, $filenum, $origsize, $outsize) = @$ff;
203 psstr(sprintf("%3d", $filenum)),
204 psstr(sprintf("%8s", $origsize || "")),
205 psstr(sprintf("%8s", $outsize || "")),
209 print $fh "\nshowpage\n";
213 # copy the user's configured template file into $fh
218 $filename = config_dir_relative($filename) if $filename;
220 if (!$filename || !-r $filename) {
221 debug("could not open template file '$filename'");
225 return Amanda::Util::slurp($filename);
228 # TODO: this should be a common function somewhere
236 : ( ($q = $a / $b) > 99999.95 ) ? "#####"
237 : ( $q > 999.95 ) ? sprintf( "%5.0f", $q )
238 : sprintf( "%5.1f", $q );