blib/lib/Flexnet/lmutil.pm

filter: Statementstmt 79.2% 80 / 101 Branchbran 56.9% 33 / 58 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 69.0% 125 / 181 CC 34 SCAR 32.8
CC
34
Cov
69%
lmstat
33.7
lmremove
30.0
new
6.7
CRAP 26.6
↑ Summary
1 package Flexnet::lmutil;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Flexnet::lmutil - Convenient OO-interface for Flexnet license server utility lmutil
11  
12 =head1 VERSION
13  
14 Version 1.5
15  
16 =cut
17  
18 2 our $VERSION = '1.5';
19  
20 =head1 DESCRIPTION
21  
22 Flexnet::lmutil is a small wrapper around the Flexnet license server utility lmutil,
23 currently implementing the sub-functions lmstat and lmremove. The module parses the
24 output of lmstat and returns an easy-to-use data structure. This makes it easy to
25 work further with lmstat output for e.g. web pages, monitoring plugins etc.
26  
27  
28 =head1 SYNOPSIS
29  
30 use Flexnet::lmutil;
31  
32 my $lmutil = new Flexnet::lmutil (
33 lm_license_path => 'port@host',
34 ...
35
36 );
37  
38 $status = $lmutil->lmstat (
39 feature => 'feature',
40
41 OR
42
43 daemon => 'daemon',
44
45 OR
46
47 'all'
48 );
49  
50 $lmutil->lmremove (
51 feature => 'feature',
52 serverhost => 'host',
53 port => 'port',
54 handle => 'handle'
55 );
56  
57 =head1 DETAILS
58  
59 =over 1
60  
61 =item new
62  
63 Possible arguments for the constructor are:
64  
65 =over 4
66  
67 =item C<lm_license_path>
68  
69 either the full pathname of the license file or the string C<port@host>
70 or even C<port1@host1:port2@host2>...
71  
72 =item C<verbose>
73  
74 show command line call
75  
76 =item C<testfile>
77  
78 textfile containing lmstat output (for testing), does not run lmstat
79  
80 =back
81  
82 =item lmstat
83  
84 Possible arguments for C<lmstat> are:
85  
86 =over 4
87  
88 =item C<feature>
89  
90 get info about feature usage
91  
92 =item C<daemon>
93  
94 get info about daemon usage
95  
96 =item C<all>
97  
98 get info about usage of all daemons and features
99  
100 =back
101  
102 C<lmstat> returns a hash reference with the following keys:
103  
104 =over 4
105  
106 =item * C<server>
107  
108 =item * C<vendor>
109  
110 =item * C<feature>
111  
112 =back
113  
114 B<server> points to another structure like
115  
116 'server' => {
117 'elba.uni-paderborn.de' => {
118 'ok' => 1,
119 'status' => 'UP'
120 }
121 },
122  
123 B<vendor> points to a structure like
124  
125 'vendor' => {
126 'cdslmd' => {
127 'ok' => 1,
128 'status' => 'UP v11.11',
129 'version' => '11.11'
130 }
131 }
132  
133 B<feature> points to a structure like
134  
135 'feature' => {
136 'MATLAB' => {
137 'reservations' => [
138 {
139 'reservations' => '1',
140 'group' => 'etechnik-labor',
141 'type' => 'HOST_GROUP'
142 }
143 ],
144 'issued' => '115',
145 'used' => '36',
146 'users' => [
147 {
148 'serverhost' => 'dabu.uni-paderborn.de',
149 'startdate' => 'Wed 8/12 17:18',
150 'port' => '27000',
151 'licenses' => 1,
152 'display' => 'bessel',
153 'host' => 'bessel',
154 'handle' => '4401',
155 'user' => 'hangmann'
156 },
157 ]
158 },
159 },
160 ...
161  
162 =item lmremove
163  
164 The C<lmremove> method expects the following arguments as a hash:
165  
166 feature => 'feature',
167 serverhost => 'host',
168 port => 'port',
169 handle => 'handle'
170  
171 =back
172  
173 =head1 AUTHOR
174  
175 Christopher Odenbach, C<< <odenbach at uni-paderborn.de> >>
176  
177 =head1 BUGS
178  
179 Please report any bugs or feature requests to C<bug-flexnet-lmutil at rt.cpan.org>, or through
180 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Flexnet-lmutil>. I will be notified, and then you'll
181 automatically be notified of progress on your bug as I make changes.
182  
183  
184  
185  
186 =head1 SUPPORT
187  
188 You can find documentation for this module with the perldoc command.
189  
190 perldoc Flexnet::lmutil
191  
192  
193 You can also look for information at:
194  
195 =over 4
196  
197 =item * RT: CPAN's request tracker (report bugs here)
198  
199 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Flexnet-lmutil>
200  
201 =item * AnnoCPAN: Annotated CPAN documentation
202  
203 L<http://annocpan.org/dist/Flexnet-lmutil>
204  
205 =item * CPAN Ratings
206  
207 L<http://cpanratings.perl.org/d/Flexnet-lmutil>
208  
209 =item * Search CPAN
210  
211 L<http://search.cpan.org/dist/Flexnet-lmutil/>
212  
213 =back
214  
215  
216 =head1 ACKNOWLEDGEMENTS
217  
218  
219 =head1 LICENSE AND COPYRIGHT
220  
221 Copyright 2015 Christopher Odenbach.
222  
223 This program is free software; you can redistribute it and/or modify it
224 under the terms of either: the GNU General Public License as published
225 by the Free Software Foundation; or the Artistic License.
226  
227 See http://dev.perl.org/licenses/ for more information.
228  
229  
230 =cut
231  
232 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.1
Pod documented
233 1 my $pkg = shift;
234 1 my %args = @_;
235  
236 1 ▶ my $lmutil = ($args{lmutil} or which ('lmutil') or '');
Condition40%2/5
$args{'lmutil'} || which('lmutil') || ''
l!l
01
$args{"lmutil"} or which("lmutil")
l!l&&r!l&&!r
001
MC/DC0%0/2
$args{'lmutil'} || which('lmutil') || ''
$args{"lmutil"} which("lmutil")
Truth table33%1/3
A or B
A= $args{"lmutil"}B= which("lmutil")
ABresult
1X1
011
000
237  
238 1 ▶ if (not defined $args{testfile} and not -x $lmutil ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"testfile"} or -x $lmutil) 0 1 1
Condition33%1/3
defined $args{"testfile"} or -x $lmutil
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $args{"testfile"} or -x $lmutil
defined $args{"testfile"} -x $lmutil
Truth table33%1/3
A or B
A= defined $args{"testfile"}B= -x $lmutil
ABresult
1X1
011
000
239 0 die "lmutil not executable\n";
240 }
241  
242 1 my $self = {
243 lmutil => $lmutil,
244 %args
245 };
246 1 return bless ($self, $pkg);
247 }
248  
249 1 ▶sub lmstat {
Subroutine lmstat: called CC=26 SCAR=35.2
Pod documented
250 1 my $self = shift;
251
252 1 my @args = @_;
253
254 1 ▶ if (@args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 1 0 1
255 1 push @args, 1;
256 }
257 1 my %args = @args;
258  
259 1 my ($feature, $status);
260  
261 1 my $cmd = "$self->{lmutil} lmstat";
262  
263 1 ▶ if ( defined ($self->{lm_license_path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"lm_license_path"}) 0 1 1
264 0 $cmd .= " -c $self->{lm_license_path}";
265 }
266  
267 1 ▶ if ( defined ($args{all}) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $args{'all'}) { } 1 0 1
elsif (defined $args{'feature'}) { } 0 0 0
elsif (defined $args{'daemon'}) { } 0 0 0
268 1 $cmd .= " -a";
269 } elsif ( defined ($args{feature}) ) {
270 0 $cmd .= " -f $args{feature}";
271 } elsif ( defined ($args{daemon}) ) {
272 0 $cmd .= " -S $args{daemon}";
273 }
274
275 # for testing purpose we can provide a text file with the output of lmstat
276 1 my $fh;
277 1 ▶ if ( defined ($self->{testfile}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'testfile'}) { } 1 0 1
278 1 ▶ open ($fh, $self->{testfile}) or die "Could not open $self->{testfile}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $self->{"testfile"} or die qq[Could not open $self->{"testfile"}: $!] 1 0 1
279 } else {
280 0 ▶ print "Running command: $cmd\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
281 0 open ($fh, "$cmd |");
282 }
283
284 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 98 1 99
285 98 ▶ print "lmstat: $_" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 98 98
286
287 # lmgrd status
288 98 ▶ if ( my ($server, $server_status) = /^\s*([\w.-]+): license server (\S+)/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if (() = /^\s*([\w.-]+): license server (\S+)/) { } 1 97 98
elsif (() = /^\s*(\S+?): (.*)/) { } 3 94 97
elsif (/^Users of (\S+):\s*\(Total of (\d+) licenses? issued;\s*Total of (\d+) lic/) { } 11 83 94
elsif (() = m[^\s+(.+) \(v([\d\.]+)\) \(([^/]+)/(\d+) (\d+)\), start (.*)]) { } 36 47 83
elsif (() = /^\s+(\d+)\s+RESERVATIONs? for ([\w_]+) ([\w_-]+) /) { } 4 43 47
289 1 $status->{server}->{$server}->{status} = $server_status;
290 1 ▶ if ( $server_status eq "UP" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server_status eq 'UP') { } 1 0 1
291 1 $status->{server}->{$server}->{ok} = 1;
292 } else {
293 0 $status->{server}->{$server}->{ok} = 0;
294 }
295
296 # vendor daemon status
297 } elsif ( my ($vendor, $state) = /^\s*(\S+?): (.*)/ ) {
298  
299 # skip vendor_string line
300 3 ▶ next if $vendor eq 'vendor_string';
Branch: 1 branch
BranchTrueFalseTotal
if $vendor eq "vendor_string" 0 3 3
301  
302 3 $status->{vendor}->{$vendor}->{status} = $state;
303 3 ▶ if ( $state =~ /^UP v([\d.]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /^UP v([\d.]+)/) { } 3 0 3
304 3 $status->{vendor}->{$vendor}->{ok} = 1;
305 3 $status->{vendor}->{$vendor}->{version} = $1;
306 } else {
307 0 $status->{vendor}->{$vendor}->{ok} = 0;
308 }
309
310 # feature usage info
311 } elsif ( /^Users of (\S+):\s*\(Total of (\d+) licenses? issued;\s*Total of (\d+) lic/ ) {
312 11 $feature = $1;
313 11 my $issued = $2;
314 11 my $used = $3;
315 11 $status->{feature}->{$feature}->{issued} = $issued;
316 11 $status->{feature}->{$feature}->{used} = $used;
317
318 # user info
319 } elsif ( my ($clientinfo, $version, $serverhost, $port, $handle, $rest) =
320 m{^\s+(.+) \(v([\d\.]+)\) \(([^/]+)/(\d+) (\d+)\), start (.*)} ) {
321
322 36 my ($user, $host);
323 36 my $display = '';
324
325 # split clientinfo
326
327 36 my @parts = split / /, $clientinfo;
328 36 ▶ if (@parts == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parts == 2) { } 4 32 36
elsif (@parts == 3) { } 28 4 32
329 4 ($user, $host) = @parts;
330 } elsif (@parts == 3) {
331 28 ($user, $host, $display) = @parts;
332 } else {
333 4 my $max = @parts;
334
335 # host = display?
336 4 ▶ if ($parts[$max - 2] eq $parts[$max - 1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[$max - 2] eq $parts[$max - 1]) { } 1 3 4
337 1 $display = pop @parts;
338 1 $host = pop @parts;
339 1 $user = join (' ', @parts);
340 } else {
341 # display contains / or : ?
342 3 my $i = 2;
343 3 ▶ while ($i <= @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= @parts) 3 0 3
344 3 ▶ if ($parts[$i] =~ m{^[:/]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[$i] =~ m[^[:/]]) 3 0 3
345 3 $display = $parts[$i];
346 3 $host = $parts[$i - 1];
347 3 $user = join (' ', map { $parts[$_] } 0..$i-2);
348 3 last;
349 }
350 0 $i++;
351 }
352
353 # if still no luck, just guess
354 3 ▶ unless (defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 3 3
355 0 ($user, $host, $display) = @parts;
356 }
357 }
358 }
359
360 # starttime and optional number of licenses
361 36 my $startdate;
362 36 my $licenses = 1;
363 36 ▶ if ($rest =~ /^([^,]+), (\d+) license/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^([^,]+), (\d+) license/) { } 1 35 36
364 1 $startdate = $1;
365 1 $licenses = $2;
366 } else {
367 35 $startdate = $rest;
368 }
369
370 36 push @{$status->{feature}->{$feature}->{users}}, {
371 user=>$user,
372 host=>$host,
373 display=>$display,
374 licenses=>$licenses,
375 serverhost=>$serverhost,
376 port=>$port,
377 handle=>$handle,
378 startdate=>$startdate
379 };
380
381 # reservation info
382 } elsif ( my ($reservations, $type, $group) = /^\s+(\d+)\s+RESERVATIONs? for ([\w_]+) ([\w_-]+) / ) {
383 4 push @{$status->{feature}->{$feature}->{reservations}},
384 {type=>$type, group=>$group, reservations=>$reservations};
385 }
386
387 }
388 1 close ($fh);
389
390 1 return $status;
391 }
392  
393 0 ▶sub lmremove {
Subroutine lmremove: not called CC=5 SCAR=34.0
Pod documented
394 0 my $self = shift;
395 0 my %args = @_;
396 0 my $cmd;
397
398 0 foreach my $arg (qw (feature serverhost port handle)) {
399 0 ▶ die "Parameter '$arg' missing\n" unless $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{$arg} 0 0 0
400 }
401
402 0 $cmd = "$self->{lmutil} lmremove";
403 0 ▶ if ( defined ($self->{lm_license_path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"lm_license_path"}) 0 0 0
404 0 $cmd .= " -c $self->{lm_license_path}";
405 }
406 0 $cmd .= " -h $args{feature} $args{serverhost} $args{port} $args{handle}";
407  
408 0 ▶ print "Running command: $cmd\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
409 0 system($cmd);
410 }
411  
412  
413  
414  
415 2 1; # End of Flexnet::lmutil
↑ Summary