Imported Upstream version 3.2.0
[debian/amanda] / server-src / amoverview.pl
1 #!@PERL@
2 # Copyright (c) 2010 Zmanda Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use lib '@amperldir@';
21 use strict;
22 use warnings;
23
24 use Amanda::Config qw( :init :getconf );
25 use Amanda::Disklist;
26 use Amanda::DB::Catalog;
27 use FileHandle;
28 use Getopt::Long;
29 use Carp;
30 use POSIX;
31
32 sub Usage {
33     print STDERR <<END;
34 Usage: $0 [[--config] CONFIG] [--hostwidth width] [--diskwidth width]
35           [-skipmissed] [--last] [--num0] [--togo0] [--verbose]
36
37 This script generates to standard output an overview of the filesystems
38 dumped over time and the type of dump done on a particular day, such as
39 a full dump, or an incremental, or if the dump failed.
40
41 You may override the default configuration `@DEFAULT_CONFIG@' by using
42 the --config command line option.  On larger installations, this script
43 will take a while to run.  In this case, run it with --verbose to see
44 how far along it is.
45 END
46     exit 1;
47 }
48
49 # overrideable defaults
50 my $opt_config          = undef;
51 my $opt_hostwidth       = 8;
52 my $opt_diskwidth       = 20;
53 my $opt_skipmissed      = 0;
54 my $opt_last            = 0;
55 my $opt_num0            = 0;
56 my $opt_togo0           = 0;
57 my $opt_verbose         = 0;
58
59 GetOptions('config=s'           => \$opt_config,
60            'hostwidth=i'        => \$opt_hostwidth,
61            'diskwidth=i'        => \$opt_diskwidth,
62            'skipmissed'         => \$opt_skipmissed,
63            'last'               => \$opt_last,
64            'num0'               => \$opt_num0,
65            'togo0'              => \$opt_togo0,
66            'verbose'            => \$opt_verbose)
67 or Usage();
68
69 if(@ARGV == 1) {
70   $opt_config = $ARGV[0];
71 } else {
72   Usage();
73 }
74
75 #Initialize configuration
76 config_init($CONFIG_INIT_EXPLICIT_NAME, $opt_config);
77 my ($cfgerr_level, @cfgerr_errors) = config_errors();
78 if ($cfgerr_level >= $CFGERR_WARNINGS) {
79     config_print_errors();
80     if ($cfgerr_level >= $CFGERR_ERRORS) {
81         die("errors processing config file");
82     }
83 }
84
85 # read disklist
86 $cfgerr_level = Amanda::Disklist::read_disklist();
87 die("Config errors") if ($cfgerr_level >= $CFGERR_WARNINGS);
88
89 my %disks = ();
90 foreach my $dle (Amanda::Disklist::all_disks()) {
91     $disks{$dle->{"host"}->{"hostname"}}{$dle->{"name"}}++;
92 }
93
94 # Get dumps
95 my %dates = ();
96 my %level = ();
97 my ($date, $host, $disk);
98 $opt_verbose and
99     print STDERR "Processing $opt_config dumps\n";
100 foreach my $dump (Amanda::DB::Catalog::sort_dumps(['hostname','diskname','write_timestamp'],Amanda::DB::Catalog::get_dumps())) {
101     $host = $dump->{"hostname"};
102     $disk = $dump->{"diskname"};
103     $date = substr($dump->{"dump_timestamp"},0,8);
104
105     if (defined $disks{$host}{$disk}) {
106         defined($level{$host}{$disk}{$date}) or
107             $level{$host}{$disk}{$date} = '';
108         $level{$host}{$disk}{$date} .= ($dump->{"status"} eq 'OK') ? $dump->{"level"} : 'E';
109         $dates{$date}++;
110     }
111 }
112
113 # Process the status to arrive at a "last" status
114 if ($opt_last) {
115     for $host (sort keys %disks) {
116         for $disk (sort keys %{$disks{$host}}) {
117             $level{$host}{$disk}{"0000LAST"} = '';
118             for $date (sort keys %dates) {
119                 next unless defined($level{$host}{$disk}{$date});
120                 if ($level{$host}{$disk}{$date} eq "E"
121                      && $level{$host}{$disk}{"0000LAST"} =~ /^\d/ ) {
122                     $level{$host}{$disk}{"0000LAST"} .= $level{$host}{$disk}{$date};
123                 } else {
124                     $level{$host}{$disk}{"0000LAST"} = $level{$host}{$disk}{$date};
125                 }
126             }
127         }
128     }
129 }
130
131 # Number of level 0 backups
132 if ($opt_num0) {
133     for $host (sort keys %disks) {
134         for $disk (sort keys %{$disks{$host}}) {
135             $level{$host}{$disk}{'0000NML0'} = 0;
136             for $date (sort keys %dates) {
137                 if (defined($level{$host}{$disk}{$date})
138                         && $level{$host}{$disk}{$date} =~ /0/) {
139                     $level{$host}{$disk}{'0000NML0'} += 1;
140                 }
141             }
142         }
143     }
144 }
145
146 # Runs to the last level 0
147 if ($opt_togo0) {
148     for $host (sort keys %disks) {
149         for $disk (sort keys %{$disks{$host}}) {
150             $level{$host}{$disk}{'0000TOGO'} = 0;
151             my $togo=0;
152             for $date (sort keys %dates) {
153                 if (defined($level{$host}{$disk}{$date})
154                         && $level{$host}{$disk}{$date} =~ /0/) {
155                     $level{$host}{$disk}{'0000TOGO'} = $togo;
156                 }
157                 $togo++;
158             }
159         }
160     }
161 }
162
163 unless ($opt_skipmissed)
164 # touch all the dates just in case whole days were missed.
165 {
166     my ($start, $finish) = 
167         map {
168             my($y,$m,$d) = /(....)(..)(..)/;
169             POSIX::mktime(0,0,0,$d,$m-1,$y-1900);
170         } (sort keys %dates)[0,-1];
171
172     # Special case of only one date
173     if (defined($finish)) {
174         while ($start < $finish) {
175             my @l = localtime $start;
176             $dates{sprintf("%d%02d%02d", 1900+$l[5], $l[4]+1, $l[3])}++;
177             $start += 86400;
178         }
179     }
180 }
181
182 #Add the "last" entry    
183 $dates{"0000LAST"}=1 if ($opt_last);
184
185 #Add the "Number of Level 0s" entry
186 $dates{"0000NML0"}=1 if ($opt_num0);
187
188 #Add the "Runs to go" entry
189 $dates{"0000TOGO"}=1 if ($opt_togo0);
190
191 # make formats
192
193 sub row {
194     my ($host, $disk, @cols) = @_;
195     $host = substr($host, 0, $opt_hostwidth);
196     $disk = substr($disk, 0, $opt_diskwidth);
197     print
198         sprintf("%-0${opt_hostwidth}s %-0${opt_diskwidth}s ", $host, $disk) .
199         join(" ", map(sprintf("%-2s ", $_), @cols)) .
200         "\n";
201 }
202
203 sub fmt_levels {
204     my ($host, $disk, $date) = @_;
205     my $levels = $level{$host}{$disk}{$date};
206
207     # no dumps on this date
208     $levels = '-' unless defined($levels);
209
210     return substr($levels, -2);
211 }
212
213 # header
214 row('',     'date', map((/....(..) .. /x)[0], sort keys %dates));
215 row('host', 'disk', map((/.... .. (..)/x)[0], sort keys %dates));
216
217 # body
218 for $host (sort keys %disks) {
219     for $disk (sort keys %{$disks{$host}}) {
220         row($host, $disk,
221             map(fmt_levels($host, $disk, $_), sort keys %dates));
222     }
223 }