e4bda6e0b0a3d808a40d95b9aa0fb4489bdfbfe7
[debian/amanda] / server-src / amoverview.pl
1 #!@PERL@
2
3 # Catch for sh/csh on systems without #! ability.
4 eval '(exit $?0)' && eval 'exec @PERL@ -S $0 ${1+"$@"}'
5         & eval 'exec @PERL@ -S $0 $argv:q'
6                 if 0;
7
8 require 5.001;
9
10 use FileHandle;
11 use Getopt::Long;
12 use Text::ParseWords;
13 use Carp;
14 use POSIX;
15
16 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
17 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin";
18
19 sub Usage {
20     print STDERR <<END;
21 Usage: $0 [[-config] CONFIG] [-hostwidth width] [-diskwidth width] [-skipmissed] [-last] [-num0] [-togo0] [-verbose]
22
23 This script generates to standard output an overview of the filesystems
24 dumped over time and the type of dump done on a particular day, such as
25 a full dump, or an incremental, or if the dump failed.
26
27 You may override the default configuration `@DEFAULT_CONFIG@' by using
28 the -config command line option.  On larger installations, this script
29 will take a while to run.  In this case, run it with --verbose to see
30 how far along it is.
31 END
32     exit 1;
33 }
34
35 # Default paths for this installation of Amanda.
36 my $prefix='@prefix@';
37 $prefix=$prefix;                # avoid warnings about possible typo
38 my $exec_prefix="@exec_prefix@";
39 $exec_prefix=$exec_prefix;      # ditto
40 my $amlibexecdir="@amlibexecdir@";
41 my $sbindir="@sbindir@";
42  
43 # The directory where configurations can be found.
44 my $confdir="@CONFIG_DIR@";
45
46 # The default configuration.
47 my $config="@DEFAULT_CONFIG@";
48
49 my $amadmin     = "$sbindir/amadmin";
50
51 # overrideable defaults
52 my $opt_config          = "$config";
53 my $opt_hostwidth       = 8;
54 my $opt_diskwidth       = 20;
55 my $opt_skipmissed      = 0;
56 my $opt_last            = 0;
57 my $opt_num0            = 0;
58 my $opt_togo0           = 0;
59 my $opt_verbose         = 0;
60
61 GetOptions('config=s'           => \$opt_config,
62            'hostwidth=i'        => \$opt_hostwidth,
63            'diskwidth=i'        => \$opt_diskwidth,
64            'skipmissed'         => \$opt_skipmissed,
65            'last'               => \$opt_last,
66            'num0'               => \$opt_num0,
67            'togo0'              => \$opt_togo0,
68            'verbose'            => \$opt_verbose)
69 or Usage();
70
71 if($#ARGV == 0) {
72   $opt_config = $ARGV[0];
73 }
74 elsif($#ARGV > 0) {
75   Usage();
76 }
77
78 #untaint user input $ARGV[0]
79
80 if ($opt_config =~ /^([\w.-]+)$/) {          # $1 is untainted
81    $opt_config = $1;
82 } else {
83     die "filename '$opt_config' has invalid characters.\n";
84 }
85
86
87 -d "$confdir/$opt_config" or
88         die "$0: directory `$confdir/$opt_config' does not exist.\n";
89
90 # read disklist
91 my %disks = ();
92 $::host = '';
93 $::disk = '';
94 $opt_verbose and
95     print STDERR "Running $amadmin $opt_config disklist\n";
96 my $dlfh = new FileHandle "$amadmin $opt_config disklist|" or
97     die "$0: error in opening `$amadmin $opt_config disklist' pipe: $!\n";
98 $/ = "";
99 while (<$dlfh>) {
100     ($host, $disk) = m/    host (.*?):\n.*    disk (.*?):\n.*strategy (STANDARD|NOFULL|NOINC|HANOI|INCRONLY).*ignore NO/ms;
101     next unless $host;
102     $disks{$host}{$disk}++;
103 }
104
105 $/ = "\n";
106 $dlfh->close or
107     die "$0: error in closing `$amadmin $opt_config disklist|' pipe: $!\n";
108
109 # Get backup dates
110 %::dates = ();
111 %::level = ();
112 $::level = '';
113 my ($date, $tape, $file, $status);
114 $opt_verbose and
115     print STDERR "Running $amadmin $opt_config find\n";
116 my $fh = new FileHandle "$amadmin $opt_config find|" or
117     die "$0: error in opening `$amadmin $opt_config find' pipe: $!\n";
118 <$fh>;
119 while (<$fh>) {
120     chomp;
121     next if /found Amanda directory/;
122     next if /skipping cruft directory/;
123     next if /skip-incr/;
124
125     ($date, $time, $host, $disk, $level, $tape, $file, $part, $status, $remaining) = shellwords($_);
126
127     next if $date eq 'date';
128     next if $date eq 'Warning:';
129     next if $date eq 'Scanning';
130     next if $date eq "";
131
132     $status .= " " . $remaining;
133     if($time !~/^\d\d:\d\d:\d\d$/) {
134         $status = $part;
135         $part = $file;
136         $file = $tape;
137         $tape = $level;
138         $level = $disk;
139         $disk = $host;
140         $host = $time;
141     }
142     next if ($part != 1);
143
144     if ($date =~ /^\d\d\d\d-\d\d-\d\d$/) {
145         if(defined $disks{$host}{$disk}) {
146             defined($level{$host}{$disk}{$date}) or
147                 $level{$host}{$disk}{$date} = '';
148             $level{$host}{$disk}{$date} .= ($status eq 'OK') ? $level : 'E';
149             $dates{$date}++;
150         }
151     }
152     else {
153         print "bad date $date in $_\n";
154     }
155 }
156 $fh->close or
157     die "$0: error in closing `$amadmin $opt_config find|' pipe: $!\n";
158
159 # Process the status to arrive at a "last" status
160 if ($opt_last) {
161     for $host (sort keys %disks) {
162         for $disk (sort keys %{$disks{$host}}) {
163             $level{$host}{$disk}{"0000-LA-ST"} = '';
164             for $date (sort keys %dates) {
165                 if ($level{$host}{$disk}{$date} eq "E"
166                      && $level{$host}{$disk}{"0000-LA-ST"} =~ /^\d/ ) {
167                     $level{$host}{$disk}{"0000-LA-ST"} .= $level{$host}{$disk}{$date};
168                 } elsif ($level{$host}{$disk}{$date} eq "") {
169                     $level{$host}{$disk}{"0000-LA-ST"} =~ s/E//;
170                 } else {
171                     $level{$host}{$disk}{"0000-LA-ST"} = $level{$host}{$disk}{$date};
172                 }
173             }
174         }
175     }
176 }
177
178 # Number of level 0 backups
179 if ($opt_num0) {
180     for $host (sort keys %disks) {
181         for $disk (sort keys %{$disks{$host}}) {
182             $level{$host}{$disk}{'0000-NM-L0'} = 0;
183             for $date (sort keys %dates) {
184                 if ($level{$host}{$disk}{$date} =~ /0/) {
185                     $level{$host}{$disk}{'0000-NM-L0'} += 1;
186                 }
187             }
188         }
189     }
190 }
191
192 # Runs to the last level 0
193 if ($opt_togo0) {
194     for $host (sort keys %disks) {
195         for $disk (sort keys %{$disks{$host}}) {
196             $level{$host}{$disk}{'0000-TO-GO'} = 0;
197             my $togo=0;
198             for $date (sort keys %dates) {
199                 if ($level{$host}{$disk}{$date} =~ /0/) {
200                     $level{$host}{$disk}{'0000-TO-GO'} = $togo;
201                 }
202                 $togo++;
203             }
204         }
205     }
206 }
207
208 unless ($opt_skipmissed)
209 # touch all the dates just in case whole days were missed.
210 {
211     my ($start, $finish) = 
212         map {
213             my($y,$m,$d) = split /-/, $_;
214             POSIX::mktime(0,0,0,$d,$m-1,$y-1900);
215         } (sort keys %dates)[0,-1];
216
217     while ($start < $finish) {
218         my @l = localtime $start;
219         $dates{sprintf("%d-%02d-%02d", 1900+$l[5], $l[4]+1, $l[3])}++;
220         $start += 86400;
221     }
222 }
223
224 #Add the "last" entry    
225 $dates{"0000-LA-ST"}=1 if ($opt_last);
226
227 #Add the "Number of Level 0s" entry
228 $dates{"0000-NM-L0"}=1 if ($opt_num0);
229
230 #Add the "Runs to go" entry
231 $dates{"0000-TO-GO"}=1 if ($opt_togo0);
232
233 # make formats
234
235 my $top_format = "format TOP =\n\n" .
236     sprintf("%-0${opt_hostwidth}s %-0${opt_diskwidth}s ", '', 'date') .
237     join(' ', map((split(/-/, $_))[1], sort keys %dates)) . "\n" .
238     sprintf("%-0${opt_hostwidth}s %-0${opt_diskwidth}s ", 'host', 'disk') .
239     join(' ', map((split(/-/, $_))[2], sort keys %dates)) . "\n" .
240     "\n.\n";
241
242 my $out_format = "format STDOUT =\n" .
243     "@" . "<" x ($opt_hostwidth - 1) . ' ' .
244     "@" . "<" x ($opt_diskwidth - 1) . ' ' .
245     '@> ' x scalar(keys %dates) . "\n" .
246     join(', ', '$host', '$disk', 
247          map("substr(\$level{\$host}{\$disk}{'$_'},-2)", sort keys %dates)) . "\n" .
248     ".\n";
249
250 eval $top_format;
251 die $@ if $@;
252 $^ = 'TOP';
253 eval $out_format;
254 die $@ if $@;
255
256 for $host (sort keys %disks) {
257     for $disk (sort keys %{$disks{$host}}) {
258         write;
259     }
260 }