| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Getopt::Std; | ||
| 6 | use Time::Local; | ||
| 7 | |||
| 8 | my %opts; | ||
| 9 | getopts('d?ht:a:p:', \%opts); | ||
| 10 | |||
| 11 | if ($opts{'?'}) { | ||
| 12 | print STDERR <<EOT; | ||
| 13 | policy-space.pl [-a <as of date>] [-p <days in period>] [-t <schedule type>] [-h] | ||
| 14 | EOT | ||
| 15 | |||
| 16 | exit; | ||
| 17 | } | ||
| 18 | |||
| 19 | use NBU; | ||
| 20 | |||
| 21 | NBU->debug($opts{'d'}); | ||
| 22 | |||
| 23 | my $period = 1; | ||
| 24 | my ($mm, $dd, $yyyy); | ||
| 25 | if (!$opts{'a'}) { | ||
| 26 | my ($s, $m, $h, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(); | ||
| 27 | $year += 1900; | ||
| 28 | $mm = $mon + 1; | ||
| 29 | $dd = $mday; | ||
| 30 | $yyyy = $year; | ||
| 31 | } | ||
| 32 | else { | ||
| 33 | $opts{'a'} =~ /^([\d]{4})([\d]{2})([\d]{2})$/; | ||
| 34 | $mm = $2; | ||
| 35 | $dd = $3; | ||
| 36 | $yyyy = $1; | ||
| 37 | } | ||
| 38 | if ($opts{'p'}) { | ||
| 39 | $period = $opts{'p'}; | ||
| 40 | } | ||
| 41 | |||
| 42 | my ($midnightStart, $midnightEnd); | ||
| 43 | if ($period > 0) { | ||
| 44 | $midnightStart = timelocal(0, 0, 0, $dd, $mm-1, $yyyy); | ||
| 45 | $midnightEnd = $midnightStart + (24 * 60 * 60 * $period); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | $midnightEnd = timelocal(0, 0, 0, $dd, $mm-1, $yyyy); | ||
| 49 | $midnightStart = $midnightEnd + (24 * 60 * 60 * $period); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | NBU::Class->populate; | ||
| 54 | |||
| 55 | my @list; | ||
| 56 | if ($#ARGV > -1 ) { | ||
| 57 | for my $policyName (@ARGV) { | ||
| 58 | my $p = NBU::Class->byName($policyName); | ||
| 59 | push @list, $p if (defined($p)); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | @list = NBU::Class->list(); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $totalImages = 0; | ||
| 67 | my $totalSize = 0; | ||
| 68 | foreach my $p (@list) { | ||
| 69 | my @hl = (sort {$a->name cmp $b->name} $p->clients); | ||
| 70 | if (@hl) { | ||
| 71 | for my $h (@hl) { | ||
| 72 | my %totalHostImages; | ||
| 73 | my %totalHostSize; | ||
| 74 | foreach my $i (sort { $b->ctime <=> $a->ctime} $h->images) { | ||
| 75 | next if ($i->ctime < $midnightStart); | ||
| 76 | next if ($i->ctime > $midnightEnd); | ||
| 77 | |||
| 78 | my $st = $i->schedule->type; | ||
| 79 | next if ($opts{'t'} && ($st ne $opts{'t'})); | ||
| 80 | |||
| 81 | $totalHostImages{$st} += 1; | ||
| 82 | $totalHostSize{$st} += $i->size; | ||
| 83 | } | ||
| 84 | |||
| 85 | # | ||
| 86 | # Maintain running total for this entire scan | ||
| 87 | foreach my $k (keys %totalHostImages) { | ||
| 88 | $totalImages += $totalHostImages{$k}; | ||
| 89 | $totalSize += $totalHostSize{$k}; | ||
| 90 | } | ||
| 91 | |||
| 92 | # | ||
| 93 | # Add up sizes from all schedule types for this host | ||
| 94 | my $ths = 0; my $thi = 0; | ||
| 95 | foreach my $k (keys %totalHostImages) { | ||
| 96 | $thi += $totalHostImages{$k}; | ||
| 97 | $ths += $totalHostSize{$k}; | ||
| 98 | } | ||
| 99 | |||
| 100 | if ($opts{'h'}) { | ||
| 101 | my $hn = sprintf("%-15s", $h->name); | ||
| 102 | $ths = sprintf("%.2fGB", $ths / 1024 / 1024); | ||
| 103 | print "$hn: $thi images, $ths\n"; | ||
| 104 | if (!defined($opts{'t'})) { | ||
| 105 | foreach my $k (keys %totalHostImages) { | ||
| 106 | my $hs = sprintf("%.2fGB", $totalHostSize{$k} / 1024 / 1024); | ||
| 107 | my $hi = $totalHostImages{$k}; | ||
| 108 | print " $k: $hi images, $hs\n"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | $totalSize = sprintf("%.2fGB", $totalSize / 1024 / 1024); | ||
| 117 | print "Between ".localtime($midnightStart)." and ".localtime($midnightEnd).", "; | ||
| 118 | print "$totalImages backup images consumed $totalSize\n"; |