« blib/lib/JMX/Jmx4Perl/Product/BaseHandler.pm
↑ Summary
blib/lib/JMX/Jmx4Perl/Agent/Jolokia/Verifier/OpenPGPVerifier.pm »
| 1 | package JMX::Jmx4Perl::Nagios::CheckJmx4Perl; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use JMX::Jmx4Perl::Nagios::SingleCheck; | ||
| 6 | use JMX::Jmx4Perl::Nagios::MessageHandler; | ||
| 7 | use JMX::Jmx4Perl; | ||
| 8 | use JMX::Jmx4Perl::Request; | ||
| 9 | use JMX::Jmx4Perl::Response; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Nagios::Plugin; | ||
| 12 | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT); | ||
| 13 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 14 | use Carp; | ||
| 15 | use Text::ParseWords; | ||
| 16 | |||
| 17 | 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 | sub new { | ||
| 53 | my $class = shift; | ||
| 54 | my $self = { }; | ||
| 55 | bless $self,(ref($class) || $class); | ||
| 56 | $self->{np} = $self->create_nagios_plugin(); | ||
| 57 | $self->{cmd_args} = [ @ARGV ]; | ||
| 58 | |||
| 59 | $self->_print_doc_and_exit($self->{np}->opts->{doc}) if defined $self->{np}->opts->{doc}; | ||
| 60 | $self->_verify_and_initialize(); | ||
| 61 | 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 | sub execute { | ||
| 73 | my $self = shift; | ||
| 74 | my $np = $self->{np}; | ||
| 75 | eval { | ||
| 76 | # Request | ||
| 77 | my @optional = (); | ||
| 78 | |||
| 79 | my $error_stat = { }; | ||
| 80 | 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 | timeout => $np->opts->{timeout} || 180, | ||
| 86 | target => $target_config, | ||
| 87 | # For Jolokia agents < 1.0 | ||
| 88 | 'legacy-escape' => $self->legacy_escape); | ||
| 89 | my @requests; | ||
| 90 | for my $check (@{$self->{checks}}) { | ||
| 91 | push @requests,@{$check->get_requests($jmx,\@ARGV)}; | ||
| 92 | } | ||
| 93 | my $responses = $self->_send_requests($jmx,@requests); | ||
| 94 | #print Dumper($responses); | ||
| 95 | my @extra_requests = (); | ||
| 96 | my $nr_checks = scalar(@{$self->{checks}}); | ||
| 97 | if ($nr_checks == 1) { | ||
| 98 | eval { | ||
| 99 | my @r = $self->{checks}->[0]->extract_responses($responses,\@requests,{ target => $target_config }); | ||
| 100 | push @extra_requests,@r if @r; | ||
| 101 | }; | ||
| 102 | $self->nagios_die($@) if $@; | ||
| 103 | } else { | ||
| 104 | my $i = 1; | ||
| 105 | for my $check (@{$self->{checks}}) { | ||
| 106 | # A check can consume more than one response | ||
| 107 | my $prefix = $self->_multi_check_prefix($check,$i++,$nr_checks); | ||
| 108 | eval { | ||
| 109 | my @r = $check->extract_responses($responses,\@requests, | ||
| 110 | { | ||
| 111 | target => $target_config, | ||
| 112 | prefix => $prefix, | ||
| 113 | error_stat => $error_stat | ||
| 114 | }); | ||
| 115 | push @extra_requests,@r if @r; | ||
| 116 | }; | ||
| 117 | if ($@) { | ||
| 118 | my $txt = $@; | ||
| 119 | $txt =~ s/^(.*?)\n.*$/$1/s; | ||
| 120 | my $code = $np->opts->{'unknown-is-critical'} ? CRITICAL : UNKNOWN; | ||
| 121 | $check->update_error_stats($error_stat,$code); | ||
| 122 | $prefix =~ s/\%c/$STATUS_TEXT{$code}/g; | ||
| 123 | my $msg_handler = $np->{msg_handler} || $np; | ||
| 124 | $msg_handler->add_message($code,$prefix . $txt); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | # Send extra requests, e.g. for switching on the history | ||
| 129 | if (@extra_requests) { | ||
| 130 | $self->_send_requests($jmx,@extra_requests); | ||
| 131 | } | ||
| 132 | |||
| 133 | # Different outputs for multi checks/single checks | ||
| 134 | $self->do_exit($error_stat); | ||
| 135 | }; | ||
| 136 | if ($@) { | ||
| 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 | if ($@ !~ /^ExitTrap:/) { | ||
| 141 | $self->nagios_die("Error: $@"); | ||
| 142 | } else { | ||
| 143 | 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 | sub do_exit { | ||
| 156 | my $self = shift; | ||
| 157 | my $error_stat = shift; | ||
| 158 | my $np = $self->{np}; | ||
| 159 | |||
| 160 | my $msg_handler = $np->{msg_handler} || $np; | ||
| 161 | my ($code,$message) = $msg_handler->check_messages(join => "\n", join_all => "\n"); | ||
| 162 | ($code,$message) = $self->_prepare_multicheck_message($np,$code,$message,$error_stat) if scalar(@{$self->{checks}}) > 1; | ||
| 163 | |||
| 164 | $np->nagios_exit($code, $message); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _prepare_multicheck_message { | ||
| 168 | my $self = shift; | ||
| 169 | my $np = shift; | ||
| 170 | my $code = shift; | ||
| 171 | my $message = shift; | ||
| 172 | my $error_stat = shift; | ||
| 173 | |||
| 174 | my $summary; | ||
| 175 | my $labels = $self->{multi_check_labels} || {}; | ||
| 176 | my $nr_checks = scalar(@{$self->{checks}}); | ||
| 177 | $code = $self->_check_for_UNKNOWN($error_stat,$code); | ||
| 178 | if ($code eq OK) { | ||
| 179 | $summary = $self->_format_multicheck_ok_summary($labels->{summary_ok} || | ||
| 180 | "All %n checks OK",$nr_checks); | ||
| 181 | } else { | ||
| 182 | $summary = $self->_format_multicheck_failure_summary($labels->{summary_failure} || | ||
| 183 | "%e of %n checks failed [%d]", | ||
| 184 | $nr_checks, | ||
| 185 | $error_stat); | ||
| 186 | } | ||
| 187 | return ($code,$summary . "\n" . $message); | ||
| 188 | } | ||
| 189 | |||
| 190 | # UNKNOWN shadows everything else | ||
| 191 | sub _check_for_UNKNOWN { | ||
| 192 | my $self = shift; | ||
| 193 | my $error_stat = shift; | ||
| 194 | my $code = shift; | ||
| 195 | return $error_stat->{UNKNOWN} && scalar(@$error_stat->{UNKNOWN}) ? UNKNOWN : $code; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _format_multicheck_ok_summary { | ||
| 199 | my $self = shift; | ||
| 200 | my $format = shift; | ||
| 201 | my $nr_checks = shift; | ||
| 202 | my $ret = $format; | ||
| 203 | $ret =~ s/\%n/$nr_checks/g; | ||
| 204 | return $ret; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _format_multicheck_failure_summary { | ||
| 208 | my $self = shift; | ||
| 209 | my $format = shift; | ||
| 210 | my $nr_checks = shift; | ||
| 211 | my $error_stat = shift; | ||
| 212 | |||
| 213 | my $ret = $format; | ||
| 214 | |||
| 215 | my $details = ""; | ||
| 216 | my $total_errors = 0; | ||
| 217 | for my $code (UNKNOWN,CRITICAL,WARNING) { | ||
| 218 | if (my $errs = $error_stat->{$code}) { | ||
| 219 | $details .= scalar(@$errs) . " " . $STATUS_TEXT{$code} . " (" . join (",",@$errs) . "), "; | ||
| 220 | $total_errors += scalar(@$errs); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | if ($total_errors > 0) { | ||
| 224 | # Cut off extra chars at the end | ||
| 225 | $details = substr($details,0,-2); | ||
| 226 | } | ||
| 227 | |||
| 228 | $ret =~ s/\%d/$details/g; | ||
| 229 | $ret =~ s/\%e/$total_errors/g; | ||
| 230 | $ret =~ s/\%n/$nr_checks/g; | ||
| 231 | return $ret; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Create a formatted prefix for multicheck output | ||
| 235 | sub _multi_check_prefix { | ||
| 236 | my $self = shift; | ||
| 237 | my $check = shift; | ||
| 238 | my $idx = shift; | ||
| 239 | my $max = shift; | ||
| 240 | |||
| 241 | my $c = $check->{config}; | ||
| 242 | |||
| 243 | my $l = length($max); | ||
| 244 | |||
| 245 | return sprintf("[%$l.${l}s] %%c ",$idx) | ||
| 246 | if (defined($c->{multicheckprefix}) && !length($c->{multicheckprefix})); | ||
| 247 | |||
| 248 | my $label = $c->{multicheckprefix} || $c->{name} || $c->{key} || ""; | ||
| 249 | return sprintf("[%$l.${l}s] %%c %s: ",$idx,$label); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | # Send the requests via the build up agent | ||
| 254 | sub _send_requests { | ||
| 255 | my ($self,$jmx,@requests) = @_; | ||
| 256 | my $o = $self->{opts}; | ||
| 257 | |||
| 258 | my $start_time; | ||
| 259 | if ($o->verbose) { | ||
| 260 | # TODO: Print summary of request (GET vs POST) | ||
| 261 | if ($self->user) { | ||
| 262 | print "Remote User: ",$o->user,"\n"; | ||
| 263 | } | ||
| 264 | $start_time = [gettimeofday]; | ||
| 265 | } | ||
| 266 | # Detangle request for direct method calls and JMX requests to call: | ||
| 267 | my $req_map = $self->_detangle_requests(\@requests); | ||
| 268 | my @responses = (); | ||
| 269 | |||
| 270 | $self->_execute_scripts(\@responses,$req_map); | ||
| 271 | $self->_execute_requests(\@responses,$req_map,$jmx); | ||
| 272 | |||
| 273 | if ($o->verbose) { | ||
| 274 | print "Result fetched in ",tv_interval($start_time) * 1000," ms:\n"; | ||
| 275 | print Dumper(\@responses); | ||
| 276 | } | ||
| 277 | 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 | sub _detangle_requests { | ||
| 284 | my $self = shift; | ||
| 285 | my $requests = shift; | ||
| 286 | my $req_map = {}; | ||
| 287 | my $idx = 0; | ||
| 288 | for my $r (@$requests) { | ||
| 289 | push @{$req_map->{ref($r) eq "CODE" ? "code" : "request"}},[$r,$idx]; | ||
| 290 | $idx++; | ||
| 291 | } | ||
| 292 | 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 | sub _execute_scripts { | ||
| 298 | my $self = shift; | ||
| 299 | my $responses = shift; | ||
| 300 | my $req_map = shift; | ||
| 301 | for my $e (@{$req_map->{"code"}}) { | ||
| 302 | # Will die on error which will bubble up | ||
| 303 | $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 | sub _execute_requests { | ||
| 311 | my $self = shift; | ||
| 312 | my $responses = shift; | ||
| 313 | my $req_map = shift; | ||
| 314 | my $jmx = shift; | ||
| 315 | |||
| 316 | # Call remote JMX and weave in | ||
| 317 | my $reqs2send = $req_map->{"request"}; | ||
| 318 | if ($reqs2send) { | ||
| 319 | my @resp_received = $jmx->request(map { $_->[0] } @$reqs2send); | ||
| 320 | for my $r (@$reqs2send) { | ||
| 321 | $responses->[$r->[1]] = shift @resp_received; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | # Print online manual and exit (somewhat crude, I know) | ||
| 328 | sub _print_doc_and_exit { | ||
| 329 | my $self = shift; | ||
| 330 | my $section = shift; | ||
| 331 | if (!eval "require Pod::Usage; Pod::Usage->import(qw(pod2usage)); 1;") { | ||
| 332 | print "Please install Pod::Usage for creating the online help\n"; | ||
| 333 | exit 1; | ||
| 334 | } | ||
| 335 | if ($section) { | ||
| 336 | my %sects = ( | ||
| 337 | tutorial => "TUTORIAL", | ||
| 338 | reference => "REFERENCE", | ||
| 339 | options => "COMMAND LINE", | ||
| 340 | config => "CONFIGURATION", | ||
| 341 | ); | ||
| 342 | my $real_section = $sects{lc $section}; | ||
| 343 | if ($real_section) { | ||
| 344 | pod2usage(-verbose => 99, -sections => $real_section ); | ||
| 345 | } else { | ||
| 346 | print "Unknown documentation section '$section' (known: ",join (",",sort keys %sects),")\n"; | ||
| 347 | exit 1; | ||
| 348 | } | ||
| 349 | } else { | ||
| 350 | 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 | sub _verify_and_initialize { | ||
| 360 | my $self = shift; | ||
| 361 | my $np = $self->{np}; | ||
| 362 | my $o = $np->opts; | ||
| 363 | |||
| 364 | $self->{opts} = $self->{np}->opts; | ||
| 365 | |||
| 366 | # Fetch configuration | ||
| 367 | my $config = $self->_get_config($o->config); | ||
| 368 | # Now, if a specific check is given, extract it, too. | ||
| 369 | my $check_configs; | ||
| 370 | #print Dumper($config); | ||
| 371 | $check_configs = $self->_extract_checks($config,$o->check); | ||
| 372 | #print Dumper($check_configs); | ||
| 373 | if ($check_configs) { | ||
| 374 | for my $c (@$check_configs) { | ||
| 375 | my $s_c = new JMX::Jmx4Perl::Nagios::SingleCheck($np,$c); | ||
| 376 | push @{$self->{checks}},$s_c; | ||
| 377 | } | ||
| 378 | } else { | ||
| 379 | $self->{checks} = [ new JMX::Jmx4Perl::Nagios::SingleCheck($np) ]; | ||
| 380 | } | ||
| 381 | # If a server name is given, we use that for the connection parameters | ||
| 382 | if ($o->server) { | ||
| 383 | $self->{server_config} = $config->get_server_config($o->server) | ||
| 384 | || $self->nagios_die("No server configuration for " . $o->server . " found"); | ||
| 385 | } | ||
| 386 | |||
| 387 | # Sanity checks | ||
| 388 | $self->nagios_die("No Server URL given") unless $self->url; | ||
| 389 | |||
| 390 | for my $check (@{$self->{checks}}) { | ||
| 391 | my $name = $check->name ? " [Check: " . $check->name . "]" : ""; | ||
| 392 | $self->nagios_die("An MBean name and a attribute/operation must be provided " . $name) | ||
| 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 | sub _extract_checks { | ||
| 400 | my $self = shift; | ||
| 401 | my $config = shift; | ||
| 402 | my $check = shift; | ||
| 403 | |||
| 404 | my $np = $self->{np}; | ||
| 405 | if ($check) { | ||
| 406 | $self->nagios_die("No configuration given") unless $config; | ||
| 407 | $self->nagios_die("No checks defined in configuration") unless $config->{check}; | ||
| 408 | |||
| 409 | my $check_configs; | ||
| 410 | unless ($config->{check}->{$check}) { | ||
| 411 | $check_configs = $self->_resolve_multicheck($config,$check,$self->{cmd_args}); | ||
| 412 | $self->_retrieve_mc_summary_label($config,$check); | ||
| 413 | } else { | ||
| 414 | my $check_config = $config->{check}->{$check}; | ||
| 415 | $check_configs = ref($check_config) eq "ARRAY" ? $check_config : [ $check_config ]; | ||
| 416 | $check_configs->[0]->{key} = $check; | ||
| 417 | } | ||
| 418 | $self->nagios_die("No check configuration with name " . $check . " found") unless (@{$check_configs}); | ||
| 419 | |||
| 420 | #print Dumper($check_configs); | ||
| 421 | |||
| 422 | # Resolve parent values | ||
| 423 | for my $c (@{$check_configs}) { | ||
| 424 | #print "[A] ",Dumper($c); | ||
| 425 | $self->_resolve_check_config($c,$config,$self->{cmd_args}); | ||
| 426 | |||
| 427 | #print "[B] ",Dumper($c); | ||
| 428 | # Finally, resolve any left over place holders | ||
| 429 | for my $k (keys(%$c)) { | ||
| 430 | $c->{$k} = $self->_replace_placeholder($c->{$k},undef) unless ref($c->{$k}); | ||
| 431 | } | ||
| 432 | #print "[C] ",Dumper($c); | ||
| 433 | } | ||
| 434 | return $check_configs; | ||
| 435 | } else { | ||
| 436 | return undef; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | # Resolve a multicheck configuration (<MultiCheck>) | ||
| 441 | sub _resolve_multicheck { | ||
| 442 | my $self = shift; | ||
| 443 | my $config = shift; | ||
| 444 | my $check = shift; | ||
| 445 | my $args = shift; | ||
| 446 | my $np = $self->{np}; | ||
| 447 | my $multi_checks = $config->{multicheck}; | ||
| 448 | my $check_config = []; | ||
| 449 | if ($multi_checks) { | ||
| 450 | my $m_check = $multi_checks->{$check}; | ||
| 451 | if ($m_check) { | ||
| 452 | # Resolve all checks | ||
| 453 | my $c_names = []; | ||
| 454 | for my $type( qw(check multicheck)) { | ||
| 455 | if ($m_check->{$type}) { | ||
| 456 | push @$c_names, ref($m_check->{$type}) eq "ARRAY" ? @{$m_check->{$type}} : $m_check->{$type}; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | for my $name (@$c_names) { | ||
| 460 | my ($c_name,$c_args) = $self->_parse_check_ref($name); | ||
| 461 | my $args_merged = $self->_merge_multicheck_args($c_args,$args); | ||
| 462 | $self->nagios_die("Unknown check '" . $c_name . "' for multi check " . $check) | ||
| 463 | unless defined($config->{check}->{$c_name}) or defined($multi_checks->{$c_name}); | ||
| 464 | if ($config->{check}->{$c_name}) { | ||
| 465 | # We need a copy of the check hash to avoid mangling it up | ||
| 466 | # if it is referenced multiple times | ||
| 467 | my $check = { %{$config->{check}->{$c_name}} }; | ||
| 468 | $check->{key} = $c_name; | ||
| 469 | $check->{args} = $args_merged; | ||
| 470 | push @{$check_config},$check; | ||
| 471 | } else { | ||
| 472 | # It's a multi check referenced via <Check> or <MultiCheck> .... | ||
| 473 | push @{$check_config},@{$self->_resolve_multicheck($config,$c_name,$args_merged)}; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | return $check_config; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub _retrieve_mc_summary_label { | ||
| 482 | my $self = shift; | ||
| 483 | my $config = shift; | ||
| 484 | my $check = shift; | ||
| 485 | |||
| 486 | my $multi_checks = $config->{multicheck}; | ||
| 487 | if ($multi_checks) { | ||
| 488 | my $m_check = $multi_checks->{$check}; | ||
| 489 | if ($m_check && ($m_check->{summaryok} || $m_check->{summaryfailure})) { | ||
| 490 | my $mc_labels = | ||
| 491 | $self->{multi_check_labels} = { | ||
| 492 | summary_ok => $m_check->{summaryok}, | ||
| 493 | summary_failure => $m_check->{summaryfailure} | ||
| 494 | }; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | sub _merge_multicheck_args { | ||
| 500 | my $self = shift; | ||
| 501 | my $check_params = shift; | ||
| 502 | my $args = shift; | ||
| 503 | if (!$args || !$check_params) { | ||
| 504 | return $check_params; | ||
| 505 | } | ||
| 506 | my $ret = [ @$check_params ]; # Copy it over | ||
| 507 | for my $i (0 .. $#$check_params) { | ||
| 508 | if ($check_params->[$i] =~ /^\$(\d+)$/) { | ||
| 509 | my $j = $1; | ||
| 510 | if ($j <= $#$args) { | ||
| 511 | $ret->[$i] = $args->[$j]; | ||
| 512 | next; | ||
| 513 | } | ||
| 514 | # Nothing to replace | ||
| 515 | $ret->[$i] = $check_params->[$i]; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | return $ret; | ||
| 519 | } | ||
| 520 | |||
| 521 | # Resolve a singe <Check> configuration | ||
| 522 | sub _resolve_check_config { | ||
| 523 | my $self = shift; | ||
| 524 | my $check = shift; | ||
| 525 | my $config = shift; | ||
| 526 | # Args can come from the outside, but also as part of a multicheck (stored | ||
| 527 | # in $check->{args}) | ||
| 528 | my $args = $check->{args} && @{$check->{args}} ? $check->{args} : shift; | ||
| 529 | my $np = $self->{np}; | ||
| 530 | if ($check->{use}) { | ||
| 531 | # Resolve parents | ||
| 532 | my $parents = ref($check->{use}) eq "ARRAY" ? $check->{use} : [ $check->{use} ]; | ||
| 533 | my $parent_merged = {}; | ||
| 534 | for my $p (@$parents) { | ||
| 535 | 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 | unless $config->{check}->{$p_name}; | ||
| 539 | # Clone it to avoid side effects when replacing checks inline | ||
| 540 | my $p_check = { %{$config->{check}->{$p_name}} }; | ||
| 541 | $p_check->{key} = $p_name; | ||
| 542 | #print "::::: ",Dumper($p_check,$p_args); | ||
| 543 | |||
| 544 | $self->_resolve_check_config($p_check,$config,$p_args); | ||
| 545 | |||
| 546 | #$self->_replace_args($p_check,$config,$p_args); | ||
| 547 | $parent_merged->{$_} = $p_check->{$_} for keys %$p_check; | ||
| 548 | } | ||
| 549 | # Replace inherited values | ||
| 550 | for my $k (keys %$parent_merged) { | ||
| 551 | my $parent_val = defined($parent_merged->{$k}) ? $parent_merged->{$k} : ""; | ||
| 552 | if (defined($check->{$k})) { | ||
| 553 | $check->{$k} =~ s/\$BASE/$parent_val/g; | ||
| 554 | } else { | ||
| 555 | $check->{$k} = $parent_val; | ||
| 556 | } | ||
| 557 | } | ||
| 558 | } | ||
| 559 | $self->_replace_args($check,$config,$args); | ||
| 560 | return $check; | ||
| 561 | } | ||
| 562 | |||
| 563 | # Replace argument placeholders with a given list of arguments | ||
| 564 | sub _replace_args { | ||
| 565 | my $self = shift; | ||
| 566 | my $check = shift; | ||
| 567 | my $config = shift; | ||
| 568 | my $args = shift; | ||
| 569 | for my $k (keys(%$check)) { | ||
| 570 | next if $k =~ /^(key|args)$/; # Internal keys | ||
| 571 | $check->{$k} = | ||
| 572 | $self->_replace_placeholder($check->{$k},$args) | ||
| 573 | if ($args && @$args && !ref($check->{$k})); | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | sub _replace_placeholder { | ||
| 578 | my $self = shift; | ||
| 579 | my $val = shift; | ||
| 580 | my $args = shift; | ||
| 581 | my $index = defined($args) ? join "|",0 ... $#$args : "\\d+"; | ||
| 582 | |||
| 583 | 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 | $regexp_s =~ s/\$index/$index/g; | ||
| 595 | my $regexp = qr/$regexp_s/sx; | ||
| 596 | die "Cannot create placeholder regexp" if $@; | ||
| 597 | my $rest = $val; | ||
| 598 | my $ret = ""; | ||
| 599 | while (defined($rest) && length($rest) && $rest =~ $regexp) { | ||
| 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 | my $start = defined($1) ? $1 : ""; | ||
| 607 | my $orig_val = '$' . $2; | ||
| 608 | my $i = defined($3) ? $3 : $4; | ||
| 609 | my $default = $5; | ||
| 610 | my $end = defined($6) ? $6 : ""; | ||
| 611 | $default =~ s/^\s*(.*)+?\s*$/$1/ if $default; # Trim whitespace | ||
| 612 | #print Dumper({start => $start, orig => $orig_val,end => $end, default=> $default, rest => $rest, i => $i}); | ||
| 613 | if (defined($args)) { | ||
| 614 | my $repl = $args->[$i]; | ||
| 615 | if (defined($repl)) { | ||
| 616 | if ($repl =~ /^\$(\d+)$/) { | ||
| 617 | my $new_index = $1; | ||
| 618 | #print "============== $val $new_index\n"; | ||
| 619 | # Val is a placeholder itself | ||
| 620 | if (defined($default)) { | ||
| 621 | $ret .= $start . '${' . $new_index . ':' . $default . '}'; | ||
| 622 | } else { | ||
| 623 | $ret .= $start . '$' . $new_index; | ||
| 624 | } | ||
| 625 | } else { | ||
| 626 | $ret .= $start . $repl; | ||
| 627 | } | ||
| 628 | } else { | ||
| 629 | $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 | if (defined($default)) { | ||
| 635 | $ret .= $start . $default; | ||
| 636 | } elsif (length($start) || length($end)) { | ||
| 637 | $ret .= $start; | ||
| 638 | } else { | ||
| 639 | if (!length($ret)) { | ||
| 640 | # No default value, nothing else for this value. We | ||
| 641 | # consider it undefined | ||
| 642 | return undef; | ||
| 643 | } | ||
| 644 | } | ||
| 645 | } | ||
| 646 | $rest = $end; | ||
| 647 | #print "... $ret$rest\n"; | ||
| 648 | } | ||
| 649 | return $ret . (defined($rest) ? $rest : ""); | ||
| 650 | } | ||
| 651 | |||
| 652 | # Split up a 'Use' parent config reference, including possibly arguments | ||
| 653 | sub _parse_check_ref { | ||
| 654 | my $self = shift; | ||
| 655 | my $check_ref = shift; | ||
| 656 | if ($check_ref =~/^\s*(.+?)\((.*)\)\s*$/) { | ||
| 657 | my $name = $1; | ||
| 658 | my $args_s = $2; | ||
| 659 | my $args = [ parse_line('\s*,\s*',0,$args_s) ]; | ||
| 660 | return ($name,$args); | ||
| 661 | } else { | ||
| 662 | return $check_ref; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | |||
| 666 | # Get the configuration as a hash | ||
| 667 | sub _get_config { | ||
| 668 | my $self = shift; | ||
| 669 | my $path = shift; | ||
| 670 | my $np = $self->{np}; | ||
| 671 | $self->nagios_die("No configuration file " . $path . " found") | ||
| 672 | if ($path && ! -e $path); | ||
| 673 | return new JMX::Jmx4Perl::Config($path); | ||
| 674 | } | ||
| 675 | |||
| 676 | # The global server config part | ||
| 677 | sub _server_config { | ||
| 678 | return shift->{server_config}; | ||
| 679 | } | ||
| 680 | |||
| 681 | # Create the nagios plugin used for preparing the nagios output | ||
| 682 | sub create_nagios_plugin { | ||
| 683 | my $self = shift; | ||
| 684 | my $np = Nagios::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.jmx4perl.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 | $np->shortname(undef); | ||
| 704 | $self->add_common_np_args($np); | ||
| 705 | $self->add_nagios_np_args($np); | ||
| 706 | $np->{msg_handler} = new JMX::Jmx4Perl::Nagios::MessageHandler(); | ||
| 707 | $np->getopts(); | ||
| 708 | return $np; | ||
| 709 | } | ||
| 710 | |||
| 711 | sub add_common_np_args { | ||
| 712 | my $self = shift; | ||
| 713 | my $np = shift; | ||
| 714 | |||
| 715 | $np->add_arg( | ||
| 716 | spec => "url|u=s", | ||
| 717 | help => "URL to agent web application (e.g. http://server:8080/jolokia/)", | ||
| 718 | ); | ||
| 719 | $np->add_arg( | ||
| 720 | spec => "product=s", | ||
| 721 | help => "Name of app server product. (e.g. \"jboss\")", | ||
| 722 | ); | ||
| 723 | $np->add_arg( | ||
| 724 | spec => "alias=s", | ||
| 725 | help => "Alias name for attribte (e.g. \"MEMORY_HEAP_USED\")", | ||
| 726 | ); | ||
| 727 | $np->add_arg( | ||
| 728 | spec => "mbean|m=s", | ||
| 729 | help => "MBean name (e.g. \"java.lang:type=Memory\")", | ||
| 730 | ); | ||
| 731 | $np->add_arg( | ||
| 732 | spec => "attribute|a=s", | ||
| 733 | help => "Attribute name (e.g. \"HeapMemoryUsage\")", | ||
| 734 | ); | ||
| 735 | $np->add_arg( | ||
| 736 | spec => "operation|o=s", | ||
| 737 | help => "Operation to execute", | ||
| 738 | ); | ||
| 739 | $np->add_arg( | ||
| 740 | spec => "value=s", | ||
| 741 | help => "Shortcut for specifying mbean/attribute/path. Slashes within names must be escaped with \\", | ||
| 742 | ); | ||
| 743 | $np->add_arg( | ||
| 744 | spec => "delta|d:s", | ||
| 745 | help => "Switches on incremental mode. Optional argument are seconds used for normalizing.", | ||
| 746 | ); | ||
| 747 | $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 | $np->add_arg( | ||
| 752 | spec => "target=s", | ||
| 753 | help => "JSR-160 Service URL specifing the target server" | ||
| 754 | ); | ||
| 755 | $np->add_arg( | ||
| 756 | spec => "target-user=s", | ||
| 757 | help => "Username to use for JSR-160 connection (if --target is set)" | ||
| 758 | ); | ||
| 759 | $np->add_arg( | ||
| 760 | spec => "target-password=s", | ||
| 761 | help => "Password to use for JSR-160 connection (if --target is set)" | ||
| 762 | ); | ||
| 763 | $np->add_arg( | ||
| 764 | spec => "proxy=s", | ||
| 765 | help => "Proxy to use" | ||
| 766 | ); | ||
| 767 | $np->add_arg( | ||
| 768 | spec => "user=s", | ||
| 769 | help => "User for HTTP authentication" | ||
| 770 | ); | ||
| 771 | $np->add_arg( | ||
| 772 | spec => "password=s", | ||
| 773 | help => "Password for HTTP authentication" | ||
| 774 | ); | ||
| 775 | $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 | $np->add_arg( | ||
| 781 | spec => "legacy-escape!", | ||
| 782 | help => "Use legacy escape mechanism for Jolokia agents < 1.0" | ||
| 783 | ); | ||
| 784 | $np->add_arg( | ||
| 785 | spec => "config=s", | ||
| 786 | help => "Path to configuration file. Default: ~/.j4p" | ||
| 787 | ); | ||
| 788 | $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 | $np->add_arg( | ||
| 793 | spec => "check=s", | ||
| 794 | help => "Name of a check configuration as defined in the configuration file" | ||
| 795 | ); | ||
| 796 | $np->add_arg( | ||
| 797 | spec => "method=s", | ||
| 798 | help => "HTTP method to use. Either \"get\" or \"post\"" | ||
| 799 | ); | ||
| 800 | $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 | sub add_nagios_np_args { | ||
| 807 | my $self = shift; | ||
| 808 | my $np = shift; | ||
| 809 | |||
| 810 | $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 | $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 | $np->add_arg( | ||
| 819 | spec => "base-attribute=s", | ||
| 820 | help => "Base attribute for a relative check. Used together with base-mbean", | ||
| 821 | ); | ||
| 822 | $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 | $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 | $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 | $np->add_arg( | ||
| 835 | spec => "string", | ||
| 836 | help => "Force string comparison for critical and warning checks" | ||
| 837 | ); | ||
| 838 | $np->add_arg( | ||
| 839 | spec => "numeric", | ||
| 840 | help => "Force numeric comparison for critical and warning checks" | ||
| 841 | ); | ||
| 842 | $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 | $np->add_arg( | ||
| 849 | spec => "warning|w=s", | ||
| 850 | help => "Warning Threshold for value.", | ||
| 851 | ); | ||
| 852 | $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 | $np->add_arg( | ||
| 857 | spec => "perfdata=s", | ||
| 858 | help => "Whether performance data should be omitted, which are included by default." | ||
| 859 | ); | ||
| 860 | $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 | 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 | sub target_config { | ||
| 879 | 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 | sub proxy_config { | ||
| 885 | return shift->_target_or_proxy_config("proxy","proxy-user","proxy-password"); | ||
| 886 | } | ||
| 887 | |||
| 888 | sub _target_or_proxy_config { | ||
| 889 | my $self = shift; | ||
| 890 | |||
| 891 | my $main_key = shift; | ||
| 892 | my $user_opt = shift; | ||
| 893 | my $password_opt = shift; | ||
| 894 | |||
| 895 | my $np = $self->{np}; | ||
| 896 | my $opts = $np->opts; | ||
| 897 | my $server_config = $self->_server_config; | ||
| 898 | if ($opts->{$main_key}) { | ||
| 899 | # Use configuration from the command line: | ||
| 900 | return { | ||
| 901 | url => $opts->{$main_key}, | ||
| 902 | user => $opts->{$user_opt}, | ||
| 903 | password => $opts->{$password_opt} | ||
| 904 | } | ||
| 905 | } elsif ($server_config && $server_config->{$main_key}) { | ||
| 906 | # Use configuration directly from the server definition: | ||
| 907 | return $server_config->{$main_key} | ||
| 908 | } else { | ||
| 909 | return undef; | ||
| 910 | } | ||
| 911 | } | ||
| 912 | |||
| 913 | # Autoloading is used to fetch the proper connection parameters | ||
| 914 | sub AUTOLOAD { | ||
| 915 | my $self = shift; | ||
| 916 | my $np = $self->{np}; | ||
| 917 | my $name = $AUTOLOAD; | ||
| 918 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 919 | my $opts_name = $name; | ||
| 920 | $opts_name =~ s/_/-/; | ||
| 921 | |||
| 922 | if ($SERVER_CONFIG_KEYS->{$name}) { | ||
| 923 | return $np->opts->{$opts_name} if $np->opts->{$opts_name}; | ||
| 924 | my $c = $SERVER_CONFIG_KEYS->{$name}; | ||
| 925 | if ($c) { | ||
| 926 | my @parts = split "/",$c; | ||
| 927 | my $h = $self->_server_config || | ||
| 928 | return undef; | ||
| 929 | while (@parts) { | ||
| 930 | my $p = shift @parts; | ||
| 931 | return undef unless $h->{$p}; | ||
| 932 | $h = $h->{$p}; | ||
| 933 | return $h unless @parts; | ||
| 934 | } | ||
| 935 | } else { | ||
| 936 | return undef; | ||
| 937 | } | ||
| 938 | } else { | ||
| 939 | $self->nagios_die("No config attribute \"" . $name . "\" known"); | ||
| 940 | } | ||
| 941 | } | ||
| 942 | |||
| 943 | sub nagios_die { | ||
| 944 | my $self = shift; | ||
| 945 | my @args = @_; | ||
| 946 | |||
| 947 | my $np = $self->{np}; | ||
| 948 | $np->nagios_die(join("",@args),$np->opts->{'unknown-is-critical'} ? CRITICAL : UNKNOWN) | ||
| 949 | } | ||
| 950 | |||
| 951 | # Declared here to avoid AUTOLOAD confusions | ||
| 952 | sub DESTROY { | ||
| 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; |