| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use XML::XPath; | ||
| 6 | use XML::XPath::XMLParser; | ||
| 7 | |||
| 8 | use Getopt::Std; | ||
| 9 | |||
| 10 | my %opts; | ||
| 11 | getopts('d?nf:', \%opts); | ||
| 12 | |||
| 13 | if ($opts{'?'}) { | ||
| 14 | print STDERR <<EOT; | ||
| 15 | Usage: scratch.pl [-n] [-f <config.xml>] | ||
| 16 | Options: | ||
| 17 | -n Don't actually scratch volumes; only list them | ||
| 18 | -f Use alternate config file | ||
| 19 | EOT | ||
| 20 | exit; | ||
| 21 | } | ||
| 22 | |||
| 23 | use NBU; | ||
| 24 | NBU->debug($opts{'d'}); | ||
| 25 | |||
| 26 | my $file = "/usr/local/etc/robot-conf.xml"; | ||
| 27 | if (defined($opts{'f'})) { | ||
| 28 | $file = $opts{'f'}; | ||
| 29 | die "No such configuration file: $file\n" if (! -f $file); | ||
| 30 | } | ||
| 31 | |||
| 32 | my $xp; | ||
| 33 | if (-f $file) { | ||
| 34 | $xp = XML::XPath->new(filename => $file); | ||
| 35 | die "robot-snapshot.pl: Could not parse XML configuration file $file\n" unless (defined($xp)); | ||
| 36 | } | ||
| 37 | |||
| 38 | # | ||
| 39 | # Rather than XPath-ing on every volume, we build a little hash of the density/pool | ||
| 40 | # combinations we're allowed to scratch. | ||
| 41 | my %itchy; | ||
| 42 | my $nodeset = $xp->find('//itchy/pool'); | ||
| 43 | foreach my $pool ($nodeset->get_nodelist) { | ||
| 44 | my $poolName = $pool->getAttribute('name'); | ||
| 45 | my $nodeset = $pool->find('density'); | ||
| 46 | foreach my $density ($nodeset->get_nodelist) { | ||
| 47 | my $densityCode = $density->getAttribute('code'); | ||
| 48 | my $key = $densityCode.":".$poolName; | ||
| 49 | $itchy{$key} += 1; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | my $scratch = NBU::Pool->scratch; | ||
| 54 | die "No scratch pool defined\n" unless (defined($scratch)); | ||
| 55 | |||
| 56 | NBU::Media->populate(1); | ||
| 57 | my $tc = 0; | ||
| 58 | my $sc = 0; | ||
| 59 | for my $m (NBU::Media->list) { | ||
| 60 | next if ($m->cleaningTape); | ||
| 61 | $tc += 1; | ||
| 62 | next if ($m->allocated); | ||
| 63 | next if ($m->netbackup); | ||
| 64 | |||
| 65 | next if (defined($m->pool) && !exists($itchy{$m->type.":".$m->pool->name})); | ||
| 66 | |||
| 67 | $sc += 1; | ||
| 68 | if ($opts{'n'}) { | ||
| 69 | print "Could scratch ".$m->id." from ".$m->pool->name."\n"; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $m->pool($scratch); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | printf("Scratched $sc volumes (%.2f%%)\n", ($sc * 100) / $tc); | ||
| 76 | |||
| 77 | =head1 NAME | ||
| 78 | |||
| 79 | scratch.pl - Return empty volumes back to scratch pool | ||
| 80 | |||
| 81 | =head1 SYNOPSIS | ||
| 82 | |||
| 83 | scratch.pl [-n] [-f <config.xml>] | ||
| 84 | |||
| 85 | =head1 DESCRIPTION | ||
| 86 | |||
| 87 | Although NetBackup will move volumes from the scratch pool (vmpool -listscratch) to | ||
| 88 | whatever pool it deems necessary, once the images on these volumes expire the volumes | ||
| 89 | are not automatically returned to the scratch pool. Scratch.pl addresses this need | ||
| 90 | by locating all unassigned volumes NOT in the scratch pool, matching them up against | ||
| 91 | a set of rules gleaned from a configuration file and returning back to the scratch | ||
| 92 | pool. | ||
| 93 | |||
| 94 | =head1 OPTIONS | ||
| 95 | |||
| 96 | =over 4 | ||
| 97 | |||
| 98 | =item B<-n> | ||
| 99 | |||
| 100 | Don't actually return the volumes to the scratch pool, just list which ones are eligible | ||
| 101 | to be returned. | ||
| 102 | |||
| 103 | =item B<-f> config.xml | ||
| 104 | |||
| 105 | The default configuration file is /usr/local/etc/robot-conf.xml but this option allows you | ||
| 106 | to override this with a selection of your own. | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =head1 FILES | ||
| 111 | |||
| 112 | As alluded to above, the return of volumes to the scratch pool is controlled via | ||
| 113 | a configuration file, the default being /usr/local/etc/robot-conf.xml. This is an XML | ||
| 114 | file and in particular, scratch.pl concerns itself with the the element called <itchy/> | ||
| 115 | Here is an example of such an element: | ||
| 116 | |||
| 117 | <itchy> | ||
| 118 | <pool name="NetBackup"> | ||
| 119 | <density code="11"/> | ||
| 120 | <density code="14"/> | ||
| 121 | </pool> | ||
| 122 | <pool name="MAXpool"> | ||
| 123 | <density code="11"/> | ||
| 124 | <density code="14"/> | ||
| 125 | </pool> | ||
| 126 | <pool name="SAP_Production"> | ||
| 127 | <density code="14"/> | ||
| 128 | </pool> | ||
| 129 | </itchy> | ||
| 130 | |||
| 131 | Each <pool/> element sets the rules volumes in that pool have | ||
| 132 | to obey in order to be returned to the scratch pool. In the above example, | ||
| 133 | only SAP_Production volumes of density "14" (i.e. hcart2) will be | ||
| 134 | scratched whereas dlt volumes are left alone. | ||
| 135 | |||
| 136 | =head1 SEE ALSO | ||
| 137 | |||
| 138 | =over 4 | ||
| 139 | |||
| 140 | =item L<volume-list.pl|volume-list.pl> | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =head1 AUTHOR | ||
| 145 | |||
| 146 | Winkeler, Paul pwinkeler@pbnj-solutions.com | ||
| 147 | |||
| 148 | =head1 COPYRIGHT | ||
| 149 | |||
| 150 | Copyright (C) 2002 Paul Winkeler | ||
| 151 | |||
| 152 | =cut |