script/robot-snapshot.pl untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 278 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2  
3 use strict;
4  
5 use Getopt::Std;
6  
7 use XML::XPath;
8 use XML::XPath::XMLParser;
9  
10 use NBU;
11  
12 my %opts;
13 getopts('d?ohispf:', \%opts);
14  
15 if ($opts{'?'}) {
16 print STDERR <<EOT;
17 Usage: robot-snapshot.pl [-s] [-hi] [-p] [-o] [-f <configfile>] [robot# [robot# ...]]
18 Options:
19 -s Provide robot content summary
20  
21 -h Preface each robot's listing with a header section
22 -i List full robot inventory
23  
24 -p Gather and display volume pool data
25  
26 -o List volume id of next volume to expire
27  
28 -f Provide alternate robot configuration control file
29 EOT
30 exit;
31 }
32  
33  
34 NBU->debug($opts{'d'});
35  
36 my $file = "/usr/local/etc/robot-conf.xml";
37 if (defined($opts{'f'})) {
38 $file = $opts{'f'};
39 die "No such configuration file: $file\n" if (! -f $file);
40 }
41  
42 my $xp;
43 if (-f $file) {
44 $xp = XML::XPath->new(filename => $file);
45 die "robot-snapshot.pl: Could not parse XML configuration file $file\n" unless (defined($xp));
46 }
47  
48 NBU::Media->populate(1);
49  
50 my @list;
51 if ($#ARGV > -1 ) {
52 for my $robotNumber (@ARGV) {
53 my $robot = NBU::Robot->byID($robotNumber);
54 push @list, $robot if (defined($robot));
55 }
56 }
57 else {
58 @list = (NBU::Robot->farm);
59 }
60  
61 for my $robot (@list) {
62  
63 next unless defined($robot);
64  
65 my $r = $robot->id;
66 my @l = $robot->slotList;
67  
68 my $prefix;
69 if ($opts{'h'}) {
70 print "Robot number $r";
71 if (defined($robot->host)) {
72 print " on ".$robot->host->name
73 }
74 else {
75 print " cannot be located!\n";
76 next;
77 }
78 print "\n";
79 $prefix = " ";
80 }
81 else {
82 $prefix = "$r\: ";
83 }
84  
85 my $volumeCount = 0;
86 my $netbackupCount = 0;
87 my $cleanCount = 0; my $cleanings = 0;
88 my %poolCount;
89 my %emptyCount; my %fullCount;
90 my %frozenCount; my %suspendedCount;
91  
92 my $firstExpiration;
93 for my $position (1..$robot->capacity) {
94 $position = sprintf("%03d", $position);
95 my $slot;
96 my $display = $opts{'i'};
97 my $comments = "";
98 if (my $volume = $l[$position]) {
99 $volumeCount += 1;
100 $slot = "$prefix$position\: ".$volume->id;
101 if (defined($volume)) {
102 if ($volume->netbackup) {
103 $netbackupCount += 1;
104 $slot .= " ".$volume->pool->name if ($opts{'p'});
105 $slot .= " *";
106 $poolCount{$volume->pool->name} += 1;
107 }
108 elsif (!$volume->cleaningTape) {
109 $slot .= " ".$volume->pool->name if ($opts{'p'});
110 if ($volume->allocated) {
111 $slot .= " ALLOCATED";
112 if ($volume->full) {
113 $slot .= " FULL";
114 $fullCount{$volume->pool->name} += 1;
115 if (!defined($firstExpiration) || ($volume->expires < $firstExpiration->expires)) {
116 $firstExpiration = $volume;
117 }
118 }
119 if ($volume->suspended) {
120 $slot .= " SUSPENDED";
121 $suspendedCount{$volume->pool->name} += 1;
122 }
123 if ($volume->frozen) {
124 $slot .= " FROZEN";
125 $frozenCount{$volume->pool->name} += 1;
126 }
127 if ($volume->expires < time) {
128 $slot .= " EXPIRED";
129 }
130 else {
131 $slot .= " expires ".substr(localtime($volume->expires), 4);
132 }
133 $slot .= " rl=".$volume->retention->level;
134 }
135 else {
136 $emptyCount{$volume->pool->name} += 1;
137 }
138 $poolCount{$volume->pool->name} += 1;
139 }
140 elsif ($volume->cleaningTape) {
141 $cleanCount += 1;
142 $cleanings += $volume->cleaningCount;
143 if ($volume->cleaningCount == 0) {
144 $display ||= 1;
145 $comments = " <-- No cleanings left!";
146 }
147  
148 }
149 }
150 }
151 else {
152 $slot = "$prefix$position\: <EMPTY>";
153 }
154 print "$slot$comments\n" if ($display);
155 }
156  
157 my $emptyCount = 0;
158 my $fullCount = 0;
159  
160 my ($nodeset, $robotConfig);
161 if (defined($xp)) {
162 $nodeset = $xp->find('//robot[@id=\''.$r.'\']');
163 $robotConfig = ($nodeset->size == 1) ? $nodeset->pop : undef;
164 }
165  
166 if (defined($robotConfig)) {
167 $nodeset = $robotConfig->find('pool[@name=\'NetBackup\']');
168 if ($nodeset->size == 1) {
169 my $constraint = $nodeset->pop;
170 my $target = $constraint->getAttribute('total');
171 if ((my $n = ($target - $netbackupCount)) > 0) {
172 print "${prefix} Add $n NetBackup volumes\n";
173 }
174 }
175 }
176  
177 foreach my $pool (keys %poolCount) {
178 my $poolSpecs;
179  
180 if (defined($robotConfig)) {
181 $nodeset = $robotConfig->find('pool[@name=\''.$pool.'\']');
182 if ($nodeset->size == 1) {
183 $poolSpecs = $nodeset->pop;
184 }
185 }
186  
187 my $total = sprintf("%3u", $poolCount{$pool});
188 my $empty = $emptyCount{$pool} += 0;
189 my $full = $fullCount{$pool} += 0;
190 my $partial = $total - $full - $empty;
191 my $frozen = $frozenCount{$pool} += 0;
192 my $suspended = $suspendedCount{$pool} += 0;
193  
194 if (defined($poolSpecs)) {
195  
196 print "${prefix}$total $pool\: $empty/$partial/$full\n";
197  
198 if ((my $limit = $poolSpecs->find('suspended | ancestor::*/suspended'))->size > 0) {
199 if ($suspended > (my $value = $limit->pop->string_value)) {
200 my $count = $suspended - $value;
201 print "${prefix} Remove $count suspended $pool volumes\n";
202 }
203 }
204 if ((my $limit = $poolSpecs->find('frozen | ancestor::*/frozen'))->size > 0) {
205 if ($frozen > (my $value = $limit->pop->string_value)) {
206 my $count = $frozen - $value;
207 print "${prefix} Remove $count frozen $pool volumes\n";
208 }
209 }
210 if ((my $limit = $poolSpecs->find('full'))->size == 1) {
211 if ($full > (my $value = $limit->pop->string_value)) {
212 my $count = $full - $value;
213 print "${prefix} Remove $count full $pool volumes\n";
214 }
215 }
216 if ((my $limit = $poolSpecs->find('empty'))->size == 1) {
217 if ($empty < (my $value = $limit->pop->string_value)) {
218 my $count = $value - $empty;
219 print "${prefix} Add $count empty $pool volumes\n";
220 }
221 }
222  
223 $emptyCount += $empty;
224 $fullCount += $full;
225 }
226 elsif (defined($robotConfig)) {
227 my $count = $total - 0;
228 print "${prefix}$total $pool\n";
229 print "${prefix} Remove $count disallowed $pool volumes\n";
230 }
231 else {
232 $emptyCount += $empty;
233 $fullCount += $full;
234 }
235 }
236  
237 if ($opts{'s'}) {
238 print "${prefix}$volumeCount out of ".$robot->capacity." occupied\n";
239 print "${prefix}$emptyCount completely empty volumes available\n";
240 print "${prefix}$cleanings cleanings left on $cleanCount cleaning volumes\n";
241 }
242 if ($opts{'o'} && defined($firstExpiration)) {
243 print "${prefix}Oldest full volume is ".$firstExpiration->id." expiring on ".localtime($firstExpiration->expires)."\n";
244 }
245 }
246  
247 =head1 NAME
248  
249 robot-snapshot.pl - Report and Analyze Tape Robot Contents
250  
251 =head1 SYNOPSIS
252  
253 robot-snapshot.pl [-s] [-hi] [-p] [-o] [-f <configfile>] [robot#1 [robot#2 ...]]
254  
255 =head1 DESCRIPTION
256  
257 Taking a snapshot of a robot involves taking an inventory of its tape volumes, optionally
258 listing them in robot slot number order, and commenting on the quantity of volumes in
259 various volume pools. The observations robot-snapshot.pl makes with regards to the various
260 volume pools are driven by a set of robot content rules defined in a configuration file. By
261 default this is the file /usr/local/etc/robot-conf.xml
262  
263 =head1 SEE ALSO
264  
265 =over 4
266  
267 =item L<volume-list.pl|volume-list.pl>, L<volume-status.pl|volume-status.pl>, L<scratch.pl|scratch.pl>
268  
269 =back
270  
271 =head1 AUTHOR
272  
273 Winkeler, Paul pwinkeler@pbnj-solutions.com
274  
275 =head1 COPYRIGHT
276  
277 Copyright (C) 2002 Paul Winkeler
278  
279 =cut