Logs/Acct.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 79.1% 57 / 72 Conditioncond 44.4% 12 / 27 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 79.6% 203 / 255 CC 51 SCAR 23.3
CC
51
Cov
86%
message_hash
16.4
get_hash
11.4
message_hash_dump
10.1
CRAP 10.3
↑ Summary Logs.pm »
1 package PBS::Logs::Acct;
2  
3 =head1 NAME
4  
5 PBS::Logs::Acct - parses the PBS accounting log files
6 and inherits from PBS::Logs.
7  
8 =head1 SYNOPSIS
9  
10 See the sections below:
11  
12 use PBS::Logs::Acct;
13  
14 The only non-inheritable function is the class level debug()
15  
16 PBS::Logs::Acct::debug()
17  
18 You must use
19  
20 PBS::Logs::debug()
21  
22 to read or set global debugging.
23 However, the instance version works just fine:
24  
25 $pl->debug()
26  
27 Other than that
28 PBS::Logs::Acct inherits all the methods that are available from
29 PBS::Logs, plus adds the methods listed below.
30  
31 =head1 DESCRIPTION
32  
33 =head2 EXPORT
34  
35 Can export message_hash() and message_hash_dump()
36  
37 =head1 SEE ALSO
38  
39 =over
40  
41 =item PBS::Logs
42  
43 =item PBS::Logs::Event
44  
45 =item The PBS Pro 5.4 Administrator Guide
46  
47 =back
48  
49 =head1 AUTHOR
50  
51 Dr R K Owen, E<lt>rkowen@nersc.govE<gt>
52  
53 =head1 COPYRIGHT AND LICENSE
54  
55 Copyright (C) 2005 The Regents of the University of California
56  
57 This library is free software; you can redistribute it
58 and/or modify it under the terms of the GNU Lesser General
59 Public License as published by the Free Software Foundation;
60 either version 2.1 of the License, or (at your option) any
61 later version.
62  
63 This library is distributed in the hope that it will be useful,
64 but WITHOUT ANY WARRANTY; without even the implied warranty
65 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
66 the GNU Lesser General Public License for more details,
67 which can be found at:
68  
69 http://www.gnu.org/copyleft/lesser.html
70 or http://www.opensource.org/licenses/lgpl-license.php
71  
72 =cut
73  
74 16 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 16 ▶use PBS::Logs;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 16 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 16 our @ISA = qw(Exporter PBS::Logs);
82  
83 16 our @EXPORT_OK = qw{message_hash message_hash_dump};
84  
85 16 our $VERSION = $PBS::Logs::VERSION;
86  
87 16 our %num2keys = (
88 0 => 'datetime',
89 1 => 'record_type',
90 2 => 'id',
91 3 => 'message'
92 );
93  
94 16 our %keys;
95 16 $keys{$num2keys{$_}}=$_ for (keys %num2keys);
96  
97 16 our %record_type = (
98 'A' => 'job aborted by server',
99 'B' => 'resource reservation period begin',
100 'C' => 'job checkpointed and held',
101 'D' => 'job deleted by request',
102 'E' => 'job ended',
103 'F' => 'resource reservation period finish',
104 'K' => 'removal of resource reservation by sheduler or server',
105 'k' => 'removal of resource reservation by client',
106 'Q' => 'job queued',
107 'R' => 'job rerun',
108 'S' => 'job execution started',
109 'T' => 'job restarted from checkpoint',
110 'U' => 'unconfirmed resource reservation created by server',
111 'Y' => 'confirmed resource reservation created by scheduler',
112 );
113  
114 16 our %record_message_fields = (
115 'B' => [
116 qw{owner name account queue ctime start end duration nodes
117 authorized_users authorized_groups authorized_hosts resource_list.}],
118 'E' => [
119 qw{user group account jobname queue resvname resvID resvjobID
120 ctime qtime etime start exec_host Resource_List. session
121 alt_id end Exit_status Resources_used.}],
122 'S' => [
123 qw{user group jobname queue
124 ctime qtime etime start exec_host Resource_List. session}],
125 );
126  
127 # Preloaded methods go here.
128  
129 =head1 new
130  
131 Create a PBS::Logs::Acct object.
132 It takes only one argument which is either a filename, array reference,
133 or a FILE glob reference.
134  
135 See PBS::Logs::new for examples and specifics.
136  
137 =cut
138  
139 16 ▶sub new {
Subroutine new: called CC=4 SCAR=15.2
Pod undocumented
140 16 my $proto = shift;
141 16 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0160
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
142 16 my $self = $class->SUPER::new(@_);
143 carp __PACKAGE__.": creating $self\n"
144 16 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 16 16
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
0016
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
145 16 $self->{'-records'} = undef;
146 16 bless ($self, $class);
147 16 return $self;
148 }
149  
150 =head1 get()
151  
152 Get the next accounting log entry. Extends PBS::Logs::get()
153 by filtering based on record_types. See PBS::Logs::Acct::filter_records()
154 below for more info on this filtering, and PBS::Logs::get() for
155 info on return values.
156  
157 =cut
158  
159 482 ▶sub get {
Subroutine get: called CC=9 SCAR=22.3
Pod undocumented
160 482 my $self = shift;
161 carp __PACKAGE__.": get $self(".join(',',@_).")\n"
162 482 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 482 482
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
00482
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
163  
164 482 my $a;
165 482 ▶ while ($a = $self->SUPER::get()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a = $self->SUPER::get) 560 50 610
166 560 ▶ if (defined $self->{'-records'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-records'}) { } 240 320 560
167 last if exists
168 240 ▶ $self->{'-records'}->{$a->[$keys{'record_type'}]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"-records"}{$$a[$PBS::Logs::Acct::keys{"record_type"}]} 112 128 240
169 } else {
170 320 last;
171 }
172 }
173  
174 482 ▶ return if ! defined wantarray; # just read log entry
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 482 482
175 482 ▶ return (defined $a ? @$a : ()) if (wantarray); # return array
Branch: 2 branches
BranchTrueFalseTotal
if wantarray 75 407 482
defined $a ? : 70 5 75
176 407 $a; # return array ref
177 }
178  
179 =head1 Special Arrays
180  
181 The following special associative arrays (hashes)
182 are provided by this package, which may be useful for
183 translating between arrays returned by the get() method
184 to/from hashes returned by the get_hash() method, or for
185 selecting a subset of the log entry.
186  
187 =head2 %PBS::Logs::Acct::num2keys
188  
189 Relates array position (number) to the keys (or field
190 descriptions) of a get_hash() generated hash.
191  
192 %num2keys = (
193 0 => 'datetime',
194 1 => 'record_type',
195 2 => 'id',
196 3 => 'message'
197 );
198  
199 =head2 %PBS::Logs::Acct::keys
200  
201 Relates keys (field descriptions) as used by the get_hash() method
202 to array positions (number) as returned from the get() method.
203 Essentially, just the inverse of %PBS::Logs::Acct::num2keys above.
204  
205 =head2 %PBS::Logs::Acct::record_type
206  
207 Describes the record types, which are keys to this hash array.
208  
209 %record_type = (
210 'A' => 'job aborted by server',
211 'B' => 'resource reservation period begin',
212 'C' => 'job checkpointed and held',
213 'D' => 'job deleted by request',
214 'E' => 'job ended',
215 'F' => 'resource reservation period finish',
216 'K' => 'removal of resource reservation by sheduler or server',
217 'k' => 'removal of resource reservation by client',
218 'Q' => 'job queued',
219 'R' => 'job rerun',
220 'S' => 'job execution started',
221 'T' => 'job restarted from checkpoint',
222 'U' => 'unconfirmed resource reservation created by server',
223 'Y' => 'confirmed resource reservation created by scheduler',
224 );
225  
226 =head1 get_hash()
227  
228 Like the PBS::Logs::Acct::get() method; however, instead of returning an
229 array reference, it (obviously) returns a hash
230 where the keys are the same keys as given by %PBS::Logs::Acct::keys .
231  
232 The accounting log entry looks like this with respect to the keys:
233  
234 datetime;record_type;id;message
235  
236 where the message field can have several key=value pairs depending on
237 the record_type and all the new-lines have been replaced with spaces.
238  
239 If in a scalar mode it will return a hash reference else it
240 returns a hash.
241  
242 =cut
243  
244 166 ▶sub get_hash {
Subroutine get_hash: called CC=9 SCAR=24.3
Pod undocumented
245 166 my $self = shift;
246 carp __PACKAGE__.": get_hashref $self(".join(',',@_).")\n"
247 166 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 166 166
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
00166
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
248  
249 166 my $a = $self->get();
250  
251 166 ▶ if (! defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 20 146 166
252 20 ▶ return () if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 20 20
253 20 return undef;
254 }
255 146 my $h = {};
256  
257 146 $h->{$_} = $a->[$keys{$_}] for (keys %keys);
258  
259 146 ▶ return if ! defined wantarray; # just read log line
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 146 146
260 146 ▶ return (defined $h ? %$h : ()) if (wantarray); # return hash
Branch: 2 branches
BranchTrueFalseTotal
if wantarray 0 146 146
defined $h ? : 0 0 0
261 146 $h; # return hash ref
262 }
263  
264 =head1 filter_records(\@array_reference_list_of_record_types)
265  
266 =head1 filter_records(@array_list_of_record_types)
267  
268 Sets or reads the record_type filter for the get() method.
269  
270 get() or get_hash() will only retrieve lines that have a record_type
271 in the list given.
272  
273 Sending an empty array reference will clear the record_type filtering.
274  
275 It will return '1' if successful, else undef if some warning occurs.
276  
277 If no arguments are given then the method will return an array
278 of record_types filtered.
279  
280 =cut
281  
282 39 ▶sub filter_records {
Subroutine filter_records: called CC=8 SCAR=20.9
Pod undocumented
283 39 my $self = shift;
284 carp __PACKAGE__.": filter_records $self(".join(',',@_).")\n"
285 39 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 39 39
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
0039
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
286  
287 39 my @f;
288 39 ▶ if (! defined $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 7 32 39
elsif (ref $_[0] eq 'ARRAY') { } 6 26 32
289 4 return (sort keys %{$self->{'-records'}})
290 7 ▶ if defined $self->{'-records'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"-records"} 4 3 7
291 3 return ();
292 } elsif (ref $_[0] eq "ARRAY") {
293 6 @f = (@{$_[0]});
294 } else {
295 26 @f = @_;
296 }
297 32 ▶ if (! scalar @f) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @f) { } 4 28 32
298 4 $self->{'-records'} = undef;
299 } else {
300 # create hash
301 28 $self->{'-records'} = {};
302 28 $self->{'-records'}->{$_} = 1 for (sort @f);
303 }
304  
305 32 1;
306 }
307  
308 =head1 message_hash($message_text)
309  
310 Parses an accounting log message and returns an associative array (hash)
311 containing the key/value pairs. And for certain fields, such as:
312 Resource_List and resources_used, the value is another hash array
313 containing the resource key and value.
314 Can be called either as a method of an instantiated
315 object, or as a class function.
316  
317 An example of the message text with resources_used dotted field:
318  
319 ...
320 resources_used.cpupercent=0 resources_used.cput=00:00:00
321 resources_used.mem=2880kb resources_used.ncpus=4
322 resources_used.vmem=6848kb resources_used.walltime=00:00:00
323 ...
324  
325 Results in a hash array of:
326  
327 ...
328 resources_used => {
329 cpupercent => 0,
330 cput => 00:00:00,
331 mem => 2880kb,
332 ncpus => 4,
333 vmem => 6848kb,
334 walltime => 00:00:00
335 }, ...
336  
337 =cut
338  
339 3 ▶sub message_hash {
Subroutine message_hash: called CC=16 SCAR=28.0
Pod undocumented
340 3 my $self = shift;
341 3 ▶ if (ref $self eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) { } 1 2 3
342 carp __PACKAGE__.": message_hash $self(".join(',',@_).")\n"
343 1 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 1 1
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
344 } else { # called directly as function
345 2 unshift @_, $self;
346 2 $self = undef;
347 2 ▶ carp __PACKAGE__.":: message_hash(".join(',',@_).")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug 0 2 2
348 if ($SUPER::debug);
349 }
350 3 my $m = shift;
351  
352 3 my @m;
353 3 my ($text,$quote) = (undef,undef);
354  
355 # Handle any key=value where the value may be ' or " delimited
356 # (which are the only two delimiters recognized).
357 # Since we are splitting on any whitespace ... this will have
358 # the effect that all whitespace gets replaced by spaces (' ').
359 3 foreach (split /\s/, $m) {
360 84 ▶ if (defined $quote && /(.*)$quote$/) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $quote and /(.*)$quote$/) { } 9 75 84
elsif (defined $quote and /^$/) { } 6 69 75
elsif (defined $quote) { } 0 69 69
elsif (/^([^=]+)=(['"])(.*)\2$/) { } 3 66 69
elsif (/^([^=]+)=(['"])(.*)$/) { } 9 57 66
Condition100%3/3
defined $quote and /(.*)$quote$/
!ll&&!rl&&r
6969
MC/DC100%2/2
defined $quote and /(.*)$quote$/
defined $quote /(.*)$quote$/
Truth table100%3/3
A and B
A= defined $quoteB= /(.*)$quote$/
ABresult
0X0
100
111
361 # end of quoted block
362 9 push @m, "$text $1";
363 9 $text = undef;
364 9 $quote = undef;
365 ▶ } elsif (defined $quote && /^$/) {
Condition66%2/3
defined $quote and /^$/
!ll&&!rl&&r
6906
MC/DC50%1/2
defined $quote and /^$/
defined $quote /^$/
Truth table66%2/3
A and B
A= defined $quoteB= /^$/
ABresult
0X0
100
111
366 # some type of whitespace (replace with space)
367 6 $text .= ' ';
368 } elsif (defined $quote) {
369 0 $text .= $_;
370 } elsif (/^([^=]+)=(['"])(.*)\2$/) {
371 3 push @m, "$1=$3";
372 } elsif (/^([^=]+)=(['"])(.*)$/) {
373 9 $text = "$1=$3";
374 9 $quote = $2;
375 } else {
376 57 push @m, $_;
377 57 $text = undef;
378 57 $quote = undef;
379 }
380 }
381  
382 3 my $h = {};
383  
384 3 for (@m) {
385 69 my ($k,$v) = m/^([^=]*)=*(.*)$/;
386 69 ▶ if ($k =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /\./) { } 30 39 69
387 30 my ($kk,$vv) = split('\.',$k);
388 30 ▶ $h->{$kk} = {} if ! exists $h->{$kk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h->{$kk} 6 24 30
389 30 $h->{$kk}->{$vv} = $v;
390 } else {
391 39 $h->{$k} = $v;
392 }
393 }
394 3 return $h;
395 }
396  
397 =head1 message_hash_dump($message_hash)
398  
399 Takes the hash returned by message_hash() and recursively
400 dumps the keys and values into a string suitable for viewing
401 or evaluation. Can be called either as a method of an instantiated
402 object, or as a class function.
403  
404 Example of evaluating the output:
405  
406 my $m = PBS::Logs::Acct::message_hash($some_message);
407 my $t = PBS::Logs::Acct::message_hash_dump($m);
408 my $x;
409 eval "\$x = $t"; # $x is now a HASH reference, equivalent to $m
410  
411 =cut
412  
413 9 ▶sub message_hash_dump {
Subroutine message_hash_dump: called CC=10 SCAR=23.2
Pod undocumented
414 9 my $self = shift;
415 9 ▶ if (ref $self eq "HASH") { # called directly as function
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'HASH') { } 6 3 9
416 6 unshift @_, $self;
417 6 $self = undef;
418 6 ▶ carp __PACKAGE__.":: message_hash_dump(".join(',',@_).")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug 0 6 6
419 if ($SUPER::debug);
420 } else {
421 carp __PACKAGE__.": message_hash_dump $self(".join(',',@_).")\n"
422 3 ▶ if ($SUPER::debug || $self->{'-debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $SUPER::debug or $self->{"-debug"} 0 3 3
Condition33%1/3
$SUPER::debug or $self->{"-debug"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$SUPER::debug or $self->{"-debug"}
$SUPER::debug $self->{"-debug"}
Truth table33%1/3
A or B
A= $SUPER::debugB= $self->{"-debug"}
ABresult
1X1
011
000
423 }
424 9 my $h = shift;
425 9 my $level = shift;
426 9 ▶ $level = 0 if ! defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 3 6 9
427  
428 9 my $text = (" " x $level)."{\n";
429  
430 9 foreach my $k (sort {lc($a) cmp lc($b);} keys %$h) {
431 75 ▶ if (ref $h->{$k} eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h->{$k} eq 'HASH') { } 6 69 75
432 6 $text .= (" " x $level)."'".$k."' => \n";
433 6 ▶ if (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) { } 2 4 6
434 $text .=
435 2 $self->message_hash_dump($h->{$k}, $level + 1);
436 } else {
437 $text .=
438 4 &message_hash_dump($h->{$k}, $level + 1);
439 }
440 } else {
441 $text .= (" " x $level)."'".$k."' => '".
442 69 $h->{$k}."',\n";
443 }
444 }
445 9 ▶ $text .= (" " x $level)."}".($level?",":"")."\n";
Branch: 1 branch
BranchTrueFalseTotal
$level ? : 6 3 9
446 9 $text;
447 }
448  
449 16 1;
450 __END__
↑ Summary Logs.pm »