dc303d17474331149e94d8e790307c21329047c7
[debian/amanda] / server-src / amoverview.pl.in
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 Carp;
13 use POSIX;
14
15 sub Usage {
16     print STDERR <<END;
17 Usage: $0 [[-config] CONFIG] [-hostwidth width] [-diskwidth width] [-skipmissed] [-verbose]
18
19 This script generates to standard output an overview of the filesystems
20 dumped over time and the type of dump done on a particular day, such as
21 a full dump, or an incremental, or if the dump failed.
22
23 You may override the default configuration `@DEFAULT_CONFIG@' by using
24 the -config command line option.  On larger installations, this script
25 will take a while to run.  In this case, run it with --verbose to see
26 how far along it is.
27 END
28     exit 1;
29 }
30
31 # Default paths for this installation of Amanda.
32 my $prefix='@prefix@';
33 $prefix=$prefix;                # avoid warnings about possible typo
34 my $exec_prefix="@exec_prefix@";
35 $exec_prefix=$exec_prefix;      # ditto
36 my $libexecdir="@libexecdir@";
37 my $sbindir="@sbindir@";
38  
39 # The directory where configurations can be found.
40 my $confdir="@CONFIG_DIR@";
41
42 # The default configuration.
43 my $config="@DEFAULT_CONFIG@";
44
45 # Get the version suffix.
46 my $USE_VERSION_SUFFIXES = '@USE_VERSION_SUFFIXES@';
47 my $suf = '';
48 if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
49         $suf='-@VERSION@';
50 }
51
52 my $amadmin     = "$sbindir/amadmin$suf";
53
54 # overrideable defaults
55 my $opt_config          = "$config";
56 my $opt_hostwidth       = 8;
57 my $opt_diskwidth       = 20;
58 my $opt_skipmissed      = 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            'verbose'            => \$opt_verbose)
66 or Usage();
67
68 if($#ARGV == 0) {
69   $opt_config = $ARGV[0];
70 }
71 elsif($#ARGV > 0) {
72   Usage();
73 }
74
75 -d "$confdir/$opt_config" or
76         die "$0: directory `$confdir/$opt_config' does not exist.\n";
77
78 # read disklist
79 my %disks = ();
80 $::host = '';
81 $::disk = '';
82 $opt_verbose and
83     print STDERR "Running $amadmin $opt_config disklist\n";
84 my $dlfh = new FileHandle "$amadmin $opt_config disklist|" or
85     die "$0: error in opening `$amadmin $opt_config disklist' pipe: $!\n";
86 $/ = "";
87 while (<$dlfh>) {
88     ($host, $disk) = m/    host (.*?):.*    disk (.*?):/s;
89     next unless $host;
90     $disks{$host}{$disk}++;
91 }
92
93 $/ = "\n";
94 $dlfh->close or
95     die "$0: error in closing `$amadmin $opt_config disklist|' pipe: $!\n";
96
97 # Get backup dates
98 %::dates = ();
99 %::level = ();
100 $::level = '';
101 my ($date, $tape, $file, $status);
102 $opt_verbose and
103     print STDERR "Running $amadmin $opt_config find\n";
104 my $fh = new FileHandle "$amadmin $opt_config find|" or
105     die "$0: error in opening `$amadmin $opt_config find' pipe: $!\n";
106 <$fh>;
107 while (<$fh>) {
108     chomp;
109     next if /found Amanda directory/;
110     next if /skipping cruft directory/;
111     next if /skip-incr/;
112     ($date, $host, $disk, $level, $tape, $file, $status) = split ' ', $_;
113     next if $date eq 'date';
114     next if $date eq 'Warning:';
115     next if $date eq 'Scanning';
116     next if $date eq "";
117     if ($date =~ /^\d\d\d\d-\d\d-\d\d$/) {
118         defined($level{$host}{$disk}{$date}) or
119             $level{$host}{$disk}{$date} = '';
120         $level{$host}{$disk}{$date} .= ($status eq 'OK') ? $level : 'E';
121         $dates{$date}++;
122     }
123     else {
124         print "bad date $date in $_\n";
125     }
126 }
127 $fh->close or
128     die "$0: error in closing `$amadmin $opt_config find|' pipe: $!\n";
129
130 # Process the status to arrive at a "last" status
131 for $host (sort keys %disks) {
132     for $disk (sort keys %{$disks{$host}}) {
133         $level{$host}{$disk}{"0000-LA-ST"} = '';
134         for $date (sort keys %dates) {
135             if ($level{$host}{$disk}{$date} eq "E"
136                  && $level{$host}{$disk}{"0000-LA-ST"} =~ /^\d/ ) {
137                 $level{$host}{$disk}{"0000-LA-ST"} .= $level{$host}{$disk}{$date};
138             } elsif ($level{$host}{$disk}{$date} eq "") {
139                 $level{$host}{$disk}{"0000-LA-ST"} =~ s/E//;
140             } else {
141                 $level{$host}{$disk}{"0000-LA-ST"} = $level{$host}{$disk}{$date};
142             }
143         }
144     }
145 }
146
147 unless ($opt_skipmissed)
148 # touch all the dates just in case whole days were missed.
149 {
150     my ($start, $finish) = 
151         map {
152             my($y,$m,$d) = split /-/, $_;
153             POSIX::mktime(0,0,0,$d,$m-1,$y-1900);
154         } (sort keys %dates)[0,-1];
155
156     while ($start < $finish) {
157         my @l = localtime $start;
158         $dates{sprintf("%d-%02d-%02d", 1900+$l[5], $l[4]+1, $l[3])}++;
159         $start += 86400;
160     }
161 }
162
163 #Add the "last" entry    
164 $dates{"0000-LA-ST"}=1;
165
166 # make formats
167
168 my $top_format = "format TOP =\n\n" .
169     sprintf("%-0${opt_hostwidth}s %-0${opt_diskwidth}s ", '', 'date') .
170     join(' ', map((split(/-/, $_))[1], sort keys %dates)) . "\n" .
171     sprintf("%-0${opt_hostwidth}s %-0${opt_diskwidth}s ", 'host', 'disk') .
172     join(' ', map((split(/-/, $_))[2], sort keys %dates)) . "\n" .
173     "\n.\n";
174
175 my $out_format = "format STDOUT =\n" .
176     "@" . "<" x ($opt_hostwidth - 1) . ' ' .
177     "@" . "<" x ($opt_diskwidth - 1) . ' ' .
178     '@> ' x scalar(keys %dates) . "\n" .
179     join(', ', '$host', '$disk', 
180          map("substr(\$level{\$host}{\$disk}{'$_'},-2)", sort keys %dates)) . "\n" .
181     ".\n";
182
183 eval $top_format;
184 die $@ if $@;
185 $^ = 'TOP';
186 eval $out_format;
187 die $@ if $@;
188
189 for $host (sort keys %disks) {
190     for $disk (sort keys %{$disks{$host}}) {
191         write;
192     }
193 }