Jmx4Perl.pm

filter: Statementstmt 30.0% 114 / 379 Branchbran 17.1% 33 / 192 Conditioncond 17.2% 14 / 81 MC/DCmcdc 8.7% 4 / 46 Subroutinesub 41.8% 18 / 43 Podpod 84.2% 16 / 19 totaltotal 26.1% 199 / 760 CC 133 SCAR 45.4
CC
133
Cov
25%
_extract_get_set_parameters
380.0
_format_map
240.0
_extract_execute_parameters
210.0
CRAP 93.5
« Jmx4Perl/Nagios/MessageHandler.pm ↑ Summary Jmx4Perl/Nagios/CheckJmx4Perl.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 JMX::Jmx4Perl - JMX access for Perl
6  
7 =head1 SYNOPSIS
8  
9 Simple:
10  
11 use strict;
12 use JMX::Jmx4Perl;
13 use JMX::Jmx4Perl::Alias; # Import MBean aliases
14  
15 print "Memory Used: ",
16 JMX::Jmx4Perl
17 ->new(url => "http://localhost:8080/j4p")
18 ->get_attribute(MEMORY_HEAP_USED);
19  
20 Advanced:
21  
22 use strict;
23 use JMX::Jmx4Perl;
24 use JMX::Jmx4Perl::Request; # Type constants are exported here
25  
26 my $jmx = new JMX::Jmx4Perl(url => "http://localhost:8080/j4p",
27 product => "jboss");
28 my $request = new JMX::Jmx4Perl::Request({type => READ,
29 mbean => "java.lang:type=Memory",
30 attribute => "HeapMemoryUsage",
31 path => "used"});
32 my $response = $jmx->request($request);
33 print "Memory used: ",$response->value(),"\n";
34  
35 # Get general server information
36 print "Server Info: ",$jmx->info();
37  
38 =head1 DESCRIPTION
39  
40 Jmx4Perl is here to connect the Java and Perl Enterprise world by providing
41 transparent access to the Java Management Extensions (JMX) from the perl side.
42  
43 It uses a traditional request-response paradigma for performing JMX operations
44 on a remote Java Virtual machine.
45  
46 There a various ways how JMX information can be transfered. Jmx4Perl is based
47 on a Jolokia I<agent> (www.jolokia.org), which needs to deployed on the target
48 platform. It plays the role of a proxy, which on one side communicates with the
49 MBeanServer within in the application server and transfers JMX related
50 information via HTTP and JSON to the client (i.e. this module). Please refer to
51 L<JMX::Jmx4Perl::Manual> for installation instructions for how to deploy the
52 Jolokia agent.
53  
54 An alternative and more 'java like' approach is the usage of JSR 160
55 connectors. However, the default connectors provided by the Java Virtual
56 Machine (JVM) since version 1.5 support only proprietary protocols which
57 require serialized Java objects to be exchanged. This implies that a JVM needs
58 to be started on the client side adding quite some overhead if used from within
59 Perl. If you absolutely require JSR 160 communication (e.g. because a agent can
60 not be deployed on the target for some reason), you can still use Jmx4Perl
61 operating with the so called I<proxy mode>.
62  
63 For further discussion comparing both approaches, please refer to
64 L<JMX::Jmx4Perl::Manual>
65  
66 JMX itself knows about the following operations on so called I<MBeans>, which
67 are specific "managed beans" designed for JMX and providing access to
68 management functions:
69  
70 =over
71  
72 =item *
73  
74 Reading and writing of attributes of an MBean (like memory usage or connected
75 users)
76  
77 =item *
78  
79 Executing of exposed operations (like triggering a garbage collection)
80  
81 =item *
82  
83 Registering of notifications which are send from the application server to a
84 listener when a certain event happens.
85  
86 =back
87  
88 =head1 METHODS
89  
90 =over
91  
92 =cut
93  
94 package JMX::Jmx4Perl;
95  
96 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use JMX::Jmx4Perl::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use JMX::Jmx4Perl::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 4 ▶use vars qw($VERSION $HANDLER_BASE_PACKAGE @PRODUCT_HANDLER_ORDERING);
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 4 $VERSION = "1.13";
106  
107 4 my $REGISTRY = {
108 # Agent based
109 "agent" => "JMX::Jmx4Perl::Agent",
110 "JMX::Jmx4Perl::Agent" => "JMX::Jmx4Perl::Agent",
111 "JJAgent" => "JMX::Jmx4Perl::Agent",
112 };
113  
114 4 my %PRODUCT_HANDLER;
115  
116 5 ▶sub _register_handlers {
Subroutine _register_handlers: called CC=7 SCAR=19.5
117 5 my $handler_package = shift;
118 5 %PRODUCT_HANDLER = ();
119  
120 5 my @id2order = ();
121 5 for my $handler (findsubmod $handler_package) {
122 62 ▶ next unless $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 62 62
123 62 my $handler_file = $handler;
124 62 $handler_file =~ s|::|/|g;
125 62 require $handler_file.".pm";
126 62 ▶ next if $handler eq $handler_package."::BaseHandler";
Branch: 1 branch
BranchTrueFalseTotal
if $handler eq $handler_package . '::BaseHandler' 4 58 62
127 58 my $id = eval "${handler}::id()";
128 58 ▶ die "No id() method on $handler: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 58 58
129 58 $PRODUCT_HANDLER{lc $id} = $handler;
130 58 push @id2order, [ lc $id, $handler->order() ];
131 }
132 # Ordering Schema according to $handler->order():
133 # -10,-5,-3,0,undef,undef,undef,1,8,9,1000
134 0 ▶ my @high = map { $_->[0] } sort { $a->[1] <=> $b->[1] } grep { defined($_->[1]) && $_->[1] <= 0 } @id2order;
Condition100%3/3
defined $_->[1] && $_->[1] <= 0
!ll&&!rl&&r
12415
MC/DC100%2/2
defined $_->[1] && $_->[1] <= 0
defined $_->[1] $_->[1] <= 0
Truth table100%3/3
A && B
A= defined $_->[1]B= $_->[1] <= 0
ABresult
0X0
100
111
135 5 my @med = map { $_->[0] } grep { not defined($_->[1]) } @id2order;
136 5 ▶ my @low = map { $_->[0] } sort { $a->[1] <=> $b->[1] } grep { defined($_->[1]) && $_->[1] > 0 } @id2order;
Condition100%3/3
defined $_->[1] && $_->[1] > 0
!ll&&!rl&&r
12541
MC/DC100%2/2
defined $_->[1] && $_->[1] > 0
defined $_->[1] $_->[1] > 0
Truth table100%3/3
A && B
A= defined $_->[1]B= $_->[1] > 0
ABresult
0X0
100
111
137 5 @PRODUCT_HANDLER_ORDERING = (@high,@med,@low);
138 }
139  
140 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
141 4 &_register_handlers("JMX::Jmx4Perl::Product");
142 }
143  
144  
145 =item $jmx = JMX::Jmx4Perl->new(mode => <access module>, ....)
146  
147 Create a new instance. The call is dispatched to an Jmx4Perl implementation by
148 selecting an appropriate mode. For now, the only mode supported is "agent",
149 which uses the L<JMX::Jmx4Perl::Agent> backend. Hence, the mode can be
150 submitted for now.
151  
152 Options can be given via key value pairs (or via a hash). Recognized options
153 are:
154  
155 =over
156  
157 =item server
158  
159 You can provide a server name which is looked up in a configuration file. The
160 configuration file's name can be given via C<config_file> (see below) or, by
161 default, C<.j4p> in the users home directory is used.
162  
163 =item config_file
164  
165 Path to a configuration file to use
166  
167 =item config
168  
169 A L<JMX::Jmx4Perl::Config> object which is used for
170 configuraton. Use this is you already read in the
171 configuration on your own.
172  
173 =item product
174  
175 If you provide a product id via the named parameter C<product> you can given
176 B<jmx4perl> a hint which server you are using. By default, this module uses
177 autodetection to guess the kind of server you are talking to. You need to
178 provide this argument only if you use B<jmx4perl>'s alias feature and if you
179 want to speed up things (autodetection can be quite slow since this requires
180 several JMX request to detect product specific MBean attributes).
181  
182 =item timeout
183  
184 Timeout in seconds for an HTTP request
185  
186 =item method
187  
188 Default HTTP method to use for requests which can be overridden for each
189 specific request
190  
191 =back
192  
193 Any other named parameters are interpreted by the backend, please
194 refer to its documentation for details (i.e. L<JMX::Jmx4Perl::Agent>)
195  
196 =cut
197  
198 5 ▶sub new {
Subroutine new: called CC=12 SCAR=32.0
Pod documented
199 5 my $class = shift;
200 5 ▶ my $cfg = ref($_[0]) eq "HASH" ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 5 5
201  
202 # Merge in config from a configuration file if a server name is given
203 5 ▶ if ($cfg->{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"server"}) 0 5 5
204 0 ▶ my $config = $cfg->{config} ? $cfg->{config} : new JMX::Jmx4Perl::Config($cfg->{config_file});
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'config'} ? : 0 0 0
205 0 my $server_cfg = $config->get_server_config($cfg->{server});
206 0 ▶ if (defined($server_cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server_cfg) 0 0 0
207 0 $cfg = { %$server_cfg, %$cfg };
208 }
209 }
210  
211 5 ▶ my $mode = delete $cfg->{mode} || autodiscover_mode();
Condition33%1/3
delete $cfg->{'mode'} || autodiscover_mode()
l!l&&r!l&&!r
050
MC/DC0%0/2
delete $cfg->{'mode'} || autodiscover_mode()
delete $cfg->{'mode'} autodiscover_mode()
Truth table33%1/3
A || B
A= delete $cfg->{'mode'}B= autodiscover_mode()
ABresult
1X1
011
000
212 5 ▶ my $product = $cfg->{product} ? lc delete $cfg->{product} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{'product'} ? : 1 4 5
213  
214 5 ▶ $class = $REGISTRY->{$mode} || croak "Unknown runtime mode " . $mode;
Condition50%1/2
$REGISTRY->{$mode} || croak('Unknown runtime mode ' . $mode)
l!l
50
MC/DC0%0/1
$REGISTRY->{$mode} || croak('Unknown runtime mode ' . $mode)
$REGISTRY->{$mode}
Truth table50%1/2
A
A= $REGISTRY->{$mode}
Aresult
11
00
215 5 ▶ if ($product && !$PRODUCT_HANDLER{lc $product}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($product and not $PRODUCT_HANDLER{lc $product}) 0 5 5
Condition66%2/3
$product and not $PRODUCT_HANDLER{lc $product}
!ll&&!rl&&r
410
MC/DC0%0/2
$product and not $PRODUCT_HANDLER{lc $product}
$product not $PRODUCT_HANDLER{lc $product}
Truth table66%2/3
A and not(B)
A= $productB= not $PRODUCT_HANDLER{lc $product}
ABresult
0X0
100
111
216 0 die "No handler for product '$product'. Known Handlers are [".(join ", ",keys %PRODUCT_HANDLER)."]";
217 }
218  
219 5 eval "require $class";
220 5 ▶ croak "Cannot load $class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
221  
222 5 my $self = {
223 cfg => $cfg,
224 product => $product
225 };
226 5 ▶ bless $self,(ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
227 5 $self->init();
228 5 return $self;
229 }
230  
231 # ==========================================================================
232  
233 =item $value => $jmx->get_attribute(...)
234  
235 $value = $jmx->get_attribute($mbean,$attribute,$path)
236 $value = $jmx->get_attribute($alias)
237 $value = $jmx->get_attribute(ALIAS) # Literal alias as defined in
238 # JMX::Jmx4Perl::Alias
239 $value = $jmx->get_attribute({ domain => <domain>,
240 properties => { <key> => value },
241 attribute => <attribute>,
242 path => <path> })
243 $value = $jmx->get_attribute({ alias => <alias>,
244 path => <path })
245  
246 Read a JMX attribute. In the first form, you provide the MBean name, the
247 attribute name and an optional path as positional arguments. The second
248 variant uses named parameters from a hashref.
249  
250 The Mbean name can be specified with the canonical name (key C<mbean>), or with
251 a domain name (key C<domain>) and one or more properties (key C<properties> or
252 C<props>) which contain key-value pairs in a Hashref. For more about naming of
253 MBeans please refer to
254 L<http://java.sun.com/j2se/1.5.0/docs/api/javax/management/ObjectName.html> for
255 more information about JMX naming.
256  
257 Alternatively, you can provide an alias, which gets resolved to its real name
258 by so called I<product handler>. Several product handlers are provided out of
259 the box. If you have specified a C<product> id during construction of this
260 object, the associated handler is selected. Otherwise, autodetection is used to
261 guess the product. Note, that autodetection is potentially slow since it
262 involves several JMX calls to the server. If you call with a single, scalar
263 value, this argument is taken as alias (without any path). If you want to use
264 aliases together with a path, you need to use the second form with a hash ref
265 for providing the (named) arguments.
266  
267 Additionally you can use a pattern and/or an array ref for attributes to
268 combine multiple reads into a single request. With an array ref as attribute
269 argument, all the given attributes are queried. If C<$attribute> is C<undef>
270 all attributes on the MBean are queried.
271  
272 If you provide a pattern as described for the L<"/search"> method, a search
273 will be performed on the server side, an for all MBeans found which carry the
274 given attribute(s), their value will be returned. Attributes which doesn't
275 apply to an MBean are ignored.
276  
277 Note, that the C<path> feature is not available when using MBean patterns or
278 multiple values.
279  
280 Depending on the arguments, this method return value has a different format:
281  
282 =over 4
283  
284 =item Single MBean, single attribute
285  
286 The return value is the result of the serverside read operation. It will throw
287 an exception (die), if an error occurs on the server side, e.g. when the name
288 couldn't be found.
289  
290 Example:
291  
292 $val = $jmx->get_attribute("java.lang:type=Memory","HeapMemoryUsage");
293 print Dumper($val);
294  
295 {
296 committed => 174530560,
297 init => 134217728,
298 max => "1580007424",
299 used => 35029320
300 }
301  
302 =item Single MBean, multiple attributes
303  
304 In this case, this method returns a map with the attribute name as keys and the
305 attribute values as map values. It will die if not a single attribute could be
306 fetched, otherwise unknown attributes are ignored.
307  
308 $val = $jmx->get_attribute(
309 "java.lang:type=Memory",
310 ["HeapMemoryUsage","NonHeapMemoryUsage"]
311 );
312 print Dumper($val);
313  
314 {
315 HeapMemoryUsage => {
316 committed => 174530560,
317 init => 134217728,
318 max => "1580007424",
319 used => 37444832
320 },
321 NonHeapMemoryUsage => {
322 committed => 87552000,
323 init => 24317952,
324 max => 218103808,
325 used => 50510976
326 }
327 }
328  
329 =item MBean pattern, one or more attributes
330  
331 $val = $jmx->get_attribute(
332 "java.lang:type=*",
333 ["HeapMemoryUsage","NonHeapMemoryUsage"]
334 );
335 print Dumper($val);
336  
337 {
338 "java.lang:type=Memory" => {
339 HeapMemoryUsage => {
340 committed => 174530560,
341 init => 134217728,
342 max => "1580007424",
343 used => 38868584
344 },
345 NonHeapMemoryUsage => {
346 committed => 87552000,
347 init => 24317952,
348 max => 218103808,
349 used => 50514304
350 }
351 }
352 }
353  
354 The return value is a map with the matching MBean names as keys and as value
355 another map, with attribute names keys and attribute value values. If not a
356 singel MBean matches or not a single attribute can be found on the matching
357 MBeans this method dies. This format is the same whether you are using a single
358 attribute or an array ref of attribute names.
359  
360 =back
361  
362 Please don't overuse pattern matching (i.e. don't use patterns like "*:*"
363 except you really want to) since this could easily blow up your Java
364 application. The return value is generated completely in memory. E.g if you
365 want to retrieve all attributes for Weblogic with
366  
367 $jmx->get_attribute("*:*",undef);
368  
369 you will load more than 200 MB in to the Heap. Probably not something you
370 want to do. So please be nice to your appserver and use a more restrictive
371 pattern.
372  
373 =cut
374  
375 0 ▶sub get_attribute {
Subroutine get_attribute: not called CC=7 SCAR=40.3
Pod documented
376 0 my $self = shift;
377 0 my ($object,$attribute,$path) = $self->_extract_get_set_parameters(with_value => 0,params => [@_]);
378 0 ▶ croak "No object name provided" unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
379  
380 0 my $response;
381 0 ▶ if (ref($object) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq 'CODE') { } 0 0 0
382 0 $response = $self->delegate_to_handler($object);
383 } else {
384 #croak "No attribute provided for object $object" unless $attribute;
385 0 my $request = JMX::Jmx4Perl::Request->new(READ,$object,$attribute,$path);
386 0 $response = $self->request($request);
387 # print Dumper($response);
388 }
389 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
390 0 ▶ my $a = ref($attribute) eq "ARRAY" ? "[" . join(",",@$attribute) . "]" : $attribute;
Branch: 1 branch
BranchTrueFalseTotal
ref $attribute eq 'ARRAY' ? : 0 0 0
391 0 ▶ my $o = "(".$object.",".$a.($path ? "," . $path : "").")";
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 0 0
392 0 ▶ croak "The attribute $o is not registered on the server side"
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 0 0 0
393 if $response->status == 404;
394 0 croak "Error requesting $o: ",$response->error_text;
395 }
396 0 return $response->value;
397 }
398  
399 =item $resp = $jmx->set_attribute(...)
400  
401 $new_value = $jmx->set_attribute($mbean,$attribute,$value,$path)
402 $new_value = $jmx->set_attribute($alias,$value)
403 $new_value = $jmx->set_attribute(ALIAS,$value) # Literal alias as defined in
404 # JMX::Jmx4Perl::Alias
405 $new_value = $jmx->set_attribute({ domain => <domain>,
406 properties => { <key> => value },
407 attribute => <attribute>,
408 value => <value>,
409 path => <path> })
410 $new_value = $jmx->set_attribute({ alias => <alias>,
411 value => <value>,
412 path => <path })
413  
414 Method for writing an attribute. It has the same signature as L</get_attribute>
415 except that it takes an additional parameter C<value> for setting the value. It
416 returns the old value of the attribute (or the object pointed to by an inner
417 path).
418  
419 As for C<get_attribute> you can use a path to specify an inner part of a more
420 complex data structure. The value is tried to set on the inner object which is
421 pointed to by the given path.
422  
423 Please note that only basic data types can be set this way. I.e you can set
424 only values of the following types
425  
426 =over
427  
428 =item C<java.lang.String>
429  
430 =item C<java.lang.Boolean>
431  
432 =item C<java.lang.Integer>
433  
434 =back
435  
436 =cut
437  
438 0 ▶sub set_attribute {
Subroutine set_attribute: not called CC=6 SCAR=37.4
Pod documented
439 0 my $self = shift;
440  
441 0 my ($object,$attribute,$path,$value) =
442 $self->_extract_get_set_parameters(with_value => 1,params => [@_]);
443 0 ▶ croak "No object name provided" unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
444 0 my $response;
445 0 ▶ if (ref($object) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq 'CODE') { } 0 0 0
446 0 $response = $self->delegate_to_handler($object,$value);
447 } else {
448 0 ▶ croak "No attribute provided for object $object" unless $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute 0 0 0
449 0 ▶ croak "No value to set provided for object $object and attribute $attribute" unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
450 0 my $request = JMX::Jmx4Perl::Request->new(WRITE,$object,$attribute,$value,$path);
451 0 $response = $self->request($request);
452 }
453 0 ▶ if ($response->status == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status == 404) 0 0 0
454 0 return undef;
455 }
456 0 return $response->value;
457 }
458  
459 =item $info = $jmx->info($verbose)
460  
461 Get a textual description of the server as returned by a product specific
462 handler (see L<JMX::Jmx4Perl::Product::BaseHandler>). It uses the
463 autodetection facility if no product is given explicitely during construction.
464  
465 If C<$verbose> is true, print even more information
466  
467 =cut
468  
469 0 ▶sub info {
Subroutine info: not called CC=2 SCAR=17.9
Pod documented
470 0 my $self = shift;
471 0 my $verbose = shift;
472 0 ▶ my $handler = $self->{product_handler} || $self->_create_handler();
Condition0%0/3
$self->{'product_handler'} || $self->_create_handler
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'product_handler'} || $self->_create_handler
$self->{'product_handler'} $self->_create_handler
Truth table0%0/3
A || B
A= $self->{'product_handler'}B= $self->_create_handler
ABresult
1X1
011
000
473 0 return $handler->info($verbose);
474 }
475  
476  
477 =item $mbean_list = $jmx->search($mbean_pattern)
478  
479 Search for MBean based on a pattern and return a reference to the list of found
480 MBeans names (as string). If no MBean can be found, C<undef> is returned. For
481 example,
482  
483 $jmx->search("*:j2eeType=J2EEServer,*")
484  
485 searches all MBeans whose name are matching this pattern, which are according
486 to JSR77 all application servers in all available domains.
487  
488 =cut
489  
490 0 ▶sub search {
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
491 0 my $self = shift;
492 0 ▶ my $pattern = shift || croak "No pattern provided";
Condition0%0/2
shift() || croak('No pattern provided')
l!l
00
MC/DC0%0/1
shift() || croak('No pattern provided')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
493  
494 0 my $request = new JMX::Jmx4Perl::Request(SEARCH,$pattern);
495 0 my $response = $self->request($request);
496  
497 # An error of 404 was the behaviour of Jolokia < 0.90,
498 # for > 0.90 an empty list was returned
499 0 ▶ return undef if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 0 0 0
500 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
501 0 die "Error searching for $pattern: ",$response->error_text;
502 }
503 0 my $val = $response->value;
504 0 ▶ return ref($val) eq "ARRAY" && @$val ? $val : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' && @$val ? : 0 0 0
Condition0%0/3
ref $val eq "ARRAY" and @$val
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val eq "ARRAY" and @$val
ref $val eq "ARRAY" @$val
Truth table0%0/3
A and B
A= ref $val eq "ARRAY"B= @$val
ABresult
0X0
100
111
505 }
506  
507 =item $ret = $jmx->execute(...)
508  
509 $ret = $jmx->execute($mbean,$operation,$arg1,$arg2,...)
510 $ret = $jmx->execute(ALIAS,$arg1,$arg2,...)
511  
512 $value = $jmx->execute({ domain => <domain>,
513 properties => { <key> => value },
514 operation => <operation>,
515 arguments => [ <arg1>, <arg2>, ... ] })
516 $value = $jmx->execute({ alias => <alias>,
517 arguments => [ <arg1,<arg2>, .... ]})
518  
519 Execute a JMX operation with the given arguments. If used in the second form,
520 with an alias as first argument, it is recommended to use the constant as
521 exported by L<JMX::Jmx4Perl::Alias>, otherwise it is guessed, whether the first
522 string value is an alias or a MBean name. To be sure, use the variant with an
523 hashref as argument.
524  
525 If you are calling an overloaded JMX operation (i.e. operations with the same
526 name but a different argument signature), the operation name must include the
527 signature as well. This is be done by adding the parameter types comma
528 separated within parentheses:
529  
530 ...
531 operation => "overloadedMethod(java.lang.String,int)"
532 ...
533  
534 This method will croak, if something fails during execution of this
535 operation or when the MBean/Operation combination could not be found.
536  
537 The return value of this method is the return value of the JMX operation.
538  
539 =cut
540  
541 0 ▶sub execute {
Subroutine execute: not called CC=5 SCAR=34.0
Pod documented
542 0 my $self = shift;
543  
544 0 my @args = @_;
545 0 my ($mbean,$operation,$op_args) = $self->_extract_execute_parameters(@_);
546 0 my $response;
547 0 ▶ if (ref($mbean) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mbean eq 'CODE') { } 0 0 0
548 0 $response = $self->delegate_to_handler($mbean,@{$op_args});
549 } else {
550 0 my $request = new JMX::Jmx4Perl::Request(EXEC,$mbean,$operation,@{$op_args});
551 0 $response = $self->request($request);
552 }
553 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
554 0 ▶ croak "No MBean ".$mbean." with operation ".$operation.
Branch: 2 branches
BranchTrueFalseTotal
if $response->status == 404 0 0 0
@$op_args ? : 0 0 0
555 (@$op_args ? " (Args: [".join(",",@$op_args)."]" : "").") found on the server side"
556 if $response->status == 404;
557 0 croak "Error executing operation $operation on MBean $mbean: ",$response->error_text;
558 }
559 0 return $response->value;
560 }
561  
562  
563 =item $resp = $jmx->version()
564  
565 This method return the version of the agent as well as the j4p protocol
566 version. The agent's version is a regular program version and corresponds to
567 jmx4perl's version from which the agent has been taken. The protocol version
568 is an integer number which indicates the version of the protocol specification.
569  
570 The return value is a hash with the keys C<agent> and C<protocol>
571  
572 =cut
573  
574 0 ▶sub version {
Subroutine version: not called CC=2 SCAR=17.9
Pod documented
575 0 my $self = shift;
576  
577 0 my $request = new JMX::Jmx4Perl::Request(AGENT_VERSION);
578 0 my $response = $self->request($request);
579  
580 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
581 0 die "Error getting the agent's version: ",$response->error_text;
582 }
583  
584 0 return $response->value;
585 }
586  
587 =item $resp = $jmx->request($request)
588  
589 Send a request to the underlying agent and return the response. This is an
590 abstract method which needs to be overwritten by a subclass. The argument must
591 be of type L<JMX::Jmx4Perl::Request> and it returns an object of type
592 L<JMX::Jmx4Perl::Response>.
593  
594 =cut
595  
596 0 ▶sub request {
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
597 0 croak "Internal: Must be overwritten by a subclass";
598 }
599  
600 =item $agents = JMX::Jmx4Perl->discover_agents($timeout)
601  
602 Discover agents by sending a multicast request on which Jolokia agents are
603 listening. The optional C<$timeout> can be used to tune how long to wait for
604 discovery answers (in seconds). By default 1 seconds is waited. This functionality
605 requires L<IO::Socket::Multicast> to be installed.
606  
607 This methods returns an array ref, which looks like
608  
609 [
610 {
611 'version' => '1.2.0-SNAPSHOT',
612 'server_version' => '7.0.50',
613 'server_product' => 'tomcat',
614 'secured' => 0,
615 'url' => 'http://10.9.11.2:8778/jolokia/',
616 'server_vendor' => 'Apache',
617 'confidence' => 100,
618 'type' => 'response'
619 }
620 ]
621  
622 Please refer to Jolokia's reference documentation for the meaning of the keys.
623 The most important part it C<url> which points to the agent's URL which can
624 be used to construct a new L<JMX::Jmx4Perl> object.
625  
626 =cut
627  
628  
629 0 ▶sub discover_agents {
Subroutine discover_agents: not called CC=5 SCAR=34.0
Pod documented
630 0 my $self = shift;
631 0 my $timeout = shift | 1;
632  
633 0 my $s;
634 0 eval {
635 0 $s = IO::Socket::Multicast->new();
636 };
637 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
638 0 eval {
639 0 require "IO/Socket/Multicast.pm";
640 0 $s = IO::Socket::Multicast->new();
641 };
642 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
643 0 die "No IO::Socket::Multicast installed\n";
644 }
645 }
646  
647 0 $s->mcast_send('{"type" : "query"}',"239.192.48.84:24884");
648  
649 0 my @result = ();
650 0 my $data;
651 LOOP:
652 0 while (1) {
653 0 eval {
654 0 ▶ local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
Subroutine __ANON__: not called CC=1 SCAR=6.9
655 0 alarm $timeout;
656 0 $s->recv($data,8192);
657 0 push @result,from_json($data, {utf8 => 1} );
658 0 alarm 0;
659 };
660 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
661 0 ▶ die unless $@ eq "timeout\n"; # propagate unexpected errors
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "timeout\n" 0 0 0
662 # timed out
663 0 last LOOP;
664 }
665 }
666 0 return \@result;
667 }
668  
669 # ===========================================================================
670 # Alias handling
671  
672 =item ($object,$attribute,$path) = $self->resolve_alias($alias)
673  
674 Resolve an alias for an attibute or operation. This is done by querying registered
675 product handlers for resolving an alias. This method will croak if a handler
676 could be found but not such alias is known by C<jmx4perl>.
677  
678 If the C<product> was not set during construction, the first call to this
679 method will try to autodetect the server. If it cannot determine the proper
680 server it will throw an exception.
681  
682 For an attribute, this method returns the object, attribute, path triple which
683 can be used for requesting the server or C<undef> if the handler can not
684 handle this alias.
685  
686 For an operation, the MBean, method name and the (optional) path, which should be
687 applied to the return value, is returned or C<undef> if the handler cannot
688 handle this alias.
689  
690 A handler can decide to handle the fetching of the alias value directly. In
691 this case, this metod returns the code reference which needs to be executed
692 with the handler as argument (see "delegate_to_handler") below.
693  
694 =cut
695  
696 5 ▶sub resolve_alias {
Subroutine resolve_alias: called CC=3 SCAR=11.3
Pod documented
697 5 my $self = shift;
698 5 ▶ my $alias = shift || croak "No alias provided";
Condition50%1/2
shift() || croak('No alias provided')
l!l
50
MC/DC0%0/1
shift() || croak('No alias provided')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
699  
700 5 ▶ my $handler = $self->{product_handler} || $self->_create_handler();
Condition66%2/3
$self->{'product_handler'} || $self->_create_handler
l!l&&r!l&&!r
230
MC/DC0%0/2
$self->{'product_handler'} || $self->_create_handler
$self->{'product_handler'} $self->_create_handler
Truth table66%2/3
A || B
A= $self->{'product_handler'}B= $self->_create_handler
ABresult
1X1
011
000
701 5 return $handler->alias($alias);
702 }
703  
704 =item $do_support = $self->supports_alias($alias)
705  
706 Test for checking whether a handler supports a certain alias.
707  
708 =cut
709  
710 0 ▶sub supports_alias {
Subroutine supports_alias: not called CC=2 SCAR=17.9
Pod documented
711 0 my ($object) = shift->resolve_alias(shift);
712 0 ▶ return $object ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$object ? : 0 0 0
713 }
714  
715 =item $response = $self->delegate_to_handler($coderef,@args)
716  
717 Execute a subroutine with the current handler as argument and returns the
718 return value of this subroutine. This method is used in conjunction with
719 C<resolve_alias> to allow handler a more sophisticated way to access the
720 MBeanServer. The method specified by C<$coderef> must return a
721 L<JMX::Jmx4Perl::Response> as answer.
722  
723 The subroutine is supposed to handle reading and writing of attributes and
724 execution of operations. Optional additional parameters are given to the subref
725 as additional arguments.
726  
727 =cut
728  
729 0 ▶sub delegate_to_handler {
Subroutine delegate_to_handler: not called CC=2 SCAR=17.9
Pod documented
730 0 my $self = shift;
731 0 my $code = shift;
732 0 ▶ my $handler = $self->{product_handler} || $self->_create_handler();
Condition0%0/3
$self->{'product_handler'} || $self->_create_handler
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'product_handler'} || $self->_create_handler
$self->{'product_handler'} $self->_create_handler
Truth table0%0/3
A || B
A= $self->{'product_handler'}B= $self->_create_handler
ABresult
1X1
011
000
733 0 return &{$code}($handler,@_);
734 }
735  
736 =item $product = $self->product()
737  
738 For supported application servers, this methods returns product handler
739 which is an object of type L<JMX::Jmx4Perl::Product::BaseHandler>.
740  
741 This product is either detected automatically or provided during
742 construction time.
743  
744 The most interesting methods on this object are C<id()>, C<name()> and
745 C<version()>
746  
747 =cut
748  
749 0 ▶sub product {
Subroutine product: not called CC=2 SCAR=17.9
Pod documented
750 0 my $self = shift;
751 0 ▶ my $handler = $self->{product_handler} || $self->_create_handler();
Condition0%0/3
$self->{'product_handler'} || $self->_create_handler
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'product_handler'} || $self->_create_handler
$self->{'product_handler'} $self->_create_handler
Truth table0%0/3
A || B
A= $self->{'product_handler'}B= $self->_create_handler
ABresult
1X1
011
000
752 0 return $handler;
753 }
754  
755 =item $value = $jmx->list($path)
756  
757 Get all MBeans as registered at the specified server. A C<$path> can be
758 specified in order to fetchy only a subset of the information. When no path is
759 given, the returned value has the following format
760  
761 $value = {
762 <domain> =>
763 {
764 <canonical property list> =>
765 {
766 "attr" =>
767 {
768 <atrribute name> =>
769 {
770 desc => <description of attribute>
771 type => <java type>,
772 rw => true/false
773 },
774 ....
775 },
776 "op" =>
777 {
778 <operation name> =>
779 {
780 desc => <description of operation>
781 ret => <return java type>
782 args =>
783 [
784 {
785 desc => <description>,
786 name => <name>,
787 type => <java type>
788 },
789 ....
790 ]
791 },
792 ....
793 },
794 ....
795 }
796 ....
797 };
798  
799 A complete path has the format C<"E<lt>domainE<gt>/E<lt>property
800 listE<gt>/("attribute"|"operation")/E<lt>indexE<gt>">
801 (e.g. C<java.lang/name=Code Cache,type=MemoryPool/attribute/0>). A path can be
802 provided partially, in which case the remaining map/array is returned. See also
803 L<JMX::Jmx4Perl::Agent::Protocol> for a more detailed discussion of inner
804 paths.
805  
806 This method throws an exception if an error occurs.
807  
808 =cut
809  
810 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
811 0 my $self = shift;
812 0 my $path = shift;
813  
814 0 my $request = JMX::Jmx4Perl::Request->new(LIST,$path);
815 0 my $response = $self->request($request);
816 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
817 0 my $txt = "Error while listing attributes: " . $response->error_text . "\n" .
818 "Status: " . $response->status . "\n";
819 #($response->stacktrace ? "\n" . $response->stacktrace . "\n" : "\n");
820 0 die $txt;
821 }
822 0 return $response->value;
823 }
824  
825  
826 =item ($domain,$attributes) = $jmx->parse_name($name)
827  
828 Parse an object name into its domain and attribute part. If successful,
829 C<$domain> contains the domain part of the objectname, and C<$attribtutes> is a
830 hahsref to the attributes of the name with the attribute names as keys and the
831 attribute's values as values. This method returns C<undef> when the name could
832 not be parsed. Result of a C<search()> operation can be savely feed into this
833 method to get to the subparts of the name. JMX quoting is taken into account
834 properly, too.
835  
836 Example:
837  
838 my ($domain,$attrs) =
839 $jmx->parse_name("java.lang:name=Code Cache,type=MemoryPool");
840 print $domain,"\n",Dumper($attrs);
841  
842 java.lang
843 {
844 name => "Code Cache",
845 type => "MemoryPool"
846 }
847  
848 =cut
849  
850 8 ▶sub parse_name {
Subroutine parse_name: called CC=9 SCAR=22.0
Pod documented
851 8 my $self = shift;
852 8 my $name = shift;
853 8 my $escaped = shift;
854  
855 8 ▶ return undef unless $name =~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /:/ 1 7 8
856 7 my ($domain,$rest) = split(/:/,$name,2);
857 7 my $attrs = {};
858 7 ▶ while ($rest =~ s/([^=]+)\s*=\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/([^=]+)\s*=\s*//) 11 6 17
859 #print "R: $rest\n";
860 11 my $key = $1;
861 11 my $value = undef;
862 11 ▶ if ($rest =~ /^"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^"/) { } 4 7 11
863 4 $rest =~ s/("((\\"|[^"])+)")(\s*,\s*|$)//;
864 4 ▶ $value = $escaped ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
$escaped ? : 0 4 4
865 # Unescape escaped chars
866 4 ▶ $value =~ s/\\([:",=*?])/$1/g unless $escaped;
Branch: 1 branch
BranchTrueFalseTotal
unless $escaped 4 0 4
867 } else {
868 7 ▶ if ($rest =~ s/([^,]+)(\s*,\s*|$)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/([^,]+)(\s*,\s*|$)//) 6 1 7
869 6 $value = $1;
870 }
871 }
872 11 ▶ return undef unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 10 11
873 10 $attrs->{$key} = $value;
874 #print "K: $key V: $value\n";
875 }
876 # If there is something left, we were not successful
877 # in parsing the name
878 6 ▶ return undef if $rest;
Branch: 1 branch
BranchTrueFalseTotal
if $rest 1 5 6
879 5 return ($domain,$attrs);
880 }
881  
882  
883 =item $formatted_text = $jmx->formatted_list($path)
884  
885 =item $formatted_text = $jmx->formatted_list($resp)
886  
887 Get the a formatted string representing the MBeans as returnded by C<list()>.
888 C<$path> is the optional inner path for selecting only a subset of all mbean.
889 See C<list()> for more details. If called with a L<JMX::Jmx4Perl::Response>
890 object, the list and the optional path will be taken from the provided response
891 object and not fetched again from the server.
892  
893 =cut
894  
895 0 ▶sub formatted_list {
Subroutine formatted_list: not called CC=5 SCAR=34.0
Pod documented
896 0 my $self = shift;
897 0 my $path_or_resp = shift;
898 0 my $path;
899 my $list;
900  
901 0 ▶ if ($path_or_resp && UNIVERSAL::isa($path_or_resp,"JMX::Jmx4Perl::Response")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_or_resp and UNIVERSAL::isa($path_or_resp, 'JMX::Jmx4Perl::Response')) { } 0 0 0
Condition0%0/3
$path_or_resp and UNIVERSAL::isa($path_or_resp, "JMX::Jmx4Perl::Response")
!ll&&!rl&&r
000
MC/DC0%0/2
$path_or_resp and UNIVERSAL::isa($path_or_resp, "JMX::Jmx4Perl::Response")
$path_or_resp UNIVERSAL::isa($path_or_resp, "JMX::Jmx4Perl::Response")
Truth table0%0/3
A and B
A= $path_or_respB= UNIVERSAL::isa($path_or_resp, "JMX::Jmx4Perl::Response")
ABresult
0X0
100
111
902 0 $path = $path_or_resp->request->get("path");
903 0 $list = $path_or_resp->value;
904 } else {
905 0 $path = $path_or_resp;
906 0 $list = $self->list($path);
907 }
908 0 my @path = ();
909 0 ▶ @path = split m|/|,$path if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
910 #print Dumper(\@path);
911 0 ▶ croak "A path can be used only for a domain name or MBean name" if @path > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @path > 2 0 0 0
912 0 my $intent = "";
913 0 my $ret = &_format_map("",$list,\@path,0);
914 }
915  
916  
917 # ===============================================================================================
918  
919 # Helper method for extracting parameters for the set/get methods.
920 0 ▶sub _extract_get_set_parameters {
Subroutine _extract_get_set_parameters: not called CC=19 SCAR=59.4
921 0 my $self = shift;
922 0 my %args = @_;
923 0 my $p = $args{params};
924 0 my $f = $p->[0];
925 0 my $with_value = $args{with_value};
926 0 my ($object,$attribute,$path,$value);
927 0 ▶ if (ref($f) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f eq 'HASH') { } 0 0 0
928 0 $value = $f->{value};
929 0 ▶ if ($f->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{'alias'}) { } 0 0 0
930 0 my $alias_path;
931 ($object,$attribute,$alias_path) =
932 0 $self->resolve_alias($f->{alias});
933 0 ▶ if (ref($object) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq "CODE") 0 0 0
934 # Let the handler do it
935 0 ▶ return ($object,undef,undef,$args{with_value} ? $value : undef);
Branch: 1 branch
BranchTrueFalseTotal
$args{'with_value'} ? : 0 0 0
936 }
937 0 ▶ croak "No alias ",$f->{alias}," defined for handler ",$self->product->name unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
938 0 ▶ if ($alias_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias_path) { } 0 0 0
939 0 ▶ $path = $f->{path} ? $f->{path} . "/" . $alias_path : $alias_path;
Branch: 1 branch
BranchTrueFalseTotal
$f->{'path'} ? : 0 0 0
940 } else {
941 0 $path = $f->{path};
942 }
943 } else {
944 0 ▶ $object = $f->{mbean} || $self->_glue_mbean_name($f) ||
Condition0%0/5
$f->{'mbean'} || $self->_glue_mbean_name($f) || croak('No MBean name or domain + properties given')
l!l
00
$f->{"mbean"} or $self->_glue_mbean_name($f)
l!l&&r!l&&!r
000
MC/DC0%0/2
$f->{'mbean'} || $self->_glue_mbean_name($f) || croak('No MBean name or domain + properties given')
$f->{"mbean"} $self->_glue_mbean_name($f)
Truth table0%0/3
A or B
A= $f->{"mbean"}B= $self->_glue_mbean_name($f)
ABresult
1X1
011
000
945 croak "No MBean name or domain + properties given";
946 0 $attribute = $f->{attribute};
947 0 $path = $f->{path};
948 }
949 } else {
950 0 ▶ if ( (@{$p} == 1 && !$args{with_value}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (@{$p;} == 1 and not $args{'with_value'} or @{$p;} == 2 and $args{'with_value'} or $self->_is_alias($p->[0])) { } 0 0 0
Condition0%0/12
@{$p;} == 1 and not $args{"with_value"} or @{$p;} == 2 and $args{"with_value"} or $self->_is_alias($p->[0])
l!l&&r!l&&!r
000
@{$p;} == 1 and not $args{"with_value"} or @{$p;} == 2 and $args{"with_value"}
l!l&&r!l&&!r
000
@{$p;} == 1 and not $args{"with_value"}
!ll&&!rl&&r
000
@{$p;} == 2 and $args{"with_value"}
!ll&&!rl&&r
000
MC/DC0%0/5
@{$p;} == 1 and not $args{"with_value"} or @{$p;} == 2 and $args{"with_value"} or $self->_is_alias($p->[0])
@{$p;} == 1 not $args{"with_value"} @{$p;} == 2 $args{"with_value"} $self->_is_alias($p->[0])
Truth table0%0/11
A and not(B) or C and D or E
A= @{$p;} == 1B= not $args{"with_value"}C= @{$p;} == 2D= $args{"with_value"}E= $self->_is_alias($p->[0])
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
951 0 (@{$p} == 2 && $args{with_value}) || $self->_is_alias($p->[0])) {
952 # A single argument can only be used as an alias
953 0 ($object,$attribute,$path) =
954 $self->resolve_alias($f);
955 0 $value = $_[1];
956 0 ▶ if (ref($object) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq "CODE") 0 0 0
957 # Let the handler do it
958 0 ▶ return ($object,undef,undef,$args{with_value} ? $value : undef);
Branch: 1 branch
BranchTrueFalseTotal
$args{'with_value'} ? : 0 0 0
959 }
960 0 ▶ croak "No alias ",$f," defined for handler ",$self->product->name unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
961 } else {
962 0 ▶ if ($args{with_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'with_value'}) { } 0 0 0
963 0 ($object,$attribute,$value,$path) = @{$p};
964 } else {
965 0 ($object,$attribute,$path) = @{$p};
966 }
967 }
968 }
969 0 return ($object,$attribute,$path,$value);
970 }
971  
972 0 ▶sub _extract_execute_parameters {
Subroutine _extract_execute_parameters: not called CC=14 SCAR=53.5
973 0 my $self = shift;
974 0 my @args = @_;
975 0 my ($mbean,$operation,$op_args);
976 0 ▶ if (ref($args[0] eq "JMX::Jmx4Perl::Request")) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($args[0] eq 'JMX::Jmx4Perl::Request')) { } 0 0 0
elsif (ref $args[0] eq 'HASH') { } 0 0 0
977 0 die 'Use $j4p->request(), not $j4p->execute() for executing a JMX::Jmx4Perl::Request',"\n";
978 } elsif (ref($args[0]) eq "HASH") {
979 0 my $args = $args[0];
980 0 ▶ if ($args->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'alias'}) { } 0 0 0
981 0 ($mbean,$operation) = $self->resolve_alias($args->{alias});
982 0 ▶ if (ref($mbean) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mbean eq "CODE") 0 0 0
983 # Alias handles this completely on its own
984 0 ▶ return ($mbean,undef,$args->{arguments} || $args->{args});
Condition0%0/3
$args->{'arguments'} || $args->{'args'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'arguments'} || $args->{'args'}
$args->{'arguments'} $args->{'args'}
Truth table0%0/3
A || B
A= $args->{'arguments'}B= $args->{'args'}
ABresult
1X1
011
000
985 }
986 0 ▶ croak "No alias ",$args->{alias}," defined for handler ",$self->product->name unless $mbean;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbean 0 0 0
987 } else {
988 0 ▶ $mbean = $args->{mbean} || $self->_glue_mbean_name($args) ||
Condition0%0/5
$args->{'mbean'} || $self->_glue_mbean_name($args) || croak('No MBean name or domain + properties given')
l!l
00
$args->{"mbean"} or $self->_glue_mbean_name($args)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'mbean'} || $self->_glue_mbean_name($args) || croak('No MBean name or domain + properties given')
$args->{"mbean"} $self->_glue_mbean_name($args)
Truth table0%0/3
A or B
A= $args->{"mbean"}B= $self->_glue_mbean_name($args)
ABresult
1X1
011
000
989 croak "No MBean name or domain + properties given";
990 0 ▶ $operation = $args->{operation} || croak "No operation given";
Condition0%0/2
$args->{'operation'} || croak('No operation given')
l!l
00
MC/DC0%0/1
$args->{'operation'} || croak('No operation given')
$args->{'operation'}
Truth table0%0/2
A
A= $args->{'operation'}
Aresult
11
00
991 }
992 0 ▶ $op_args = $args->{arguments} || $args->{args};
Condition0%0/3
$args->{'arguments'} || $args->{'args'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'arguments'} || $args->{'args'}
$args->{'arguments'} $args->{'args'}
Truth table0%0/3
A || B
A= $args->{'arguments'}B= $args->{'args'}
ABresult
1X1
011
000
993 } else {
994 0 ▶ if ($self->_is_alias($args[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_alias($args[0])) { } 0 0 0
995 0 ($mbean,$operation) = $self->resolve_alias($args[0]);
996 0 shift @args;
997 0 ▶ if (ref($mbean) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mbean eq "CODE") 0 0 0
998 # Alias handles this completely on its own
999 0 return ($mbean,undef,[ @args ]);
1000 }
1001 0 ▶ croak "No alias ",$args[0]," defined for handler ",$self->product->name unless $mbean;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbean 0 0 0
1002 0 $op_args = [ @args ];
1003 } else {
1004 0 $mbean = shift @args;
1005 0 $operation = shift @args;
1006 0 $op_args = [ @args ];
1007 }
1008 }
1009 0 return ($mbean,$operation,$op_args);
1010 }
1011  
1012 # Check whether the argument is possibly an alias
1013 0 ▶sub _is_alias {
Subroutine _is_alias: not called CC=3 SCAR=24.8
1014 0 my $self = shift;
1015 0 my $alias = shift;
1016 0 ▶ if (UNIVERSAL::isa($alias,"JMX::Jmx4Perl::Alias::Object")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($alias, 'JMX::Jmx4Perl::Alias::Object')) { } 0 0 0
elsif ('JMX::Jmx4Perl::Alias'->by_name($alias)) { } 0 0 0
1017 0 return 1;
1018 } elsif (JMX::Jmx4Perl::Alias->by_name($alias)) {
1019 0 return 1;
1020 } else {
1021 0 return 0;
1022 }
1023 }
1024  
1025 0 ▶sub _glue_mbean_name {
Subroutine _glue_mbean_name: not called CC=7 SCAR=40.3
1026 0 my $self = shift;
1027 0 my $f = shift;
1028 0 my $object = undef;
1029 0 ▶ if ($f->{domain} && ($f->{properties} || $f->{props})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"domain"} and $f->{"properties"} || $f->{"props"}) 0 0 0
Condition0%0/6
$f->{"domain"} and $f->{"properties"} || $f->{"props"}
!ll&&!rl&&r
000
$f->{"properties"} or $f->{"props"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$f->{"domain"} and $f->{"properties"} || $f->{"props"}
$f->{"domain"} $f->{"properties"} $f->{"props"}
Truth table0%0/4
A and B or C
A= $f->{"domain"}B= $f->{"properties"}C= $f->{"props"}
ABCresult
0XX0
1000
11X1
1011
1030 0 $object = $f->{domain} . ":";
1031 0 ▶ my $href = $f->{properties} || $f->{props};
Condition0%0/3
$f->{'properties'} || $f->{'props'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$f->{'properties'} || $f->{'props'}
$f->{'properties'} $f->{'props'}
Truth table0%0/3
A || B
A= $f->{'properties'}B= $f->{'props'}
ABresult
1X1
011
000
1032 0 ▶ croak "'properties' is not a hashref" unless ref($href);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $href 0 0 0
1033 0 for my $k (keys %{$href}) {
1034 0 $object .= $k . "=" . $href->{$k};
1035 }
1036 }
1037 0 return $object;
1038 }
1039  
1040 3 ▶sub _create_handler {
Subroutine _create_handler: called CC=4 SCAR=13.9
1041 3 my $self = shift;
1042 3 ▶ if (!$self->{product}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"product"}) 2 1 3
1043 2 ($self->{product},$self->{product_handler}) = $self->_autodetect_product();
1044 }
1045 # Create product handler if not created during autodetectiong (e.g. if the
1046 # product has been set explicitely)
1047 3 ▶ $self->{product_handler} = $self->_new_handler($self->{product}) unless $self->{product_handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"product_handler"} 1 2 3
1048 3 ▶ croak "Cannot autodetect server product" unless $self->{product};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"product"} 0 3 3
1049 3 return $self->{product_handler};
1050 }
1051  
1052 2 ▶sub _autodetect_product {
Subroutine _autodetect_product: called CC=3 SCAR=11.1
1053 2 my $self = shift;
1054 2 for my $id (@PRODUCT_HANDLER_ORDERING) {
1055  
1056 4 my $handler = $self->_new_handler($id);
1057 4 ▶ return ($id,$handler) if $handler->autodetect();
Branch: 1 branch
BranchTrueFalseTotal
if $handler->autodetect 2 2 4
1058 }
1059 0 return undef;
1060 }
1061  
1062 5 ▶sub _new_handler {
Subroutine _new_handler: called CC=2 SCAR=7.0
1063 5 my $self = shift;
1064 5 my $product = shift;
1065  
1066 5 my $handler = eval $PRODUCT_HANDLER{$product}."->new(\$self)";
1067 5 ▶ croak "Cannot create handler ",$self->{product},": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
1068 5 return $handler;
1069 }
1070  
1071  
1072 4 my $SPACE = 4;
1073 4 my @SEPS = (":");
1074 4 my $CURRENT_DOMAIN = "";
1075  
1076 0 ▶sub _format_map {
Subroutine _format_map: not called CC=15 SCAR=54.8
1077 0 my ($ret,$map,$path,$level) = @_;
1078  
1079 0 my $p = shift @$path;
1080 0 ▶ my $sep = $SEPS[$level] ? $SEPS[$level] : "";
Branch: 1 branch
BranchTrueFalseTotal
$SEPS[$level] ? : 0 0 0
1081 0 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) { } 0 0 0
1082 0 $ret .= "$p".$sep;
1083 0 ▶ if (!@$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$path) 0 0 0
1084 0 my $s = length($ret);
1085 0 $ret .= "\n".("=" x length($ret))."\n\n";
1086 }
1087 0 $ret = &_format_map($ret,$map,$path,$level);
1088 } else {
1089 0 for my $d (sort(keys(%$map))) {
1090 0 my $prefix = "";
1091 0 ▶ if ($level == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($level == 0) { } 0 0 0
elsif ($level == 1) { } 0 0 0
1092 0 $CURRENT_DOMAIN = $d;
1093 } elsif ($level == 1) {
1094 0 $prefix = $CURRENT_DOMAIN . ":";
1095 }
1096 0 ▶ $ret .= &_get_space($level).$prefix.$d.$sep."\n" unless ($d =~ /^(attr|op|class|desc|error)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $d =~ /^(attr|op|class|desc|error)$/ 0 0 0
1097 0 my @args = ($ret,$map->{$d},$path);
1098 0 ▶ if ($d eq "attr") {
Branch: 5 branches
BranchTrueFalseTotal
if ($d eq 'attr') { } 0 0 0
elsif ($d eq 'op') { } 0 0 0
elsif ($d eq 'class') { } 0 0 0
elsif ($d eq 'desc') { } 0 0 0
elsif ($d eq 'error') { } 0 0 0
1099 0 $ret = &_format_attr_or_op(@args,$level,"attr","Attributes",\&_format_attribute);
1100 } elsif ($d eq "op") {
1101 0 $ret = &_format_attr_or_op(@args,$level,"op","Operations",\&_format_operation);
1102 } elsif ($d eq "class") {
1103 0 $ret .= &_get_space($level).$prefix."Class: ".$map->{$d}."\n";
1104 } elsif ($d eq "desc") {
1105 0 $ret .= &_get_space($level).$prefix."Description: ".$map->{$d}."\n";
1106 } elsif ($d eq "error") {
1107 0 $ret = $ret . "\nError: ".$map->{error}->{message}."\n";
1108 } else {
1109 0 $ret = &_format_map(@args,$level+1);
1110 0 ▶ if ($level == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($level == 0) { } 0 0 0
elsif ($level == 1) { } 0 0 0
1111 0 $ret .= "-" x 80 . "\n";
1112 } elsif ($level == 1) {
1113 0 $ret .= "\n";
1114 }
1115 }
1116 }
1117 }
1118 0 return $ret;
1119 }
1120  
1121 0 ▶sub _format_attr_or_op {
Subroutine _format_attr_or_op: not called CC=4 SCAR=30.0
1122 0 my ($ret,$map,$path,$level,$top_key,$label,$format_sub) = @_;
1123  
1124 0 my $p = shift @$path;
1125 0 ▶ if ($p eq $top_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq $top_key) { } 0 0 0
1126 0 $p = shift @$path;
1127 0 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) { } 0 0 0
1128 0 $ret .= " ".$p."\n";
1129 0 return $format_sub->($ret,$p,$map->{$p},$level);
1130 } else {
1131 0 $ret .= " $label:\n";
1132 }
1133 } else {
1134 0 $ret .= &_get_space($level)."$label:\n";
1135 }
1136 0 for my $key (sort(keys(%$map))) {
1137 0 $ret = $format_sub->($ret,$key,$map->{$key},$level+1);
1138 }
1139 0 return $ret;
1140 }
1141  
1142 0 ▶sub _format_attribute {
Subroutine _format_attribute: not called CC=3 SCAR=24.8
1143 0 my ($ret,$name,$attr,$level) = @_;
1144 0 $ret .= &_get_space($level);
1145 0 ▶ $ret .= sprintf("%-35s %s\n",$name,$attr->{type}.((!$attr->{rw} || "false" eq lc $attr->{rw}) ? " [ro]" : "").", \"".$attr->{desc}."\"");
Branch: 1 branch
BranchTrueFalseTotal
!$attr->{'rw'} || 'false' eq lc $attr->{'rw'} ? : 0 0 0
Condition0%0/3
not $attr->{"rw"} or "false" eq lc $attr->{"rw"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $attr->{"rw"} or "false" eq lc $attr->{"rw"}
not $attr->{"rw"} "false" eq lc $attr->{"rw"}
Truth table0%0/3
not(A) or B
A= not $attr->{"rw"}B= "false" eq lc $attr->{"rw"}
ABresult
1X1
011
000
1146 0 return $ret;
1147 }
1148  
1149 0 ▶sub _format_operation {
Subroutine _format_operation: not called CC=4 SCAR=30.0
1150 0 my ($ret,$name,$op,$level) = @_;
1151 0 $ret .= &_get_space($level);
1152 0 ▶ my $list = ref($op) eq "HASH" ? [ $op ] : $op;
Branch: 1 branch
BranchTrueFalseTotal
ref $op eq 'HASH' ? : 0 0 0
1153 0 my $first = 1;
1154 0 for my $o (@$list) {
1155 0 my $method = &_format_method($name,$o->{args},$o->{ret});
1156 0 ▶ $ret .= &_get_space($level) unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 0 0
1157 0 $ret .= sprintf("%-35s \"%s\"\n",$method,$o->{desc});
1158 0 $first = 0;
1159 }
1160 0 return $ret;
1161 }
1162  
1163 0 ▶sub _format_method {
Subroutine _format_method: not called CC=4 SCAR=30.0
1164 0 my ($name,$args,$ret_type) = @_;
1165 0 my $ret = $ret_type." ".$name."(";
1166 0 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 0 0 0
1167 0 for my $a (@$args) {
1168 0 $ret .= $a->{type} . " " . $a->{name} . ",";
1169 }
1170 0 ▶ chop $ret if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
1171 }
1172 0 $ret .= ")";
1173 0 return $ret;
1174 }
1175  
1176 0 ▶sub _get_space {
Subroutine _get_space: not called CC=1 SCAR=6.9
1177 0 my $level = shift;
1178 0 return " " x ($level * $SPACE);
1179 }
1180  
1181 23 ▶sub cfg {
Subroutine cfg: called CC=2 SCAR=7.1
Pod undocumented
1182 23 my $self = shift;
1183 23 my $key = shift;
1184 23 my $val = shift;
1185 23 my $ret = $self->{cfg}->{$key};
1186 23 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 23 23
1187 0 $self->{cfg}->{$key} = $val;
1188 }
1189 23 return $ret;
1190 }
1191  
1192 # ==========================================================================
1193 # Methods used for overwriting
1194  
1195 # Init method called during construction
1196 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=0.0
Pod undocumented
1197 # Do nothing by default
1198 }
1199  
1200 # ==========================================================================
1201 #
1202  
1203 5 ▶sub autodiscover_mode {
Subroutine autodiscover_mode: called CC=1 SCAR=0.0
Pod undocumented
1204  
1205 # For now, only *one* mode is supported. Additional
1206 # could be added (like calling up a local JVM)
1207 5 return "agent";
1208 }
1209  
1210 =back
1211  
1212 =head1 LICENSE
1213  
1214 This file is part of jmx4perl.
1215  
1216 Jmx4perl is free software: you can redistribute it and/or modify
1217 it under the terms of the GNU General Public License as published by
1218 the Free Software Foundation, either version 2 of the License, or
1219 (at your option) any later version.
1220  
1221 jmx4perl is distributed in the hope that it will be useful,
1222 but WITHOUT ANY WARRANTY; without even the implied warranty of
1223 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1224 GNU General Public License for more details.
1225  
1226 You should have received a copy of the GNU General Public License
1227 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
1228  
1229 A commercial license is available as well. Please contact roland@cpan.org for
1230 further details.
1231  
1232 =head1 PROFESSIONAL SERVICES
1233  
1234 Just in case you need professional support for this module (or Nagios or JMX in
1235 general), you might want to have a look at
1236 http://www.consol.com/opensource/nagios/. Contact roland.huss@consol.de for
1237 further information (or use the contact form at http://www.consol.com/contact/)
1238  
1239 =head1 AUTHOR
1240  
1241 roland@cpan.org
1242  
1243 =cut
1244  
1245 4 1;
« Jmx4Perl/Nagios/MessageHandler.pm ↑ Summary Jmx4Perl/Nagios/CheckJmx4Perl.pm »