script/nbutop.pl untested

filter: Statementstmt 0.0% 0 / 259 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 459 CC 32 SCAR 49.8
CC
32
Cov
0%
menu
342.0
sortByDrive
30.0
sortByVolume
30.0
CRAP 144.8
1 #!/usr/local/bin/perl -w
2 #
3 # Don't be intimidated by the GUI logic. The juicy, useful bits are in the
4 # main loop where we call NBU::Drive->updateStatus and NBU::Job->refreshJobs
5 # to get NBU's data structures synchronized with the real world.
6 # After that updating the display with information on the active jobs is trivial
7  
8 use strict;
9  
10 use Getopt::Std;
11 use Time::Local;
12  
13 #
14 # Download this one from CPAN
15 use Curses;
16  
17 my $program = $0; $program =~ s /^.*\/([^\/]+)$/$1/;
18  
19 my %opts;
20 getopts('d?ievlrs:p:M:', \%opts);
21  
22 if ($opts{'?'}) {
23 print STDERR <<EOT;
24 Usage: nbutop.pl [-v] [-i] [-e] [-r|-l] [-s <interval>] [-p <limit>] [-M <master>]
25 Options:
26 -v Verbose policy listing
27 -i Instantaneous throughput (instead of cumulative)
28 -e Elapsed time (instead of starting time)
29  
30 -r Replay previous job monitoring session
31 -l Log this monitoring session
32  
33 -s Refresh after <interval> seconds
34 -p Exit after <limit> passes
35  
36 -M Alternate NetBackup <master> server
37 EOT
38 exit;
39 }
40  
41 my $interval = 60;
42 if (defined($opts{'s'})) {
43 $interval = $opts{'s'};
44 }
45  
46 my $instant = $opts{'i'};
47 my $elapsed = $opts{'e'};
48  
49 my $passLimit = $opts{'p'};
50 my $refreshCounter = 0;
51  
52 use NBU;
53 NBU->debug($opts{'d'});
54  
55 my $master;
56 if ($opts{'M'}) {
57 $master = NBU::Host->new($opts{'M'});
58 }
59 else {
60 my @masters = NBU->masters; $master = $masters[0];
61 }
62  
63 sub dispInterval {
64 my $i = shift;
65  
66 my $seconds = $i % 60; $i = int($i / 60);
67 my $minutes = $i % 60;
68 my $hours = int($i / 60);
69  
70 my $fmt = sprintf("%02d", $seconds);
71 $fmt = sprintf("%02d:", $minutes).$fmt;
72 $fmt = sprintf("%02d:", $hours).$fmt;
73 return $fmt;
74 }
75  
76 sub sortByDrive {
77  
78 my $aDrive = defined($a->volume) && defined($a->volume->drive) ? $a->volume->drive->id : undef;
79 my $bDrive = defined($b->volume) && defined($b->volume->drive) ? $b->volume->drive->id : undef;
80  
81 return 1 if (!defined($aDrive));
82 return -1 if (!defined($bDrive));
83 return ($aDrive <=> $bDrive);
84 }
85 sub sortByVolume {
86  
87 my $aVolume = defined($a->volume) ? $a->volume->id : undef;
88 my $bVolume = defined($b->volume) ? $b->volume->id : undef;
89  
90 return 1 if (!defined($aVolume));
91 return -1 if (!defined($bVolume));
92 return ($aVolume cmp $bVolume);
93 }
94 sub sortByThroughput {
95  
96 my $aSpeed = $instant ? $a->ispeed : $a->speed;
97 my $bSpeed = $instant ? $b->ispeed : $b->speed;
98  
99 return 1 if (!defined($aSpeed));
100 return -1 if (!defined($bSpeed));
101  
102  
103 return ($aSpeed <=> $bSpeed);
104 }
105 sub sortBySize {
106  
107 my $aSize = defined($a->dataWritten) ? $a->dataWritten : 0;
108 my $bSize = defined($b->dataWritten) ? $b->dataWritten : 0;
109  
110 return ($bSize <=> $aSize);
111 }
112 sub sortByMediaServer {
113  
114 return ($a->mediaServer->name cmp $b->mediaServer->name);
115 }
116 sub sortByStorageUnit {
117  
118 return ($a->storageUnit->label cmp $b->storageUnit->label);
119 }
120 sub sortByClient {
121  
122 return ($a->client->name cmp $b->client->name);
123 }
124 sub sortByID {
125 my $result;
126  
127 $result = ($b->id <=> $a->id);
128 return $result;
129 }
130 my $sortOrder = \&sortByID;
131 my $sortColumn = "JOBID";
132  
133 sub menu {
134 my $answer = shift;
135 my $win = shift;
136  
137 if ($answer eq 'o') {
138 $win->addstr(2, 0, "Order by: (v)olume (d)rive (s)ize (t)hroughput (i)d (c)lient (m)ediaserver?");
139 my $o = $win->getch();
140 if ($o eq 'd') {
141 $sortOrder = \&sortByDrive;
142 $sortColumn = "DRIVE";
143 }
144 elsif ($o eq 'v') {
145 $sortOrder = \&sortByVolume;
146 $sortColumn = "VOLUME";
147 }
148 elsif ($o eq 'i') {
149 $sortOrder = \&sortByID;
150 $sortColumn = "JOBID";
151 }
152 elsif ($o eq 's') {
153 $sortOrder = \&sortBySize;
154 $sortColumn = "SIZE";
155 }
156 elsif ($o eq 'c') {
157 $sortOrder = \&sortByClient;
158 $sortColumn = "CLIENT";
159 }
160 elsif ($o eq 'm') {
161 $sortOrder = \&sortByMediaServer;
162 $sortColumn = "SRVR";
163 }
164 elsif ($o eq 'u') {
165 $sortOrder = \&sortByStorageUnit;
166 $sortColumn = "STU";
167 }
168 elsif ($o eq 't') {
169 $sortOrder = \&sortByThroughput;
170 $sortColumn = "SPD";
171 }
172 }
173 elsif ($answer eq 's') {
174 $win->addstr(2, 0, "Seconds to delay between refresh:");
175 echo(); $win->getstr(2, 34, $answer); noecho();
176 if ($answer =~ /^[\d]+$/) {
177 $interval = $answer;
178 }
179 }
180 elsif ($answer eq 'd') {
181 $win->addstr(2, 0, "Number of display passes:");
182 echo(); $win->getstr(2, 26, $answer); noecho();
183 if ($answer =~ /^[\d]+$/) {
184 $passLimit = $refreshCounter + $answer;
185 }
186 }
187 if ($answer eq 't') {
188 $win->addstr(2, 0, "Measure speed: (c)umulative (i)nstantaneously?");
189 my $t = $win->getch();
190 if ($t eq 'i') {
191 $instant = 1;
192 }
193 elsif ($t eq 'c') {
194 $instant = 0;
195 }
196 }
197 elsif (($answer eq '?') || ($answer eq 'h')) {
198 my $lines = $LINES-10; my $cols = $COLS-10;
199 my $help = $win->subwin($lines, $cols, 5, 5);
200 $help->clear(); $help->box('|', '-');
201  
202 $help->addstr(1, 2, "$program - A NetBackup job monitoring tool written in Perl");
203 $help->addstr(3, 2, "These single-character commands are available:");
204  
205 my $r = 5;
206 $help->addstr($r, 2, "h or ?");
207 $help->addstr($r, 9, "- help; show this text");
208 $help->addstr($r+=1, 2, "s");
209 $help->addstr($r, 9, "- change number of seconds to delay between updates");
210 $help->addstr($r+=1, 2, "d");
211 $help->addstr($r, 9, "- set number of display passes");
212 $help->addstr($r+=1, 2, "o");
213 $help->addstr($r, 9, "- specify sort order");
214 $help->addstr($r+=1, 2, "r");
215 $help->addstr($r, 9, "- force a refresh");
216 $help->addstr($r+=1, 2, "t");
217 $help->addstr($r, 9, "- throughput calculation method");
218 $help->addstr($r+=1, 2, "q");
219 $help->addstr($r, 9, "- quit");
220  
221 $help->attron(A_REVERSE);
222 $help->addstr($lines-2, 2, "Hit any key to continue:");
223 $help->attroff(A_REVERSE);
224  
225 $win->touchwin();
226 $help->refresh();
227 $win->getch();
228 }
229 else {
230 return 'r';
231 }
232  
233 $win->move(2, 0); $win->clrtoeol();
234 $win->refresh();
235  
236 return ' ';
237 }
238  
239 #
240 # Gather first round of drive data if we're running live
241 if (!$opts{'r'}) {
242 foreach my $server (NBU::StorageUnit->mediaServers($master)) {
243 NBU::Drive->populate($server) if ($server->mediaManager());
244 }
245 }
246  
247 #
248 # Load the first round of Job data
249 print STDERR "Loading...";
250 NBU::Job->loadJobs($master, $opts{'r'}, $opts{'l'});
251  
252 my $win = new Curses;
253 noecho(); cbreak();
254 $win->clear();
255 $win->refresh();
256  
257 local $SIG{WINCH} = sub {
258 print STDERR "Terminal resized...";
259 # initscr();
260 $win = new Curses;
261 $win->clear();
262 $win->refresh()
263 };
264  
265 my $CLIENTWIDTH = 25;
266  
267 my $hdr = sprintf("%${CLIENTWIDTH}s", "CLIENT ");
268 if ($opts{'v'}) {
269 $hdr .= " ".sprintf("%-40s", " CLASS/SCHEDULE");
270 }
271 else {
272 $hdr .= " ".sprintf("%-23s", " CLASS");
273 }
274 $hdr .= " ".sprintf("%7s", " JOBID ");
275 $hdr .= " ".sprintf("%8s", " TIME ");
276 $hdr .= " ".sprintf("%3s", "OP ");
277 $hdr .= " ".sprintf("%6s", "VOLUME");
278 $hdr .= " ".sprintf("%6s", " SIZE ");
279 $hdr .= " ".sprintf("%4s", " SPD ");
280 if ($opts{'r'}) {
281 $hdr .= " ".sprintf("%10s", " STU ");
282 }
283 else {
284 $hdr .= " ".sprintf("%11s", " DRIVE");
285 }
286  
287 while (!$passLimit || ($refreshCounter <= $passLimit)) {
288 my $jobCount = 0;
289 my $queueCount = 0; my $doneCount = 0;
290 my $totalSpeed = 0;
291 my $totalWireSpeed = 0;
292 my @jl = NBU::Job->list;
293  
294 #
295 # Sort the job list according to the order du jour
296 # For each active job, a description is built up and added to the display
297 # in one fell swoop.
298 $win->addstr(3, 1, $hdr);
299 for my $job (sort $sortOrder (@jl)) {
300 if (!$job->active) {
301 $queueCount += 1 if ($job->queued);
302 $doneCount += 1 if ($job->done);
303 next;
304 }
305  
306  
307 my $who = sprintf("%${CLIENTWIDTH}s", $job->client->name);
308  
309 my $classID = $job->class->name;
310 my $classIDlength = 23;
311 if ($opts{'v'}) {
312 $classID .= "/".$job->schedule->name;
313 $classIDlength = 40;
314 }
315 $classID = sprintf("%-".$classIDlength."s", $classID);
316  
317 my $jid = sprintf("%7u", $job->id);
318  
319 my $displayTime;
320 if ($elapsed) {
321 $displayTime = dispInterval(time - $job->start);
322 }
323 else {
324 my $startTime = ((time - $job->start) < (24 * 60 * 60)) ?
325 substr(localtime($job->start), 11, 8) :
326 " ".substr(localtime($job->start), 4, 6)." ";
327 $displayTime = $startTime;
328 }
329  
330 my $jobDescription = "$who $classID $jid $displayTime ".$job->operation;
331  
332 my $speed;
333 $jobDescription .= " ".sprintf("%8s", (defined($job->volume) ? $job->volume->id : ""));
334 $jobDescription .= " ".sprintf("%6d", int($job->dataWritten/1024));
335 if (defined($speed = ($instant ? $job->ispeed : $job->speed))) {
336 $speed /= 1024;
337 $jobDescription .= " ".sprintf("%5.2f", $speed)
338 if ($job->elapsedTime);
339 }
340 else {
341 $jobDescription .= " --.--";
342 }
343
344  
345 $jobDescription .= " ".sprintf("%10s", defined($job->storageUnit) ? $job->storageUnit->label : "");
346  
347 if (!$opts{'r'} && defined($job->volume)) {
348 if ($job->volume->drive) {
349 $jobDescription .= sprintf(":%2d", $job->volume->drive->index);
350 }
351 }
352  
353 #
354 # Alert viewer to certain jobs based on configuration data from
355 # job-alert.xml in /usr/local/etc
356 my $alert = 0;
357 if (defined($speed)) {
358  
359 $totalSpeed += $speed;
360 $totalWireSpeed += $speed if ($job->mediaServer->IPaddress ne $job->client->IPaddress);
361  
362 if ($job->dataWritten > (30 * 1024)) {
363 $alert |= ($job->class->name eq "NBUPR2") && ($speed < 5);
364 $alert |= ($job->class->name eq "PR2_SAP_ARCHIVES") && ($speed < 1.5);
365 }
366 }
367 $win->attron(A_REVERSE) if ($alert);
368 $win->addstr(4 + $jobCount++, 1, $jobDescription);
369 $win->attroff(A_REVERSE) if ($alert);
370 }
371  
372 my $down = 0;
373 my $total = 0;
374 if (!$opts{'r'}) {
375 for my $d (NBU::Drive->pool) {
376 next unless ($d->known);
377 $total++;
378 $down++ if ($d->down);
379 }
380 }
381 $win->addstr(0, 0, "Pass $refreshCounter; $jobCount active jobs, $queueCount queued jobs; Drives: $down down out of $total");
382 $refreshCounter++;
383 my $timestamp = localtime;
384 $win->addstr(0, $COLS-length($timestamp), $timestamp);
385  
386 $totalSpeed = sprintf("%.2f", $totalSpeed);
387 $totalWireSpeed = sprintf("%.2f", $totalWireSpeed);
388 $win->addstr(1, 0, ($instant ? "Instantaneous" : "Cumulative")." throughput ${totalSpeed}Mb/s; Network load ${totalWireSpeed}MB/s");
389  
390 $win->refresh;
391  
392 my $answer;
393 eval {
394 local $SIG{ALRM} = sub { die "timed out\n"; };
395  
396 alarm $interval if ($interval);
397 $answer = getch;
398 alarm 0 if ($interval);
399 };
400 if ($@) {
401 $answer = 'r';
402 }
403  
404 last if ($answer eq 'q');
405  
406 if ($answer eq 'k') {
407 # Choose which job to kill
408 }
409 elsif ($answer ne 'r') {
410 $answer = menu($answer, $win);
411 }
412  
413 if ($answer eq 'r') {
414 $win->addstr(2, 0, "Refreshing..."); $win->refresh();
415  
416 #
417 # If we're not doing a 'r'eplay, fetch the current status of the drives
418 # in the storage units so we can correlate the jobs to them.
419 if (!$opts{'r'}) {
420 foreach my $server (NBU::StorageUnit->mediaServers($master)) {
421 NBU::Drive->updateStatus($server) if ($server->mediaManager());
422 }
423 }
424 if (!defined(NBU::Job->refreshJobs($master))) {
425 last;
426 }
427 }
428  
429 $win->clear;
430 }
431 endwin();
432  
433 =head1 NAME
434  
435 nbutop.pl - An active backup job monitoring utility for NetBackup
436  
437 =head1 SYNOPSIS
438  
439 nbutop.pl [-v] [-i] [-r|-l] [-s <interval>] [-p <limit>] [-M <master>]
440  
441 =head1 DESCRIPTION
442  
443 In the vein of L<top|top> this utility presents a dynamically refreshing list of
444 running NetBackup jobs. It adapts (somewhat) to your display size but a minimum
445 of 100 columns is definitely required.
446  
447 By combining information from bpdbjobs and vmoprcmd, the exact tape drive
448 each job is using can be inferred making many trouble-shooting tasks that much
449 easier. (This obviously does not apply when replaying prior monitoring sessions
450 using the B<-r> option.)
451  
452 After a looooong initial delay during which NetBackup's bpdbjobs utility gathers
453 all information it has on all its jobs and sends it across to nbutop.pl the screen
454 will show the running jobs as well as some statistics on queued jobs. Additionally
455 you will see a reference to the number of drives nbutop.pl thinks are available
456 for use. Subsequent refreshes do not take nearly as long to calculate since
457 bpdbjobs' undocumented "refresh" command is used to gather the raw data for them.
458  
459 The throughput calculation is simply a matter of adding up all the speeds from all
460 the running streams. By default nbutop.pl will start out displaying cumulative speeds
461 meaning it simply divides a job's total amount of data written by the time spent writing
462 that data. Switching to instantaneous throughput causes it to examine the amount
463 data written most recently instead thus giving a measurement more indicative of the
464 rate at which the job is writing right now. (See the B<-i> option below.)
465  
466 Only media servers actually write data to tape. Some of that data is available local
467 to that media server, some (most?) of it arrives via one or more network connections.
468 Nbutop.pl separately adds up the throughout on only those jobs whose data arrives via
469 a network connection to quickly give you a sense of the load on the network.
470  
471 There are a few single character commands you may now issue to control nbutop.pl's
472 run-time behavior, the most important of which are 'q' to quit out of nbutop.pl and
473 '?' for a splash screen describing all the other commands.
474  
475 =head1 OPTIONS
476  
477 =over 4
478  
479 =item B<-v>
480  
481 Verbose output only adds the job's schedule to the output display.
482  
483 =item B<-i>
484  
485 Start out listing all job throughput information using a trailing "instantaneous"
486 calculation rather than the default cumulative one. This can be changed while nbutop.pl
487 is running. Instantaneous throughput number can fluctuate wildly but are helpful
488 when you suspect a job is slowly slowing down.
489  
490 =item B<-l>
491  
492 This option enables the logging of the raw job data transmitted from the NetBackup master
493 being monitored to a file in the user's home directory called ".alljobs.allcolumns". This
494 can then be used with the B<-r> option to replay the session.
495  
496 =item B<-r>
497  
498 Upon replaying a previously loggged series of job reports from a master server, nbutop.pl is
499 no longer able to correlate the mounting of volumes to the current state of the tape drives and
500 thus is not able to show which drives are (were?) being used.
501  
502 =back
503  
504 =head1 SEE ALSO
505  
506 =over 4
507  
508 =item L<js.pl|js.pl>
509  
510 =back
511  
512 =head1 AUTHOR
513  
514 Winkeler, Paul pwinkeler@pbnj-solutions.com
515  
516 =head1 COPYRIGHT
517  
518 Copyright (C) 2002 Paul Winkeler
519  
520 =cut