Imported Upstream version 3.2.0
[debian/amanda] / perl / Amanda / Report / postscript.pm
1 # Copyright (c) 2010 Zmanda, Inc.  All Rights Reserved.
2 #
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.
6 #
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
10 # for more details.
11 #
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
15 #
16 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
18
19 package Amanda::Report::postscript;
20
21 =head1 NAME
22
23 Amanda::Report::postscript -- postscript output for amreport
24
25 =head1 DESCRIPTION
26
27 This package implements the postscript output for amreport.  See amreport(8)
28 for more information.
29
30 =cut
31
32 use strict;
33 use warnings;
34
35 use Amanda::Constants;
36 use Amanda::Config qw( :getconf config_dir_relative );
37 use Amanda::Debug qw( debug );
38 use Amanda::Util;
39
40 sub new
41 {
42     my $class = shift;
43     my ($report, $config_name, $logfname) = @_;
44
45     my $self = bless {
46         report => $report,
47         config_name => $config_name,
48         logfname => $logfname,
49     }, $class;
50
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);
55
56     if ($ttyp && $tt) {
57
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);
62     }
63
64     # these values should never be zero, so assign defaults
65     $self->{'tapelen'} = $tapelen || 100 * 1024 * 1024;
66     $self->{'marksize'} = $marksize || 1 * 1024 * 1024;
67
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;
74
75     # get the template
76     $self->{'template'} = $self->_get_template($template_filename);
77
78     return $self;
79 }
80
81 sub write_report
82 {
83     my $self = shift;
84     my ($fh) = @_;
85
86     my $tape_labels = $self->{'report'}->get_program_info("taper", "tape_labels", []);
87     my $tapes = $self->{'report'}->get_program_info("taper", "tapes", {});
88
89     for my $label (@$tape_labels) {
90         my $tape = $tapes->{$label};
91         $self->_write_report_tape($fh, $label, $tape);
92     }
93 }
94
95 sub _write_report_tape
96 {
97     my $self = shift;
98     my ($fh, $label, $tape) = @_;
99
100     # function to quote string literals
101     sub psstr {
102         my ($str) = @_;
103         $str =~ s/([()\\])/\\$1/g;
104         return "($str)";
105     }
106
107     ## include the template once for each tape (might be overkill, but oh well)
108     print $fh $self->{'template'};
109
110     ## header stuff
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";
114
115     ## pre-calculate everything
116
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
120     my @first_parts;
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);
128
129         my $alldumps = $dle_info->{'dumps'};
130
131         while( my ($timestamp, $tries) = each %$alldumps ) {
132             # run once for each try for this DLE
133             foreach my $try (@$tries) {
134
135                 next unless exists $try->{taper};
136                 my $taper = $try->{taper};
137
138                 my $parts = $taper->{parts};
139                 next unless @$parts > 0;
140
141                 my $first_part = $parts->[0];
142                 my $dlename = undef;
143                 if ($first_part->{label} eq $label) {
144                     $dlename = $disk;
145                 } else { #find if one part is on the volume
146                     foreach $parts (@$parts) {
147                         if ($parts->{label} eq $label) {
148                             $dlename = '- ' . $disk;
149                             last;
150                         }
151                     }
152                     next if !defined $dlename;
153                 }
154
155                 my $filenum = $first_part->{file};
156
157                 # sum the part sizes on this label to get the outsize.  Note that
158                 # the postscript output does not contain a row for each part, but
159                 # for each part..
160                 my $outsize = 0;
161                 for my $part (@$parts) {
162                     next unless $part->{'label'} eq $label;
163                     $outsize += $part->{'kb'};
164                 }
165
166                 # Get origsize for this try.
167                 my $origsize = 0;
168                 my $level = -1;
169
170                 # TODO: this is complex and should probably be in a parent-class method
171                 if (exists $try->{dumper} and ($try->{dumper}{status} ne 'fail')) {
172                     my $try_dumper = $try->{dumper};
173                     $level    = $try_dumper->{level};
174                     $origsize = $try_dumper->{orig_kb};
175                 } else {    # we already know a taper run exists in this try
176                     $level = $taper->{level};
177                     $origsize = $taper->{orig_kb} if $taper->{orig_kb};
178                 }
179
180                 $total_outsize += $outsize;
181                 $total_origsize += $origsize;
182
183                 if ($outsize != $origsize) {
184                     $comp_outsize += $outsize;
185                     $comp_origsize += $origsize;
186                 }
187
188                 push @first_parts, [$host, $dlename, $level, $filenum, $origsize, $outsize];
189             }
190         }
191     }
192     # count filemarks in the tapeused assessment
193     my $tapeused = $tape->{'kb'};
194     $tapeused += $self->{'marksize'} * (1 + $tape->{'files'});
195
196     # sort @first_parts by filenum
197     @first_parts = sort { $a->[3] <=> $b->[3] } @first_parts;
198
199     ## output
200
201     print $fh psstr(sprintf('Total Size:        %6.1f MB', $tape->{kb} / 1024)),
202             " DrawStat\n";
203     print $fh psstr(sprintf('Tape Used (%%)       %4s %%',
204                                 $self->divzero($tapeused * 100, $self->{'tapelen'}))),
205             " DrawStat\n";
206     print $fh psstr(sprintf('Number of files:  %5s', $tape->{'files'})),
207             " DrawStat\n";
208     print $fh psstr(sprintf('Filesystems Taped: %4d', $tape->{dle})),
209             " DrawStat\n";
210
211     my $header = ["-", $label, "-", 0, 32, 32];
212     for my $ff ($header, @first_parts) {
213         my ($host, $name, $level, $filenum, $origsize, $outsize) = @$ff;
214         print $fh join(" ",
215                 psstr($host),
216                 psstr($name),
217                 psstr($level),
218                 psstr(sprintf("%3d", $filenum)),
219                 psstr(sprintf("%8s", $origsize || "")),
220                 psstr(sprintf("%8s", $outsize || "")),
221                 "DrawHost\n");
222     }
223
224     print $fh "\nshowpage\n";
225 }
226
227
228 # copy the user's configured template file into $fh
229 sub _get_template {
230     my $self = shift;
231     my ($filename) = @_;
232
233     $filename = config_dir_relative($filename) if $filename;
234
235     if (!$filename || !-r $filename) {
236         debug("could not open template file '$filename'");
237         return undef;
238     }
239
240     return Amanda::Util::slurp($filename);
241 }
242
243 # TODO: this should be a common function somewhere
244 sub divzero
245 {
246     my $self = shift;
247     my ( $a, $b ) = @_;
248     my $q;
249     return
250         ( $b == 0 )              ? "-- "
251       : ( ($q = $a / $b) > 99999.95 ) ? "#####"
252       : ( $q > 999.95 ) ? sprintf( "%5.0f", $q )
253       :                   sprintf( "%5.1f", $q );
254 }
255
256 1;