Jmx4Perl/Nagios/CheckJmx4Perl.pm

filter: Statementstmt 28.3% 131 / 462 Branchbran 6.1% 10 / 164 Conditioncond 8.1% 7 / 86 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 45.6% 21 / 46 Podpod 11.1% 1 / 9 totaltotal 20.9% 170 / 813 CC 131 SCAR 45.2
CC
131
Cov
21%
_replace_placeholder
380.0
execute
210.0
_resolve_check_config
156.0
CRAP 91.5
« Jmx4Perl.pm ↑ Summary Jmx4Perl/J4psh/CompletionHandler.pm »
1 package JMX::Jmx4Perl::Nagios::CheckJmx4Perl;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JMX::Jmx4Perl::Nagios::SingleCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JMX::Jmx4Perl::Nagios::MessageHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JMX::Jmx4Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JMX::Jmx4Perl::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JMX::Jmx4Perl::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Monitoring::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Monitoring::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $AUTOLOAD;
18  
19 =head1 NAME
20  
21 JMX::Jmx4Perl::Nagios::CheckJmx4Perl - Module for encapsulating the functionality of
22 L<check_jmx4perl>
23  
24 =head1 SYNOPSIS
25  
26 # One line in check_jmx4perl to rule them all
27 JMX::Jmx4Perl::Nagios::CheckJmx4Perl->new()->execute();
28  
29 =head1 DESCRIPTION
30  
31 The purpose of this module is to encapsulate a single run of L<check_jmx4perl>
32 in a perl object. This allows for C<check_jmx4perl> to run within the embedded
33 Nagios perl interpreter (ePN) wihout interfering with other, potential
34 concurrent, runs of this check. Please refer to L<check_jmx4perl> for
35 documentation on how to use this check. This module is probably I<not> of
36 general interest and serves only the purpose described above.
37  
38 Its main task is to set up one ore more L<JMX::Jmx4Perl::Nagios::SingleCheck>
39 objects from command line arguments and optionally from a configuration file.
40  
41 =head1 METHODS
42  
43 =over
44  
45 =item $check = new $JMX::Jmx4Perl::Nagios::CheckJmx4Perl()
46  
47 Set up a object used for a single check. It will parse the command line
48 arguments and any configuation file given.
49  
50 =cut
51  
52 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
53 1 my $class = shift;
54 1 my $self = { };
55 1 ▶ bless $self,(ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
56 1 $self->{np} = $self->create_nagios_plugin();
57 1 $self->{cmd_args} = [ @ARGV ];
58  
59 1 ▶ $self->_print_doc_and_exit($self->{np}->opts->{doc}) if defined $self->{np}->opts->{doc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"np"}->opts->{"doc"} 0 1 1
60 1 $self->_verify_and_initialize();
61 1 return $self;
62 }
63  
64 =back
65  
66 =head1 $check->execute()
67  
68 Send the JMX request to the server monitored and print out a nagios output.
69  
70 =cut
71  
72 0 ▶sub execute {
Subroutine execute: not called CC=14 SCAR=53.5
Pod undocumented
73 0 my $self = shift;
74 0 my $np = $self->{np};
75 0 eval {
76 # Request
77 0 my @optional = ();
78  
79 0 my $error_stat = { };
80 0 my $target_config = $self->target_config;
81 my $jmx = JMX::Jmx4Perl->new(mode => "agent", url => $self->url, user => $self->user,
82 password => $self->password,
83 product => $self->product,
84 proxy => $self->proxy_config,
85 0 ▶ timeout => $np->opts->{timeout} || 180,
Condition0%0/2
$np->opts->{'timeout'} || 180
l!l
00
MC/DC0%0/1
$np->opts->{'timeout'} || 180
$np->opts->{'timeout'}
Truth table0%0/2
A
A= $np->opts->{'timeout'}
Aresult
11
00
86 target => $target_config,
87 # For Jolokia agents < 1.0
88 'legacy-escape' => $self->legacy_escape);
89 0 my @requests;
90 0 for my $check (@{$self->{checks}}) {
91 0 push @requests,@{$check->get_requests($jmx,\@ARGV)};
92 }
93 0 my $responses = $self->_send_requests($jmx,@requests);
94 #print Dumper($responses);
95 0 my @extra_requests = ();
96 0 my $nr_checks = scalar(@{$self->{checks}});
97 0 ▶ if ($nr_checks == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr_checks == 1) { } 0 0 0
98 0 eval {
99 0 my @r = $self->{checks}->[0]->extract_responses($responses,\@requests,{ target => $target_config });
100 0 ▶ push @extra_requests,@r if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
101 };
102 0 ▶ $self->nagios_die($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
103 } else {
104 0 my $i = 1;
105 0 for my $check (@{$self->{checks}}) {
106 # A check can consume more than one response
107 0 my $prefix = $self->_multi_check_prefix($check,$i++,$nr_checks);
108 0 eval {
109 0 my @r = $check->extract_responses($responses,\@requests,
110 {
111 target => $target_config,
112 prefix => $prefix,
113 error_stat => $error_stat
114 });
115 0 ▶ push @extra_requests,@r if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
116 };
117 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
118 0 my $txt = $@;
119 0 $txt =~ s/^(.*?)\n.*$/$1/s;
120 0 ▶ my $code = $np->opts->{'unknown-is-critical'} ? CRITICAL : UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
$np->opts->{'unknown-is-critical'} ? : 0 0 0
121 0 $check->update_error_stats($error_stat,$code);
122 0 $prefix =~ s/\%c/$STATUS_TEXT{$code}/g;
123 0 ▶ my $msg_handler = $np->{msg_handler} || $np;
Condition0%0/3
$np->{'msg_handler'} || $np
l!l&&r!l&&!r
000
MC/DC0%0/2
$np->{'msg_handler'} || $np
$np->{'msg_handler'} $np
Truth table0%0/3
A || B
A= $np->{'msg_handler'}B= $np
ABresult
1X1
011
000
124 0 $msg_handler->add_message($code,$prefix . $txt);
125 }
126 }
127 }
128 # Send extra requests, e.g. for switching on the history
129 0 ▶ if (@extra_requests) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra_requests) 0 0 0
130 0 $self->_send_requests($jmx,@extra_requests);
131 }
132  
133 # Different outputs for multi checks/single checks
134 0 $self->do_exit($error_stat);
135 };
136 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
137 # p1.pl, the executing script of the embedded nagios perl interpreter
138 # uses this tag to catch an exit code of a plugin. We rethrow this
139 # exception if we detect this pattern.
140 0 ▶ if ($@ !~ /^ExitTrap:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ =~ /^ExitTrap:/) { } 0 0 0
141 0 $self->nagios_die("Error: $@");
142 } else {
143 0 die $@;
144 }
145 }
146 }
147  
148 =head1 $check->exit()
149  
150 Write out result and exit. This method can be overridden to provide a custom
151 output, which can be extracted from NagiosPlugin object.
152  
153 =cut
154  
155 0 ▶sub do_exit {
Subroutine do_exit: not called CC=3 SCAR=24.8
Pod undocumented
156 0 my $self = shift;
157 0 my $error_stat = shift;
158 0 my $np = $self->{np};
159  
160 0 ▶ my $msg_handler = $np->{msg_handler} || $np;
Condition0%0/3
$np->{'msg_handler'} || $np
l!l&&r!l&&!r
000
MC/DC0%0/2
$np->{'msg_handler'} || $np
$np->{'msg_handler'} $np
Truth table0%0/3
A || B
A= $np->{'msg_handler'}B= $np
ABresult
1X1
011
000
161 0 my ($code,$message) = $msg_handler->check_messages(join => "\n", join_all => "\n");
162 0 ▶ ($code,$message) = $self->_prepare_multicheck_message($np,$code,$message,$error_stat) if scalar(@{$self->{checks}}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"checks"};} > 1 0 0 0
163
164 0 $np->nagios_exit($code, $message);
165 }
166  
167 0 ▶sub _prepare_multicheck_message {
Subroutine _prepare_multicheck_message: not called CC=5 SCAR=34.0
168 0 my $self = shift;
169 0 my $np = shift;
170 0 my $code = shift;
171 0 my $message = shift;
172 0 my $error_stat = shift;
173  
174 0 my $summary;
175 0 ▶ my $labels = $self->{multi_check_labels} || {};
Condition0%0/2
$self->{'multi_check_labels'} || {}
l!l
00
MC/DC0%0/1
$self->{'multi_check_labels'} || {}
$self->{'multi_check_labels'}
Truth table0%0/2
A
A= $self->{'multi_check_labels'}
Aresult
11
00
176 0 my $nr_checks = scalar(@{$self->{checks}});
177 0 $code = $self->_check_for_UNKNOWN($error_stat,$code);
178 0 ▶ if ($code eq OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq 0) { } 0 0 0
179 $summary = $self->_format_multicheck_ok_summary($labels->{summary_ok} ||
180 0 ▶ "All %n checks OK",$nr_checks);
Condition0%0/2
$labels->{'summary_ok'} || 'All %n checks OK'
l!l
00
MC/DC0%0/1
$labels->{'summary_ok'} || 'All %n checks OK'
$labels->{'summary_ok'}
Truth table0%0/2
A
A= $labels->{'summary_ok'}
Aresult
11
00
181 } else {
182 $summary = $self->_format_multicheck_failure_summary($labels->{summary_failure} ||
183 0 ▶ "%e of %n checks failed [%d]",
Condition0%0/2
$labels->{'summary_failure'} || '%e of %n checks failed [%d]'
l!l
00
MC/DC0%0/1
$labels->{'summary_failure'} || '%e of %n checks failed [%d]'
$labels->{'summary_failure'}
Truth table0%0/2
A
A= $labels->{'summary_failure'}
Aresult
11
00
184 $nr_checks,
185 $error_stat);
186 }
187 0 return ($code,$summary . "\n" . $message);
188 }
189  
190 # UNKNOWN shadows everything else
191 0 ▶sub _check_for_UNKNOWN {
Subroutine _check_for_UNKNOWN: not called CC=3 SCAR=24.8
192 0 my $self = shift;
193 0 my $error_stat = shift;
194 0 my $code = shift;
195 0 ▶ return $error_stat->{UNKNOWN} && scalar(@$error_stat->{UNKNOWN}) ? UNKNOWN : $code;
Branch: 1 branch
BranchTrueFalseTotal
$error_stat->{'UNKNOWN'} && scalar @$error_stat->{'UNKNOWN'} ? : 0 0 0
Condition0%0/2
$error_stat->{"UNKNOWN"} and scalar @$error_stat->{"UNKNOWN"}
!ll
00
MC/DC0%0/1
$error_stat->{"UNKNOWN"} and scalar @$error_stat->{"UNKNOWN"}
$error_stat->{"UNKNOWN"}
Truth table0%0/2
A
A= $error_stat->{"UNKNOWN"}
Aresult
00
11
196 }
197  
198 0 ▶sub _format_multicheck_ok_summary {
Subroutine _format_multicheck_ok_summary: not called CC=1 SCAR=6.9
199 0 my $self = shift;
200 0 my $format = shift;
201 0 my $nr_checks = shift;
202 0 my $ret = $format;
203 0 $ret =~ s/\%n/$nr_checks/g;
204 0 return $ret;
205 }
206  
207 0 ▶sub _format_multicheck_failure_summary {
Subroutine _format_multicheck_failure_summary: not called CC=4 SCAR=30.0
208 0 my $self = shift;
209 0 my $format = shift;
210 0 my $nr_checks = shift;
211 0 my $error_stat = shift;
212  
213 0 my $ret = $format;
214  
215 0 my $details = "";
216 0 my $total_errors = 0;
217 0 for my $code (UNKNOWN,CRITICAL,WARNING) {
218 0 ▶ if (my $errs = $error_stat->{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errs = $error_stat->{$code}) 0 0 0
219 0 $details .= scalar(@$errs) . " " . $STATUS_TEXT{$code} . " (" . join (",",@$errs) . "), ";
220 0 $total_errors += scalar(@$errs);
221 }
222 }
223 0 ▶ if ($total_errors > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_errors > 0) 0 0 0
224 # Cut off extra chars at the end
225 0 $details = substr($details,0,-2);
226 }
227
228 0 $ret =~ s/\%d/$details/g;
229 0 $ret =~ s/\%e/$total_errors/g;
230 0 $ret =~ s/\%n/$nr_checks/g;
231 0 return $ret;
232 }
233  
234 # Create a formatted prefix for multicheck output
235 0 ▶sub _multi_check_prefix {
Subroutine _multi_check_prefix: not called CC=6 SCAR=37.4
236 0 my $self = shift;
237 0 my $check = shift;
238 0 my $idx = shift;
239 0 my $max = shift;
240
241 0 my $c = $check->{config};
242  
243 0 my $l = length($max);
244
245 return sprintf("[%$l.${l}s] %%c ",$idx)
246 0 ▶ if (defined($c->{multicheckprefix}) && !length($c->{multicheckprefix}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"multicheckprefix"} and not length $c->{"multicheckprefix"} 0 0 0
Condition0%0/3
defined $c->{"multicheckprefix"} and not length $c->{"multicheckprefix"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c->{"multicheckprefix"} and not length $c->{"multicheckprefix"}
defined $c->{"multicheckprefix"} not length $c->{"multicheckprefix"}
Truth table0%0/3
A and not(B)
A= defined $c->{"multicheckprefix"}B= not length $c->{"multicheckprefix"}
ABresult
0X0
100
111
247
248 0 ▶ my $label = $c->{multicheckprefix} || $c->{name} || $c->{key} || "";
Condition0%0/8
$c->{'multicheckprefix'} || $c->{'name'} || $c->{'key'} || ''
l!l
00
$c->{"multicheckprefix"} or $c->{"name"} or $c->{"key"}
l!l&&r!l&&!r
000
$c->{"multicheckprefix"} or $c->{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$c->{'multicheckprefix'} || $c->{'name'} || $c->{'key'} || ''
$c->{"multicheckprefix"} $c->{"name"} $c->{"key"}
Truth table0%0/4
A or B or C
A= $c->{"multicheckprefix"}B= $c->{"name"}C= $c->{"key"}
ABCresult
1XX1
01X1
0011
0000
249 0 return sprintf("[%$l.${l}s] %%c %s: ",$idx,$label);
250 }
251  
252  
253 # Send the requests via the build up agent
254 0 ▶sub _send_requests {
Subroutine _send_requests: not called CC=4 SCAR=30.0
255 0 my ($self,$jmx,@requests) = @_;
256 0 my $o = $self->{opts};
257  
258 0 my $start_time;
259 0 ▶ if ($o->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->verbose) 0 0 0
260 # TODO: Print summary of request (GET vs POST)
261 0 ▶ if ($self->user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user) 0 0 0
262 0 print "Remote User: ",$o->user,"\n";
263 }
264 0 $start_time = [gettimeofday];
265 }
266 # Detangle request for direct method calls and JMX requests to call:
267 0 my $req_map = $self->_detangle_requests(\@requests);
268 0 my @responses = ();
269
270 0 $self->_execute_scripts(\@responses,$req_map);
271 0 $self->_execute_requests(\@responses,$req_map,$jmx);
272  
273 0 ▶ if ($o->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->verbose) 0 0 0
274 0 print "Result fetched in ",tv_interval($start_time) * 1000," ms:\n";
275 0 print Dumper(\@responses);
276 }
277 0 return \@responses;
278 }
279  
280 # Split up request for code-requests (i.e. scripts given in the configuration)
281 # and 'real' requests. Remember the index, too so that the response can be
282 # weave together
283 0 ▶sub _detangle_requests {
Subroutine _detangle_requests: not called CC=3 SCAR=24.8
284 0 my $self = shift;
285 0 my $requests = shift;
286 0 my $req_map = {};
287 0 my $idx = 0;
288 0 for my $r (@$requests) {
289 0 ▶ push @{$req_map->{ref($r) eq "CODE" ? "code" : "request"}},[$r,$idx];
Branch: 1 branch
BranchTrueFalseTotal
ref $r eq 'CODE' ? : 0 0 0
290 0 $idx++;
291 }
292 0 return $req_map;
293 }
294  
295 # Execute subrefs created out of scripts. Put it in the right place of the
296 # result array according to the remembered index
297 0 ▶sub _execute_scripts {
Subroutine _execute_scripts: not called CC=2 SCAR=17.9
298 0 my $self = shift;
299 0 my $responses = shift;
300 0 my $req_map = shift;
301 0 for my $e (@{$req_map->{"code"}}) {
302 # Will die on error which will bubble up
303 0 $responses->[$e->[1]] = &{$e->[0]}();;
304 }
305 }
306  
307 # Execute requests and put it in the received responses in the right place for
308 # the returned array. The index has been extracted beforehand and stored in the
309 # given req_map
310 0 ▶sub _execute_requests {
Subroutine _execute_requests: not called CC=3 SCAR=24.8
311 0 my $self = shift;
312 0 my $responses = shift;
313 0 my $req_map = shift;
314 0 my $jmx = shift;
315  
316 # Call remote JMX and weave in
317 0 my $reqs2send = $req_map->{"request"};
318 0 ▶ if ($reqs2send) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqs2send) 0 0 0
319 0 my @resp_received = $jmx->request(map { $_->[0] } @$reqs2send);
320 0 for my $r (@$reqs2send) {
321 0 $responses->[$r->[1]] = shift @resp_received;
322 }
323 }
324 }
325  
326  
327 # Print online manual and exit (somewhat crude, I know)
328 0 ▶sub _print_doc_and_exit {
Subroutine _print_doc_and_exit: not called CC=4 SCAR=30.0
329 0 my $self = shift;
330 0 my $section = shift;
331 0 ▶ if (!eval "require Pod::Usage; Pod::Usage->import(qw(pod2usage)); 1;") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require Pod::Usage; Pod::Usage->import(qw(pod2usage)); 1;") 0 0 0
332 0 print "Please install Pod::Usage for creating the online help\n";
333 0 exit 1;
334 }
335 0 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) { } 0 0 0
336 0 my %sects = (
337 tutorial => "TUTORIAL",
338 reference => "REFERENCE",
339 options => "COMMAND LINE",
340 config => "CONFIGURATION",
341 );
342 0 my $real_section = $sects{lc $section};
343 0 ▶ if ($real_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real_section) { } 0 0 0
344 0 pod2usage(-verbose => 99, -sections => $real_section );
345 } else {
346 0 print "Unknown documentation section '$section' (known: ",join (",",sort keys %sects),")\n";
347 0 exit 1;
348 }
349 } else {
350 0 pod2usage(-verbose => 99);
351 }
352 }
353  
354  
355 # Initialize this object and validate the mandatory parameters (obtained from
356 # the command line or a configuration file). It will also build up
357 # one or more SingleCheck which are later on sent as a bulk request to
358 # the server.
359 1 ▶sub _verify_and_initialize {
Subroutine _verify_and_initialize: called CC=14 SCAR=35.7
360 1 my $self = shift;
361 1 my $np = $self->{np};
362 1 my $o = $np->opts;
363
364 1 $self->{opts} = $self->{np}->opts;
365  
366 # Fetch configuration
367 1 my $config = $self->_get_config($o->config);
368 # Now, if a specific check is given, extract it, too.
369 1 my $check_configs;
370 #print Dumper($config);
371 1 $check_configs = $self->_extract_checks($config,$o->check);
372 #print Dumper($check_configs);
373 1 ▶ if ($check_configs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_configs) { } 0 1 1
374 0 for my $c (@$check_configs) {
375 0 my $s_c = new JMX::Jmx4Perl::Nagios::SingleCheck($np,$c);
376 0 push @{$self->{checks}},$s_c;
377 }
378 } else {
379 1 $self->{checks} = [ new JMX::Jmx4Perl::Nagios::SingleCheck($np) ];
380 }
381 # If a server name is given, we use that for the connection parameters
382 1 ▶ if ($o->server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->server) 0 1 1
383 0 ▶ $self->{server_config} = $config->get_server_config($o->server)
Condition0%0/3
$config->get_server_config($o->server) || $self->nagios_die('No server configuration for ' . $o->server . ' found')
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->get_server_config($o->server) || $self->nagios_die('No server configuration for ' . $o->server . ' found')
$config->get_server_config($o->server) $self->nagios_die('No server configuration for ' . $o->server . ' found')
Truth table0%0/3
A || B
A= $config->get_server_config($o->server)B= $self->nagios_die('No server configuration for ' . $o->server . ' found')
ABresult
1X1
011
000
384 || $self->nagios_die("No server configuration for " . $o->server . " found");
385 }
386  
387 # Sanity checks
388 1 ▶ $self->nagios_die("No Server URL given") unless $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->url 0 1 1
389  
390 1 for my $check (@{$self->{checks}}) {
391 1 ▶ my $name = $check->name ? " [Check: " . $check->name . "]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$check->name ? : 1 0 1
392 1 ▶ $self->nagios_die("An MBean name and a attribute/operation must be provided " . $name)
Branch: 1 branch
BranchTrueFalseTotal
unless $check->mbean and $check->attribute || $check->operation or $check->alias or $check->value or $check->script 0 1 1
Condition33%5/15
$check->mbean and $check->attribute || $check->operation or $check->alias or $check->value or $check->script
l!l&&r!l&&!r
100
$check->mbean and $check->attribute || $check->operation or $check->alias or $check->value
l!l&&r!l&&!r
100
$check->mbean and $check->attribute || $check->operation or $check->alias
l!l&&r!l&&!r
100
$check->mbean and $check->attribute || $check->operation
!ll&&!rl&&r
001
$check->attribute or $check->operation
l!l&&r!l&&!r
100
MC/DC0%0/6
$check->mbean and $check->attribute || $check->operation or $check->alias or $check->value or $check->script
$check->mbean $check->attribute $check->operation $check->alias $check->value $check->script
Truth table10%1/10
A and B or C or D or E or F
A= $check->mbeanB= $check->attributeC= $check->operationD= $check->aliasE= $check->valueF= $check->script
ABCDEFresult
11XXXX1
101XXX1
0XX1XX1
1001XX1
0XX01X1
10001X1
0XX0011
1000011
0XX0000
1000000
393 if ((!$check->mbean || (!$check->attribute && !$check->operation)) && !$check->alias && !$check->value && !$check->script);
394 }
395 }
396  
397 # Extract one or more check configurations which can be
398 # simple <Check>s or <MultiCheck>s
399 1 ▶sub _extract_checks {
Subroutine _extract_checks: called CC=10 SCAR=41.5
400 1 my $self = shift;
401 1 my $config = shift;
402 1 my $check = shift;
403
404 1 my $np = $self->{np};
405 1 ▶ if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 0 1 1
406 0 ▶ $self->nagios_die("No configuration given") unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 0 0
407 0 ▶ $self->nagios_die("No checks defined in configuration") unless $config->{check};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"check"} 0 0 0
408  
409 0 my $check_configs;
410 0 ▶ unless ($config->{check}->{$check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'check'}{$check}) { } 0 0 0
411 0 $check_configs = $self->_resolve_multicheck($config,$check,$self->{cmd_args});
412 0 $self->_retrieve_mc_summary_label($config,$check);
413 } else {
414 0 my $check_config = $config->{check}->{$check};
415 0 ▶ $check_configs = ref($check_config) eq "ARRAY" ? $check_config : [ $check_config ];
Branch: 1 branch
BranchTrueFalseTotal
ref $check_config eq 'ARRAY' ? : 0 0 0
416 0 $check_configs->[0]->{key} = $check;
417 }
418 0 ▶ $self->nagios_die("No check configuration with name " . $check . " found") unless (@{$check_configs});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$check_configs;} 0 0 0
419  
420 #print Dumper($check_configs);
421  
422 # Resolve parent values
423 0 for my $c (@{$check_configs}) {
424 #print "[A] ",Dumper($c);
425 0 $self->_resolve_check_config($c,$config,$self->{cmd_args});
426
427 #print "[B] ",Dumper($c);
428 # Finally, resolve any left over place holders
429 0 for my $k (keys(%$c)) {
430 0 ▶ $c->{$k} = $self->_replace_placeholder($c->{$k},undef) unless ref($c->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c->{$k} 0 0 0
431 }
432 #print "[C] ",Dumper($c);
433 }
434 0 return $check_configs;
435 } else {
436 1 return undef;
437 }
438 }
439  
440 # Resolve a multicheck configuration (<MultiCheck>)
441 0 ▶sub _resolve_multicheck {
Subroutine _resolve_multicheck: not called CC=10 SCAR=47.0
442 0 my $self = shift;
443 0 my $config = shift;
444 0 my $check = shift;
445 0 my $args = shift;
446 0 my $np = $self->{np};
447 0 my $multi_checks = $config->{multicheck};
448 0 my $check_config = [];
449 0 ▶ if ($multi_checks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi_checks) 0 0 0
450 0 my $m_check = $multi_checks->{$check};
451 0 ▶ if ($m_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_check) 0 0 0
452 # Resolve all checks
453 0 my $c_names = [];
454 0 for my $type( qw(check multicheck)) {
455 0 ▶ if ($m_check->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_check->{$type}) 0 0 0
456 0 ▶ push @$c_names, ref($m_check->{$type}) eq "ARRAY" ? @{$m_check->{$type}} : $m_check->{$type};
Branch: 1 branch
BranchTrueFalseTotal
ref $m_check->{$type} eq 'ARRAY' ? : 0 0 0
457 }
458 }
459 0 for my $name (@$c_names) {
460 0 my ($c_name,$c_args) = $self->_parse_check_ref($name);
461 0 my $args_merged = $self->_merge_multicheck_args($c_args,$args);
462 $self->nagios_die("Unknown check '" . $c_name . "' for multi check " . $check)
463 0 ▶ unless defined($config->{check}->{$c_name}) or defined($multi_checks->{$c_name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"check"}{$c_name} or defined $multi_checks->{$c_name} 0 0 0
Condition0%0/3
defined $config->{"check"}{$c_name} or defined $multi_checks->{$c_name}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $config->{"check"}{$c_name} or defined $multi_checks->{$c_name}
defined $config->{"check"}{$c_name} defined $multi_checks->{$c_name}
Truth table0%0/3
A or B
A= defined $config->{"check"}{$c_name}B= defined $multi_checks->{$c_name}
ABresult
1X1
011
000
464 0 ▶ if ($config->{check}->{$c_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'check'}{$c_name}) { } 0 0 0
465 # We need a copy of the check hash to avoid mangling it up
466 # if it is referenced multiple times
467 0 my $check = { %{$config->{check}->{$c_name}} };
468 0 $check->{key} = $c_name;
469 0 $check->{args} = $args_merged;
470 0 push @{$check_config},$check;
471 } else {
472 # It's a multi check referenced via <Check> or <MultiCheck> ....
473 0 push @{$check_config},@{$self->_resolve_multicheck($config,$c_name,$args_merged)};
474 }
475 }
476 }
477 }
478 0 return $check_config;
479 }
480  
481 0 ▶sub _retrieve_mc_summary_label {
Subroutine _retrieve_mc_summary_label: not called CC=5 SCAR=34.0
482 0 my $self = shift;
483 0 my $config = shift;
484 0 my $check = shift;
485  
486 0 my $multi_checks = $config->{multicheck};
487 0 ▶ if ($multi_checks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi_checks) 0 0 0
488 0 my $m_check = $multi_checks->{$check};
489 0 ▶ if ($m_check && ($m_check->{summaryok} || $m_check->{summaryfailure})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m_check and $m_check->{"summaryok"} || $m_check->{"summaryfailure"}) 0 0 0
Condition0%0/6
$m_check and $m_check->{"summaryok"} || $m_check->{"summaryfailure"}
!ll&&!rl&&r
000
$m_check->{"summaryok"} or $m_check->{"summaryfailure"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$m_check and $m_check->{"summaryok"} || $m_check->{"summaryfailure"}
$m_check $m_check->{"summaryok"} $m_check->{"summaryfailure"}
Truth table0%0/4
A and B or C
A= $m_checkB= $m_check->{"summaryok"}C= $m_check->{"summaryfailure"}
ABCresult
0XX0
1000
11X1
1011
490 my $mc_labels =
491 $self->{multi_check_labels} = {
492 summary_ok => $m_check->{summaryok},
493 summary_failure => $m_check->{summaryfailure}
494 0 };
495 }
496 }
497 }
498  
499 0 ▶sub _merge_multicheck_args {
Subroutine _merge_multicheck_args: not called CC=6 SCAR=37.4
500 0 my $self = shift;
501 0 my $check_params = shift;
502 0 my $args = shift;
503 0 ▶ if (!$args || !$check_params) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args and $check_params) 0 0 0
Condition0%0/3
$args and $check_params
!ll&&!rl&&r
000
MC/DC0%0/2
$args and $check_params
$args $check_params
Truth table0%0/3
A and B
A= $argsB= $check_params
ABresult
0X0
100
111
504 0 return $check_params;
505 }
506 0 my $ret = [ @$check_params ]; # Copy it over
507 0 for my $i (0 .. $#$check_params) {
508 0 ▶ if ($check_params->[$i] =~ /^\$(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_params->[$i] =~ /^\$(\d+)$/) 0 0 0
509 0 my $j = $1;
510 0 ▶ if ($j <= $#$args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $args->$#*) 0 0 0
511 0 $ret->[$i] = $args->[$j];
512 0 next;
513 }
514 # Nothing to replace
515 0 $ret->[$i] = $check_params->[$i];
516 }
517 }
518 0 return $ret;
519 }
520  
521 # Resolve a singe <Check> configuration
522 0 ▶sub _resolve_check_config {
Subroutine _resolve_check_config: not called CC=12 SCAR=50.5
523 0 my $self = shift;
524 0 my $check = shift;
525 0 my $config = shift;
526 # Args can come from the outside, but also as part of a multicheck (stored
527 # in $check->{args})
528 0 ▶ my $args = $check->{args} && @{$check->{args}} ? $check->{args} : shift;
Branch: 1 branch
BranchTrueFalseTotal
$check->{'args'} && @{$check->{'args'};} ? : 0 0 0
Condition0%0/3
$check->{"args"} and @{$check->{"args"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$check->{"args"} and @{$check->{"args"};}
$check->{"args"} @{$check->{"args"};}
Truth table0%0/3
A and B
A= $check->{"args"}B= @{$check->{"args"};}
ABresult
0X0
100
111
529 0 my $np = $self->{np};
530 0 ▶ if ($check->{use}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check->{"use"}) 0 0 0
531 # Resolve parents
532 0 ▶ my $parents = ref($check->{use}) eq "ARRAY" ? $check->{use} : [ $check->{use} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $check->{'use'} eq 'ARRAY' ? : 0 0 0
533 0 my $parent_merged = {};
534 0 for my $p (@$parents) {
535 0 my ($p_name,$p_args) = $self->_parse_check_ref($p);
536 $self->nagios_die("Unknown parent check '" . $p_name . "' for check '" .
537 ($check->{key} ? $check->{key} : $check->{name}) . "'")
538 0 ▶ unless $config->{check}->{$p_name};
Branch: 2 branches
BranchTrueFalseTotal
unless $config->{"check"}{$p_name} 0 0 0
$check->{'key'} ? : 0 0 0
539 # Clone it to avoid side effects when replacing checks inline
540 0 my $p_check = { %{$config->{check}->{$p_name}} };
541 0 $p_check->{key} = $p_name;
542 #print "::::: ",Dumper($p_check,$p_args);
543  
544 0 $self->_resolve_check_config($p_check,$config,$p_args);
545  
546 #$self->_replace_args($p_check,$config,$p_args);
547 0 $parent_merged->{$_} = $p_check->{$_} for keys %$p_check;
548 }
549 # Replace inherited values
550 0 for my $k (keys %$parent_merged) {
551 0 ▶ my $parent_val = defined($parent_merged->{$k}) ? $parent_merged->{$k} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $parent_merged->{$k} ? : 0 0 0
552 0 ▶ if (defined($check->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check->{$k}) { } 0 0 0
553 0 $check->{$k} =~ s/\$BASE/$parent_val/g;
554 } else {
555 0 $check->{$k} = $parent_val;
556 }
557 }
558 }
559 0 $self->_replace_args($check,$config,$args);
560 0 return $check;
561 }
562  
563 # Replace argument placeholders with a given list of arguments
564 0 ▶sub _replace_args {
Subroutine _replace_args: not called CC=6 SCAR=37.4
565 0 my $self = shift;
566 0 my $check = shift;
567 0 my $config = shift;
568 0 my $args = shift;
569 0 for my $k (keys(%$check)) {
570 0 ▶ next if $k =~ /^(key|args)$/; # Internal keys
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(key|args)$/ 0 0 0
571 $check->{$k} =
572 $self->_replace_placeholder($check->{$k},$args)
573 0 ▶ if ($args && @$args && !ref($check->{$k}));
Branch: 1 branch
BranchTrueFalseTotal
if $args and @$args and not ref $check->{$k} 0 0 0
Condition0%0/6
$args and @$args and not ref $check->{$k}
!ll&&!rl&&r
000
$args and @$args
!ll&&!rl&&r
000
MC/DC0%0/3
$args and @$args and not ref $check->{$k}
$args @$args not ref $check->{$k}
Truth table0%0/4
A and B and not(C)
A= $argsB= @$argsC= not ref $check->{$k}
ABCresult
0XX0
10X0
1100
1111
574 }
575 }
576  
577 0 ▶sub _replace_placeholder {
Subroutine _replace_placeholder: not called CC=19 SCAR=59.4
578 0 my $self = shift;
579 0 my $val = shift;
580 0 my $args = shift;
581 0 ▶ my $index = defined($args) ? join "|",0 ... $#$args : "\\d+";
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
582  
583 0 my $regexp_s = <<'EOP';
584 ^(.*?) # Start containing no args
585  
586 \$( # Variable starts with '$'
587 ($index) | # $0 without default value
588 \{\s*($index)\s* # ${0:12300} with default value
589 (?: :([^\}]+) )*\} # ?: --> clustering group, optional (${0} is also ok)
590 )
591  
592 (.*|$) # The rest which will get parsed next
593 EOP
594 0 $regexp_s =~ s/\$index/$index/g;
595 0 my $regexp = qr/$regexp_s/sx;
596 0 ▶ die "Cannot create placeholder regexp" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
597 0 my $rest = $val;
598 0 my $ret = "";
599 0 ▶ while (defined($rest) && length($rest) && $rest =~ $regexp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest and length $rest and $rest =~ /$regexp/) 0 0 0
Condition0%0/6
defined $rest and length $rest and $rest =~ /$regexp/
!ll&&!rl&&r
000
defined $rest and length $rest
!ll&&!rl&&r
000
MC/DC0%0/3
defined $rest and length $rest and $rest =~ /$regexp/
defined $rest length $rest $rest =~ /$regexp/
Truth table0%0/4
A and B and C
A= defined $restB= length $restC= $rest =~ /$regexp/
ABCresult
0XX0
10X0
1100
1111
600 # $1: start with no placeholder
601 # $2: literal variable as it is defined
602 # $3: variable name (0,1,2,3,...)
603 # $4: same as $3, but either $3 or $4 is defined
604 # $5: default value (if any)
605 # $6: rest which is processed next in the loop
606 0 ▶ my $start = defined($1) ? $1 : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
607 0 my $orig_val = '$' . $2;
608 0 ▶ my $i = defined($3) ? $3 : $4;
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 0 0 0
609 0 my $default = $5;
610 0 ▶ my $end = defined($6) ? $6 : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $6 ? : 0 0 0
611 0 ▶ $default =~ s/^\s*(.*)+?\s*$/$1/ if $default; # Trim whitespace
Branch: 1 branch
BranchTrueFalseTotal
if $default 0 0 0
612 #print Dumper({start => $start, orig => $orig_val,end => $end, default=> $default, rest => $rest, i => $i});
613 0 ▶ if (defined($args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) { } 0 0 0
614 0 my $repl = $args->[$i];
615 0 ▶ if (defined($repl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $repl) { } 0 0 0
616 0 ▶ if ($repl =~ /^\$(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repl =~ /^\$(\d+)$/) { } 0 0 0
617 0 my $new_index = $1;
618 #print "============== $val $new_index\n";
619 # Val is a placeholder itself
620 0 ▶ if (defined($default)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 0 0 0
621 0 $ret .= $start . '${' . $new_index . ':' . $default . '}';
622 } else {
623 0 $ret .= $start . '$' . $new_index;
624 }
625 } else {
626 0 $ret .= $start . $repl;
627 }
628 } else {
629 0 $ret .= $start . $orig_val;
630 }
631 } else {
632 # We have to replace any left over placeholder either with its
633 # default value or with an empty value
634 0 ▶ if (defined($default)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $default) { } 0 0 0
elsif (length $start or length $end) { } 0 0 0
635 0 $ret .= $start . $default;
636 ▶ } elsif (length($start) || length($end)) {
Condition0%0/3
length $start or length $end
l!l&&r!l&&!r
000
MC/DC0%0/2
length $start or length $end
length $start length $end
Truth table0%0/3
A or B
A= length $startB= length $end
ABresult
1X1
011
000
637 0 $ret .= $start;
638 } else {
639 0 ▶ if (!length($ret)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $ret) 0 0 0
640 # No default value, nothing else for this value. We
641 # consider it undefined
642 0 return undef;
643 }
644 }
645 }
646 0 $rest = $end;
647 #print "... $ret$rest\n";
648 }
649 0 ▶ return $ret . (defined($rest) ? $rest : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $rest ? : 0 0 0
650 }
651  
652 # Split up a 'Use' parent config reference, including possibly arguments
653 0 ▶sub _parse_check_ref {
Subroutine _parse_check_ref: not called CC=2 SCAR=17.9
654 0 my $self = shift;
655 0 my $check_ref = shift;
656 0 ▶ if ($check_ref =~/^\s*(.+?)\((.*)\)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_ref =~ /^\s*(.+?)\((.*)\)\s*$/) { } 0 0 0
657 0 my $name = $1;
658 0 my $args_s = $2;
659 0 my $args = [ parse_line('\s*,\s*',0,$args_s) ];
660 0 return ($name,$args);
661 } else {
662 0 return $check_ref;
663 }
664 }
665  
666 # Get the configuration as a hash
667 1 ▶sub _get_config {
Subroutine _get_config: called CC=3 SCAR=11.8
668 1 my $self = shift;
669 1 my $path = shift;
670 1 my $np = $self->{np};
671 1 ▶ $self->nagios_die("No configuration file " . $path . " found")
Branch: 1 branch
BranchTrueFalseTotal
if $path and not -e $path 0 1 1
Condition33%1/3
$path and not -e $path
!ll&&!rl&&r
100
MC/DC0%0/2
$path and not -e $path
$path not -e $path
Truth table33%1/3
A and not(B)
A= $pathB= not -e $path
ABresult
0X0
100
111
672 if ($path && ! -e $path);
673 1 return new JMX::Jmx4Perl::Config($path);
674 }
675  
676 # The global server config part
677 0 ▶sub _server_config {
Subroutine _server_config: not called CC=1 SCAR=6.9
678 0 return shift->{server_config};
679 }
680  
681 # Create the nagios plugin used for preparing the nagios output
682 1 ▶sub create_nagios_plugin {
Subroutine create_nagios_plugin: called CC=1 SCAR=0.0
Pod undocumented
683 1 my $self = shift;
684 1 my $np = Monitoring::Plugin->
685 new(
686 usage =>
687 "Usage: %s -u <agent-url> -m <mbean> -a <attribute> -c <threshold critical> -w <threshold warning>\n" .
688 " [--alias <alias>] [--value <shortcut>] [--base <alias/number/mbean>] [--delta <time-base>]\n" .
689 " [--name <perf-data label>] [--label <output-label>] [--product <product>]\n".
690 " [--user <user>] [--password <password>] [--proxy <proxy>]\n" .
691 " [--target <target-url>] [--target-user <user>] [--target-password <password>]\n" .
692 " [--legacy-escape]\n" .
693 " [--config <config-file>] [--check <check-name>] [--server <server-alias>] [-v] [--help]\n" .
694 " arg1 arg2 ....",
695 version => $JMX::Jmx4Perl::VERSION,
696 url => "http://www.jolokia.org",
697 plugin => "check_jmx4perl",
698 blurb => "This plugin checks for JMX attribute values on a remote Java application server",
699 extra => "\n\nYou need to deploy jolokia.war on the target application server or an intermediate proxy.\n" .
700 "Please refer to the documentation for JMX::Jmx4Perl for further details.\n\n" .
701 "For a complete documentation please consult the man page of check_jmx4perl or use the option --doc"
702 );
703 1 $np->shortname(undef);
704 1 $self->add_common_np_args($np);
705 1 $self->add_nagios_np_args($np);
706 1 $np->{msg_handler} = new JMX::Jmx4Perl::Nagios::MessageHandler();
707 1 $np->getopts();
708 1 return $np;
709 }
710  
711 1 ▶sub add_common_np_args {
Subroutine add_common_np_args: called CC=1 SCAR=0.0
Pod undocumented
712 1 my $self = shift;
713 1 my $np = shift;
714  
715 1 $np->add_arg(
716 spec => "url|u=s",
717 help => "URL to agent web application (e.g. http://server:8080/jolokia/)",
718 );
719 1 $np->add_arg(
720 spec => "product=s",
721 help => "Name of app server product. (e.g. \"jboss\")",
722 );
723 1 $np->add_arg(
724 spec => "alias=s",
725 help => "Alias name for attribte (e.g. \"MEMORY_HEAP_USED\")",
726 );
727 1 $np->add_arg(
728 spec => "mbean|m=s",
729 help => "MBean name (e.g. \"java.lang:type=Memory\")",
730 );
731 1 $np->add_arg(
732 spec => "attribute|a=s",
733 help => "Attribute name (e.g. \"HeapMemoryUsage\")",
734 );
735 1 $np->add_arg(
736 spec => "operation|o=s",
737 help => "Operation to execute",
738 );
739 1 $np->add_arg(
740 spec => "value=s",
741 help => "Shortcut for specifying mbean/attribute/path. Slashes within names must be escaped with \\",
742 );
743 1 $np->add_arg(
744 spec => "delta|d:s",
745 help => "Switches on incremental mode. Optional argument are seconds used for normalizing.",
746 );
747 1 $np->add_arg(
748 spec => "path|p=s",
749 help => "Inner path for extracting a single value from a complex attribute or return value (e.g. \"used\")",
750 );
751 1 $np->add_arg(
752 spec => "target=s",
753 help => "JSR-160 Service URL specifing the target server"
754 );
755 1 $np->add_arg(
756 spec => "target-user=s",
757 help => "Username to use for JSR-160 connection (if --target is set)"
758 );
759 1 $np->add_arg(
760 spec => "target-password=s",
761 help => "Password to use for JSR-160 connection (if --target is set)"
762 );
763 1 $np->add_arg(
764 spec => "proxy=s",
765 help => "Proxy to use"
766 );
767 1 $np->add_arg(
768 spec => "user=s",
769 help => "User for HTTP authentication"
770 );
771 1 $np->add_arg(
772 spec => "password=s",
773 help => "Password for HTTP authentication"
774 );
775 1 $np->add_arg(
776 spec => "name|n=s",
777 help => "Name to use for output. Optional, by default a standard value based on the MBean ".
778 "and attribute will be used"
779 );
780 1 $np->add_arg(
781 spec => "legacy-escape!",
782 help => "Use legacy escape mechanism for Jolokia agents < 1.0"
783 );
784 1 $np->add_arg(
785 spec => "config=s",
786 help => "Path to configuration file. Default: ~/.j4p"
787 );
788 1 $np->add_arg(
789 spec => "server=s",
790 help => "Symbolic name of server url to use, which needs to be configured in the configuration file"
791 );
792 1 $np->add_arg(
793 spec => "check=s",
794 help => "Name of a check configuration as defined in the configuration file"
795 );
796 1 $np->add_arg(
797 spec => "method=s",
798 help => "HTTP method to use. Either \"get\" or \"post\""
799 );
800 1 $np->add_arg(
801 spec => "doc:s",
802 help => "Print the documentation of check_jmx4perl, optionally specifying the section (tutorial, args, config)"
803 );
804 }
805  
806 1 ▶sub add_nagios_np_args {
Subroutine add_nagios_np_args: called CC=1 SCAR=0.0
Pod undocumented
807 1 my $self = shift;
808 1 my $np = shift;
809  
810 1 $np->add_arg(
811 spec => "base|base-alias|b=s",
812 help => "Base name, which when given, interprets critical and warning values as relative in the range 0 .. 100%. Must be given in the form mbean/attribute/path",
813 );
814 1 $np->add_arg(
815 spec => "base-mbean=s",
816 help => "Base MBean name, interprets critical and warning values as relative in the range 0 .. 100%. Requires a base-attribute, too",
817 );
818 1 $np->add_arg(
819 spec => "base-attribute=s",
820 help => "Base attribute for a relative check. Used together with base-mbean",
821 );
822 1 $np->add_arg(
823 spec => "base-path=s",
824 help => "Base path for relatie checks, where this path is used on the base attribute's value",
825 );
826 1 $np->add_arg(
827 spec => "unit=s",
828 help => "Unit of measurement of the data retreived. Recognized values are [B|KB|MN|GB|TB] for memory values and [us|ms|s|m|h|d] for time values"
829 );
830 1 $np->add_arg(
831 spec => "null=s",
832 help => "Value which should be used in case of a null return value of an operation or attribute. Is \"null\" by default"
833 );
834 1 $np->add_arg(
835 spec => "string",
836 help => "Force string comparison for critical and warning checks"
837 );
838 1 $np->add_arg(
839 spec => "numeric",
840 help => "Force numeric comparison for critical and warning checks"
841 );
842 1 $np->add_arg(
843 spec => "critical|c=s",
844 help => "Critical Threshold for value. " .
845 "See http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT " .
846 "for the threshold format.",
847 );
848 1 $np->add_arg(
849 spec => "warning|w=s",
850 help => "Warning Threshold for value.",
851 );
852 1 $np->add_arg(
853 spec => "label|l=s",
854 help => "Label to be used for printing out the result of the check. Placeholders can be used."
855 );
856 1 $np->add_arg(
857 spec => "perfdata=s",
858 help => "Whether performance data should be omitted, which are included by default."
859 );
860 1 $np->add_arg(
861 spec => "unknown-is-critical",
862 help => "Map UNKNOWN errors to errors with a CRITICAL status"
863 );
864 }
865  
866 # Access to configuration informations
867 # Known config options (key: cmd line arguments, values: keys in config);
868 1 my $SERVER_CONFIG_KEYS = {
869 "url" => "url",
870 "user" => "user",
871 "password" => "password",
872 "product" => "product",
873 "legacy_escape" => "legacyconfig"
874 };
875  
876 # Get target configuration or undef if no jmx-proxy mode
877 # is used
878 0 ▶sub target_config {
Subroutine target_config: not called CC=1 SCAR=6.9
Pod undocumented
879 0 return shift->_target_or_proxy_config("target","target-user","target-password");
880 }
881  
882 # Get proxy configuration or undef if no proxy configuration
883 # is used
884 0 ▶sub proxy_config {
Subroutine proxy_config: not called CC=1 SCAR=6.9
Pod undocumented
885 0 return shift->_target_or_proxy_config("proxy","proxy-user","proxy-password");
886 }
887  
888 0 ▶sub _target_or_proxy_config {
Subroutine _target_or_proxy_config: not called CC=4 SCAR=30.0
889 0 my $self = shift;
890
891 0 my $main_key = shift;
892 0 my $user_opt = shift;
893 0 my $password_opt = shift;
894  
895 0 my $np = $self->{np};
896 0 my $opts = $np->opts;
897 0 my $server_config = $self->_server_config;
898 0 ▶ if ($opts->{$main_key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{$main_key}) { } 0 0 0
elsif ($server_config and $server_config->{$main_key}) { } 0 0 0
899 # Use configuration from the command line:
900 return {
901 url => $opts->{$main_key},
902 user => $opts->{$user_opt},
903 0 password => $opts->{$password_opt}
904 }
905 ▶ } elsif ($server_config && $server_config->{$main_key}) {
Condition0%0/3
$server_config and $server_config->{$main_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$server_config and $server_config->{$main_key}
$server_config $server_config->{$main_key}
Truth table0%0/3
A and B
A= $server_configB= $server_config->{$main_key}
ABresult
0X0
100
111
906 # Use configuration directly from the server definition:
907 0 return $server_config->{$main_key}
908 } else {
909 0 return undef;
910 }
911 }
912  
913 # Autoloading is used to fetch the proper connection parameters
914 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=33.9
915 1 my $self = shift;
916 1 my $np = $self->{np};
917 1 my $name = $AUTOLOAD;
918 1 $name =~ s/.*://; # strip fully-qualified portion
919 1 my $opts_name = $name;
920 1 $opts_name =~ s/_/-/;
921  
922 1 ▶ if ($SERVER_CONFIG_KEYS->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SERVER_CONFIG_KEYS->{$name}) { } 1 0 1
923 1 ▶ return $np->opts->{$opts_name} if $np->opts->{$opts_name};
Branch: 1 branch
BranchTrueFalseTotal
if $np->opts->{$opts_name} 1 0 1
924 0 my $c = $SERVER_CONFIG_KEYS->{$name};
925 0 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 0 0
926 0 my @parts = split "/",$c;
927 0 ▶ my $h = $self->_server_config ||
Condition0%0/2
$self->_server_config || (return undef)
l!l
00
MC/DC0%0/1
$self->_server_config || (return undef)
$self->_server_config
Truth table0%0/2
A
A= $self->_server_config
Aresult
11
00
928 return undef;
929 0 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 0 0 0
930 0 my $p = shift @parts;
931 0 ▶ return undef unless $h->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless $h->{$p} 0 0 0
932 0 $h = $h->{$p};
933 0 ▶ return $h unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 0 0
934 }
935 } else {
936 0 return undef;
937 }
938 } else {
939 0 $self->nagios_die("No config attribute \"" . $name . "\" known");
940 }
941 }
942  
943 0 ▶sub nagios_die {
Subroutine nagios_die: not called CC=2 SCAR=17.9
Pod undocumented
944 0 my $self = shift;
945 0 my @args = @_;
946  
947 0 my $np = $self->{np};
948 0 ▶ $np->nagios_die(join("",@args),$np->opts->{'unknown-is-critical'} ? CRITICAL : UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
$np->opts->{'unknown-is-critical'} ? : 0 0 0
949 }
950  
951 # Declared here to avoid AUTOLOAD confusions
952 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
953  
954 }
955  
956 =head1 LICENSE
957  
958 This file is part of jmx4perl.
959  
960 Jmx4perl is free software: you can redistribute it and/or modify
961 it under the terms of the GNU General Public License as published by
962 the Free Software Foundation, either version 2 of the License, or
963 (at your option) any later version.
964  
965 jmx4perl is distributed in the hope that it will be useful,
966 but WITHOUT ANY WARRANTY; without even the implied warranty of
967 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
968 GNU General Public License for more details.
969  
970 You should have received a copy of the GNU General Public License
971 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
972  
973 =head1 AUTHOR
974  
975 roland@cpan.org
976  
977 =cut
978  
979 1 1;
« Jmx4Perl.pm ↑ Summary Jmx4Perl/J4psh/CompletionHandler.pm »