script/js.pl untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 427 CC 6 SCAR 21.1
CC
6
Cov
0%
sortBySpeed
20.0
dispInterval
6.0
sortByClient
6.0
CRAP 8.3
1 #!/usr/local/bin/perl -w
2  
3 use strict;
4  
5 use POSIX;
6 use Getopt::Std;
7 use Time::Local;
8  
9 my %opts;
10 getopts('d?xveflrj:aAt:p:c:o:C:O:M:', \%opts);
11  
12 if ($opts{'?'}) {
13 print STDERR <<EOT;
14 Usage: js.pl [-v] [-r|-l] [-j <jobfile>] [-ef] [-aA] [-o <order>]
15 [-x]
16 [-t <policy-type>] [-c <policy>] [-C <client>] [-O <OS>]
17 [-M <master>]
18 Options:
19 -v Verbose policy listing
20  
21 -l Log this monitoring session
22 -r Replay previous job monitoring session
23 -j Provide alternate job history session data
24  
25 -e More detailed error information
26 -f List files directed to be backed up
27  
28 -a List all jobs from the last 24 hours
29 -A List all jobs.
30  
31 -o Order jobs by one of id, client or speed
32  
33 -x Produce xml output
34  
35 -t Restrict listing to jobs of <policy-type> type
36 -c Only list policies matching the provided pattern
37 -C Restrict listing to clients matching the provided pattern
38 -O Control which client OS jobs are presented
39  
40 -M Alternate NetBackup <master> server
41 EOT
42 exit;
43 }
44  
45 use NBU;
46 NBU->debug($opts{'d'});
47  
48 my $master;
49 if ($opts{'M'}) {
50 $master = NBU::Host->new($opts{'M'});
51 }
52 else {
53 my @masters = NBU->masters; $master = $masters[0];
54 }
55  
56 sub dispInterval {
57 my $i = shift;
58  
59 return "--:--:--" if (!defined($i));
60  
61 my $seconds = $i % 60; $i = int($i / 60);
62 my $minutes = $i % 60;
63 my $hours = int($i / 60);
64  
65 my $fmt = sprintf("%02d", $seconds);
66 $fmt = sprintf("%02d:", $minutes).$fmt;
67 $fmt = sprintf("%02d:", $hours).$fmt;
68 return $fmt;
69 }
70 my $period = 1;
71 if ($opts{'p'}) {
72 $period = $opts{'p'};
73 }
74  
75  
76 sub sortBySpeed {
77 my $result = 0;
78 my $aSpeed = $a->success ? ($a->dataWritten / $a->elapsedTime) : -1;
79 my $bSpeed = $b->success ? ($b->dataWritten / $b->elapsedTime) : -1;
80  
81 $result = ($bSpeed <=> $aSpeed);
82 $result = ($b->id <=> $a->id) if ($result == 0);
83 return $result;
84 }
85  
86 sub sortByClient {
87 my $result;
88  
89 $result = ($a->client->name cmp $b->client->name);
90 $result = ($b->id <=> $a->id) if ($result == 0);
91 return $result;
92 }
93  
94 sub sortByID {
95 my $result;
96  
97 $result = ($b->id <=> $a->id);
98 return $result;
99 }
100  
101 my $sortOrder = \&sortByID;
102 if ($opts{'o'}) {
103 $sortOrder = \&sortByClient if ($opts{'o'} eq 'client');
104 $sortOrder = \&sortBySpeed if ($opts{'o'} eq 'speed');
105 }
106  
107  
108 my %stateCodes = (
109 'active' => 'A',
110 'done' => 'D',
111 'queued' => 'Q',
112 're-queued' => 'R',
113 'incomplete' => 'I',
114 );
115  
116 my $totalWritten = 0;
117  
118 $opts{'r'} = 1 if ($opts{'j'});
119  
120 my $asOf = NBU::Job->loadJobs($master, $opts{'r'}, $opts{'l'}, ($opts{'j'} ? $opts{'j'} : undef));
121 my $mm; my $dd; my $yyyy;
122 my ($s, $m, $h, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($asOf);
123 $year += 1900;
124 $mm = $mon + 1;
125 $dd = $mday;
126 $yyyy = $year;
127 #my $since = timelocal(0, 0, 0, $dd, $mm-1, $yyyy);
128 my $since = $asOf - ($period * (24 * 60 * 60));
129  
130  
131 my $CLIENTWIDTH = 25;
132  
133 if ($opts{'x'}) {
134 print "<?xml version=\"1.0\"?>\n";
135 print "<job-list>\n";
136 }
137 else {
138 my $hdr = sprintf("%${CLIENTWIDTH}s", "CLIENT ");
139 if ($opts{'v'}) {
140 $hdr .= " ".sprintf("%-40s", " CLASS/SCHEDULE");
141 }
142 else {
143 $hdr .= " ".sprintf("%-23s", " CLASS");
144 }
145 $hdr .= " ".sprintf("%9s", " JOBID ");
146 $hdr .= " ".sprintf("%8s", " START ");
147 $hdr .= " ".sprintf("%1s", "R");
148 $hdr .= " ".sprintf("%-10s", " STU");
149 $hdr .= " ".sprintf("%3s", "OP");
150 $hdr .= " ".sprintf("%-8s", " TIME");
151 $hdr .= " ".sprintf("%-7s", " FILES");
152 $hdr .= " ".sprintf("%-10s", " KBYTES");
153 $hdr .= " ".sprintf("%4s", "SPD");
154 print "$hdr\n";
155 }
156  
157 NBU::Class->populate if ($opts{'t'});
158  
159 my $jobCount = 0;
160 my %activeClients;
161 my $MBytes = 0;
162  
163 my @jl = NBU::Job->list;
164 for my $job (sort $sortOrder (@jl)) {
165 next if (!$opts{'a'} && !$job->active);
166 next if (!$opts{'A'} && ($job->start < $since));
167  
168 # Skip jobs of the wrong ilk
169 my $fits = !($opts{'t'} || $opts{'c'} || $opts{'C'} || $opts{'O'});
170 if (!$fits) {
171 $fits ||= (defined($job->class) && defined($job->class->type) && ($job->class->type =~ $opts{'t'})) if (!$fits && $opts{'t'});
172 $fits ||= (defined($job->class) && ($job->class->name =~ $opts{'c'})) if (!$fits && $opts{'c'});
173 $fits ||= (defined($job->client) && ($job->client->name =~ $opts{'C'})) if (!$fits && $opts{'C'});
174 $fits ||= (defined($job->client) && defined($job->client->os) && ($job->client->os =~ $opts{'O'})) if (!$fits && $opts{'O'});
175 }
176 next if (!$fits);
177  
178 {
179 $jobCount += 1;
180  
181 my $who = $job->client->name;
182 $activeClients{$who} += 1;
183 $who = sprintf("%${CLIENTWIDTH}s", $who) unless ($opts{'x'});
184  
185 my $policyName = my $classID = $job->class->name;
186 my $scheduleName = $job->schedule->name;
187 my $classIDlength = 23;
188 if ($opts{'v'}) {
189 $classID .= "/".$scheduleName;
190 $classIDlength = 40;
191 }
192 $classID = sprintf("%-".$classIDlength."s", $classID);
193  
194 my $jid = $job->id;
195 $jid = sprintf("%7u", $job->id) unless ($opts{'x'});
196 my $try = defined($job->try) ? $job->try : 0;
197 my $state = $stateCodes{$job->state};
198  
199  
200 if ($opts{'x'}) {
201 my $startTime = substr(localtime($job->start), 4);
202 print " <job id=\"$jid\"";
203 print " try=\"$try\"" if ($try);
204 print " policy=\"$policyName\" schedule=\"$scheduleName\" client=\"$who\" start=\"$startTime\"";
205 }
206 else {
207 my $startTime = ((time - $job->start) < (24 * 60 * 60)) ?
208 substr(localtime($job->start), 11, 8) :
209 " ".substr(localtime($job->start), 4, 6)." ";
210 print "$who $classID ${jid}-${try} $startTime $state";
211 }
212  
213 if (my $stu = $job->storageUnit) {
214 if ($opts{'x'}) {
215 print " storageunit=\"".$stu->label."\"";
216 }
217 else {
218 printf(" %10s ", $stu->label);
219 }
220 }
221 else {
222 printf(" %10s ", "") unless ($opts{'x'});
223 }
224  
225 if ($state eq "D") {
226 if ($opts{'x'}) {
227 print " exitcode=\"".$job->status."\" elapsed=\"".dispInterval($job->elapsedTime)."\"";
228 }
229 else {
230 printf(" %3d ", $job->status);
231 print dispInterval($job->elapsedTime);
232 }
233 if ($job->status == 0) {
234 if (defined($job->dataWritten)) {
235 $totalWritten += ($job->dataWritten / 1024);
236 $MBytes += $job->dataWritten / 1024;
237 }
238 }
239 }
240 elsif ($state eq "A") {
241 my $op = $job->operation;
242 if ($opts{'x'}) {
243 print " operation=\"$op\" elapsed=\"".dispInterval($job->elapsedTime)."\"";
244 }
245 else {
246 print " $op ".dispInterval($job->busy);
247 }
248 }
249  
250 if ($state ne "Q") {
251 if (defined($job->filesWritten)) {
252 if ($opts{'x'}) {
253 print " files=\"".$job->filesWritten."\"";
254 }
255 else {
256 printf(" %7d", $job->filesWritten);
257 }
258 }
259 else {
260 printf(" 7%s", "") unless ($opts{'x'});
261 }
262 if (defined($job->dataWritten)) {
263 if ($opts{'x'}) {
264 print " kbytes=\"".$job->dataWritten."\"";
265 }
266 else {
267 printf(" %10d", $job->dataWritten);
268 }
269 }
270 else {
271 printf(" 10%s", "") unless ($opts{'x'});
272 }
273 if (($job->elapsedTime > 0) && defined($job->dataWritten)) {
274 my $speed = sprintf("%.2f", ($job->dataWritten / $job->elapsedTime / 1024));
275 if ($opts{'x'}) {
276 print " speed=\"$speed\"";
277 }
278 else {
279 print " $speed";
280 }
281 }
282  
283 if (($state eq "A") && ($job->volume)) {
284 print " ".$job->volume->id if (!$opts{'x'});
285 }
286 }
287  
288 print ">" if ($opts{'x'});
289  
290 if ($opts{'f'}) {
291 for my $f ($job->files) {
292 next if ($f =~ /NEW_STREAM/);
293 if ($opts{'x'}) {
294 print "\n <file name=\"$f\"/>";
295 }
296 else {
297 print "\n $f";
298 }
299 }
300 }
301 if ($opts{'e'}) {
302 my @el = $job->errors;
303 for my $e (@el) {
304 my $tm = $$e{tod};
305 my $msg = $$e{message};
306  
307 next if ($msg =~ /backup of client [\S]+ exited with status 1 /);
308  
309 my $windowsComment = $1 if ($msg =~ s/(\(WIN32.*\))//);
310  
311 printf("\n%${CLIENTWIDTH}s - %s", " +".dispInterval($tm-$job->start), $msg);
312 if (defined($windowsComment) && ($windowsComment !~ /WIN32 32:/)) {
313 printf("\n%${CLIENTWIDTH}s %s", "", $windowsComment);
314 }
315 }
316 }
317 if ($opts{'x'}) {
318 print "\n </job>";
319 }
320 print "\n";
321 }
322 }
323 print "</job-list>\n" if ($opts{'x'});
324  
325 =head1 NAME
326  
327 js.pl - NetBackup job status reporting
328  
329 =head1 SYNOPSIS
330  
331 js.pl [-v] [-r|-l] [-j <jobfile>] [-ef] [-aA] [-o <order>]
332 [-x]
333 [-t <policy-type>] [-c <policy>] [-C <client>] [-O <OS>]
334 [-M <master>]
335  
336 =head1 DESCRIPTION
337  
338 In the vein of L<ps|ps> js.pl lists the currently running NetBackup jobs. However, it
339 is also capable of regurgitating the fate of jobs already completed by using the B<-a> and
340 B<-A> options.
341  
342 =head1 OPTIONS
343  
344 =over 4
345  
346 =item B<-v>
347  
348 Verbose output only adds the job's schedule to the output display.
349  
350 =item B<-a>
351  
352 By default, js.pl only lists active jobs. With the B<-a> option it lists
353 inactive (more commonly known as completed) jobs as well.
354  
355 =item B<-A>
356  
357 By default, js.pl only lists jobs from the past 24 hours of operation. Setting
358 B<-A> lists all jobs in the NetBackup job database.
359  
360 =item B<-o> speed|client
361  
362 The most common way to list NetBackup jobs is in descending job-id order, i.e. the most recent
363 jobs first. The B<-o> option allows for two other orderings, namely speed and client. Speed
364 ordering lists jobs from fastest to slowest; client ordering lists jobs by client, leaving the
365 original reverse chronological ordering intact as much as possible.
366  
367 =item B<-x>
368  
369 The standard fixed width column output format can be exchanged for XML data by setting the B<-x>
370 option. XML data can easily be viewed in web browsers and is also manipulatable in Excel.
371  
372 =item B<-l>
373  
374 This option enables the logging of the raw job data transmitted from the NetBackup master
375 being monitored to a file in the user's home directory called ".alljobs.allcolumns". This
376 can then be used with the B<-r> option to replay the session.
377  
378 =item B<-r>
379  
380 Report on a set of data previously logged using the B<-l> option.
381  
382 =item B<-j> jobfile
383  
384 Using B<-r> without the B<-j> option will result in a replace of the data contained in the
385 file ~/.alljobs.allcolumns, i.e. the location where the B<-l> dropped off the last recorded
386 session. When reaching farther back in time, copies of these job logs can be maintained and
387 replayed by overriding the file being reported on.
388  
389 =item B<-e>
390  
391 More detailed job error information will be displayed when this option is given. NetBackup's
392 bperror command is used to retrieve this additional data but its output is distilled some to
393 keep the display more compact.
394  
395 =item B<-f>
396  
397 Those jobs with explicit file lists, the list is displayed below the job information. In cases
398 where the jobs list consists of the directive "ALL_LOCAL_DRIVES", the list is expanded to the
399 actual set of mounts/drives assigned to each job. ("NEW_STREAM" directives are filtered out.)
400  
401 =back
402  
403 =head1 SEE ALSO
404  
405 =over 4
406  
407 =item L<nbutop.pl|nbutop.pl>
408  
409 =back
410  
411 =head1 BUGS
412  
413 The B<-j> option should be made more intelligent so it can pull a collection of job history files
414 from a directory.
415  
416 =head1 AUTHOR
417  
418 Winkeler, Paul pwinkeler@pbnj-solutions.com
419  
420 =head1 COPYRIGHT
421  
422 Copyright (C) 2002 Paul Winkeler
423  
424 =cut