+ Fix Class count mode for jmx java

This commit is contained in:
garnier-quentin 2015-06-30 17:00:29 +02:00
parent 90c968b776
commit 94dfdbda99

View File

@ -1,5 +1,5 @@
################################################################################ ################################################################################
# Copyright 2005-2013 MERETHIS # Copyright 2005-2014 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under # Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0. # GPL Licence 2.0.
# #
@ -29,7 +29,7 @@
# do not wish to do so, delete this exception statement from your version. # do not wish to do so, delete this exception statement from your version.
# #
# For more information : contact@centreon.com # For more information : contact@centreon.com
# Authors : Simon BOMM <sbomm@centreon.com> # Authors : Quentin Garnier <qgarnier@merethis.com>
# #
#################################################################################### ####################################################################################
@ -39,6 +39,37 @@ use base qw(centreon::plugins::mode);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::values;
use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex);
my $instance_mode;
my $maps_counters = {
class => {
'000_current' => { set => { key_values => [ { name => 'current' } ],
output_template => 'Current : %s',
perfdatas => [
{ label => 'current', value => 'current_absolute', template => '%s', min => 0 },
],
}
},
'001_loaded' => { set => { key_values => [ { name => 'loaded', diff => 1 } ],
output_template => 'Loaded : %s',
perfdatas => [
{ label => 'loaded', value => 'loaded_absolute', template => '%s', min => 0 },
],
}
},
'003_unloaded' => { set => { key_values => [ { name => 'unloaded', diff => 1 } ],
output_template => 'Unloaded : %s',
perfdatas => [
{ label => 'unloaded', value => 'unloaded_absolute', template => '%s', min => 0 },
],
}
},
},
};
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -48,11 +79,26 @@ sub new {
$self->{version} = '1.0'; $self->{version} = '1.0';
$options{options}->add_options(arguments => $options{options}->add_options(arguments =>
{ {
"warning-loaded:s" => { name => 'warning_loaded' },
"critical-loaded:s" => { name => 'critical_loaded' },
"warning-total:s" => { name => 'warning_total' },
"critical-total:s" => { name => 'critical_total' },
}); });
$self->{statefile_value} = centreon::plugins::statefile->new(%options);
foreach my $key (('class')) {
foreach (keys %{$maps_counters->{$key}}) {
my ($id, $name) = split /_/;
if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) {
$options{options}->add_options(arguments => {
'warning-' . $name . ':s' => { name => 'warning-' . $name },
'critical-' . $name . ':s' => { name => 'critical-' . $name },
});
}
$maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(
statefile => $self->{statefile_value},
output => $self->{output}, perfdata => $self->{perfdata},
label => $name);
$maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}});
}
}
return $self; return $self;
} }
@ -60,66 +106,97 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::init(%options);
if (($self->{perfdata}->threshold_validate(label => 'warning-loaded', value => $self->{option_results}->{warning_loaded})) == 0) { foreach my $key (('class')) {
$self->{output}->add_option_msg(short_msg => "Wrong warning-loaded threshold '" . $self->{option_results}->{warning_loaded} . "'."); foreach (keys %{$maps_counters->{$key}}) {
$self->{output}->option_exit(); $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results});
}
} }
if (($self->{perfdata}->threshold_validate(label => 'critical-loaded', value => $self->{option_results}->{critical_loaded})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical-loaded threshold '" . $self->{option_results}->{critical_loaded} . "'."); $self->{statefile_value}->check_options(%options);
$self->{output}->option_exit(); $instance_mode = $self;
}
if (($self->{perfdata}->threshold_validate(label => 'warning-total', value => $self->{option_results}->{warning_total})) == 0) { $self->{overload_th} = {};
$self->{output}->add_option_msg(short_msg => "Wrong warning-total threshold '" . $self->{option_results}->{warning_total} . "'."); foreach my $val (@{$self->{option_results}->{threshold_overload}}) {
$self->{output}->option_exit(); if ($val !~ /^(.*?),(.*?),(.*)$/) {
} $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'.");
if (($self->{perfdata}->threshold_validate(label => 'critical-total', value => $self->{option_results}->{critical_total})) == 0) { $self->{output}->option_exit();
$self->{output}->add_option_msg(short_msg => "Wrong critical-total threshold '" . $self->{option_results}->{critical_total} . "'."); }
$self->{output}->option_exit(); my ($section, $status, $filter) = ($1, $2, $3);
if ($self->{output}->is_litteral_status(status => $status) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'.");
$self->{output}->option_exit();
}
$self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section}));
push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status};
} }
} }
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
# $options{snmp} = snmp object
$self->{connector} = $options{custom}; $self->{connector} = $options{custom};
$self->{request} = [ $self->manage_selection();
{ mbean => "java.lang:type=ClassLoading", attributes => [ { name => 'UnloadedClassCount' }, { name => 'LoadedClassCount' }, { name => 'TotalLoadedClassCount' } ] },
];
my $result = $self->{connector}->get_attributes(request => $self->{request}, nothing_quit => 1); $self->{new_datas} = {};
my $exit1 = $self->{perfdata}->threshold_check(value => $result->{"java.lang:type=ClassLoading"}->{TotalLoadedClassCount}, $self->{statefile_value}->read(statefile => "jvm_standard_" . $self->{mode} . '_' . md5_hex($self->{connector}->{url}));
threshold => [ { label => 'critical-total', exit_litteral => 'critical' }, { label => 'warning-total', exit_litteral => 'warning'} ]); $self->{new_datas}->{last_timestamp} = time();
my $exit2 = $self->{perfdata}->threshold_check(value => $result->{"java.lang:type=ClassLoading"}->{LoadedClassCount},
threshold => [ { label => 'critical-loaded', exit_litteral => 'critical' }, { label => 'warning-loaded', exit_litteral => 'warning'} ]);
my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2 ]); my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', '');
my @exits;
foreach (sort keys %{$maps_counters->{class}}) {
my $obj = $maps_counters->{class}->{$_}->{obj};
$obj->set(instance => 'global');
$self->{output}->output_add(severity => $exit, my ($value_check) = $obj->execute(values => $self->{global},
short_msg => sprintf("Loaded Class Count : %i, Total Loaded Class : %i, Unloaded Class Count : %i", new_datas => $self->{new_datas});
$result->{"java.lang:type=ClassLoading"}->{LoadedClassCount}, $result->{"java.lang:type=ClassLoading"}->{TotalLoadedClassCount},
$result->{"java.lang:type=ClassLoading"}->{UnloadedClassCount}));
$self->{output}->perfdata_add(label => 'TotalLoadedClassCount', if ($value_check != 0) {
value => $result->{"java.lang:type=ClassLoading"}->{TotalLoadedClassCount}, $long_msg .= $long_msg_append . $obj->output_error();
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-total'), $long_msg_append = ', ';
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-total'), next;
min => 0); }
my $exit2 = $obj->threshold_check();
push @exits, $exit2;
$self->{output}->perfdata_add(label => 'LoadedClassCount', my $output = $obj->output();
value => $result->{"java.lang:type=ClassLoading"}->{LoadedClassCount}, $long_msg .= $long_msg_append . $output;
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-loaded'), $long_msg_append = ', ';
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-loaded'),
min => 0);
$self->{output}->perfdata_add(label => 'UnloadedClassCount', if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) {
value => $result->{"java.lang:type=ClassLoading"}->{UnloadedClassCount}, $short_msg .= $short_msg_append . $output;
min => 0); $short_msg_append = ', ';
}
$obj->perfdata();
}
my $exit = $self->{output}->get_most_critical(status => [ @exits ]);
if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) {
$self->{output}->output_add(severity => $exit,
short_msg => "Class $short_msg"
);
} else {
$self->{output}->output_add(short_msg => "Class $long_msg");
}
$self->{statefile_value}->write(data => $self->{new_datas});
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
}
sub manage_selection {
my ($self, %options) = @_;
my $mbean = 'java.lang:type=ClassLoading';
$self->{request} = [
{ mbean => $mbean, attributes => [ { name => 'UnloadedClassCount' }, { name => 'LoadedClassCount' }, { name => 'TotalLoadedClassCount' } ] },
];
my $result = $self->{connector}->get_attributes(request => $self->{request}, nothing_quit => 1);
$self->{global} = {};
$self->{global}->{unloaded} = $result->{$mbean}->{UnloadedClassCount} if (defined($result->{$mbean}->{UnloadedClassCount}));
$self->{global}->{loaded} = $result->{$mbean}->{TotalLoadedClassCount} if (defined($result->{$mbean}->{TotalLoadedClassCount}));
$self->{global}->{current} = $result->{$mbean}->{LoadedClassCount} if (defined($result->{$mbean}->{LoadedClassCount}));
} }
1; 1;
@ -128,30 +205,20 @@ __END__
=head1 MODE =head1 MODE
Check Java Class Loading Mbean (Mbean java.lang:type=ClassLoading). Check Java Class Loading Mbean.
Example:
perl centreon_plugins.pl --plugin=apps::tomcat::jmx::plugin --custommode=jolokia --url=http://10.30.2.22:8080/jolokia-war --mode=classcount --warning-loaded 60 --critical-loaded 75 --warning-total 65 --critical-total 75
=over 8 =over 8
=item B<--warning-loaded> =item B<--warning-*>
Current number of loaded class triggering a warning Threshold warning.
Can be: 'unloaded', 'loaded', 'current'.
=item B<--critical-loaded> =item B<--critical-*>
Current number of loaded class triggering a critical Threshold critical.
Can be: 'unloaded', 'loaded', 'current'.
=item B<--warning-total>
Total number of loaded class triggering a warning
=item B<--critical-total>
Total number total of loaded class triggering a critical
=back =back
=cut =cut