diff --git a/centreon-plugins/apps/protocols/jmx/mode/numericvalue.pm b/centreon-plugins/apps/protocols/jmx/mode/numericvalue.pm index bd8ec6808..84aab39f8 100644 --- a/centreon-plugins/apps/protocols/jmx/mode/numericvalue.pm +++ b/centreon-plugins/apps/protocols/jmx/mode/numericvalue.pm @@ -81,9 +81,17 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Need to specify mbean-pattern option."); $self->{output}->option_exit(); } + $self->{request} = [ + { mbean => $self->{option_results}->{mbean_pattern} } + ]; if (!defined($self->{option_results}->{attribute}) || scalar($self->{option_results}->{attribute}) == 0) { $self->{option_results}->{attribute} = undef; - } + } else { + $self->{request}->[0]->{attributes} = []; + foreach (@{$self->{option_results}->{attribute}}) { + push @{$self->{request}->[0]->{attributes}}, { name => $_, path => $self->{option_results}->{lookup_path}}; + } + } if (defined($self->{option_results}->{type})) { foreach (@{$self->{option_results}->{type}}) { @@ -111,17 +119,17 @@ sub set_attributes { my ($self, %options) = @_; $self->{attributes} = {}; - $self->{attributes}->{type} = (defined($self->{option_results}->{type})) ? shift(@{$self->{option_results}->{type}}) : 'gauge'; - $self->{attributes}->{counter_per_seconds} = (defined($self->{option_results}->{counter_per_seconds})) ? shift(@{$self->{option_results}->{counter_per_seconds}}) : undef; - $self->{attributes}->{warning} = (defined($self->{option_results}->{warning})) ? shift(@{$self->{option_results}->{warning}}) : undef; - $self->{attributes}->{critical} = (defined($self->{option_results}->{critical})) ? shift(@{$self->{option_results}->{critical}}) : undef; - $self->{attributes}->{format} = (defined($self->{option_results}->{format})) ? shift(@{$self->{option_results}->{format}}) : 'current value' . $options{number} . ' is %s'; - $self->{attributes}->{format_scale} = (defined($self->{option_results}->{format_scale})) ? shift(@{$self->{option_results}->{format_scale}}) : undef; - $self->{attributes}->{format_scale_unit} = (defined($self->{option_results}->{format_scale_unit})) ? shift(@{$self->{option_results}->{format_scale_unit}}) : 'other'; - $self->{attributes}->{perfdata_unit} = (defined($self->{option_results}->{perfdata_unit})) ? shift(@{$self->{option_results}->{perfdata_unit}}) : ''; - $self->{attributes}->{perfdata_name} = (defined($self->{option_results}->{perfdata_name})) ? shift(@{$self->{option_results}->{perfdata_name}}) : 'value' . $options{number}; - $self->{attributes}->{perfdata_min} = (defined($self->{option_results}->{perfdata_min})) ? shift(@{$self->{option_results}->{perfdata_min}}) : ''; - $self->{attributes}->{perfdata_max} = (defined($self->{option_results}->{perfdata_max})) ? shift(@{$self->{option_results}->{perfdata_max}}) : ''; + $self->{attributes}->{type} = (defined($self->{option_results}->{type}) && scalar(@{$self->{option_results}->{type}}) > 0) ? shift(@{$self->{option_results}->{type}}) : 'gauge'; + $self->{attributes}->{counter_per_seconds} = (defined($self->{option_results}->{counter_per_seconds}) && scalar(@{$self->{option_results}->{counter_per_seconds}}) > 0) ? shift(@{$self->{option_results}->{counter_per_seconds}}) : undef; + $self->{attributes}->{warning} = (defined($self->{option_results}->{warning}) && scalar(@{$self->{option_results}->{warning}}) > 0) ? shift(@{$self->{option_results}->{warning}}) : undef; + $self->{attributes}->{critical} = (defined($self->{option_results}->{critical}) && scalar(@{$self->{option_results}->{critical}}) > 0) ? shift(@{$self->{option_results}->{critical}}) : undef; + $self->{attributes}->{format} = (defined($self->{option_results}->{format}) && scalar(@{$self->{option_results}->{format}}) > 0) ? shift(@{$self->{option_results}->{format}}) : 'current value' . $options{number} . ' is %s'; + $self->{attributes}->{format_scale} = (defined($self->{option_results}->{format_scale}) && scalar(@{$self->{option_results}->{format_scale}}) > 0) ? shift(@{$self->{option_results}->{format_scale}}) : undef; + $self->{attributes}->{format_scale_unit} = (defined($self->{option_results}->{format_scale_unit}) && scalar(@{$self->{option_results}->{format_scale_unit}}) > 0) ? shift(@{$self->{option_results}->{format_scale_unit}}) : 'other'; + $self->{attributes}->{perfdata_unit} = (defined($self->{option_results}->{perfdata_unit}) && scalar(@{$self->{option_results}->{perfdata_unit}}) > 0) ? shift(@{$self->{option_results}->{perfdata_unit}}) : ''; + $self->{attributes}->{perfdata_name} = (defined($self->{option_results}->{perfdata_name}) && scalar(@{$self->{option_results}->{perfdata_name}}) > 0) ? shift(@{$self->{option_results}->{perfdata_name}}) : 'value' . $options{number}; + $self->{attributes}->{perfdata_min} = (defined($self->{option_results}->{perfdata_min}) && scalar(@{$self->{option_results}->{perfdata_min}}) > 0) ? shift(@{$self->{option_results}->{perfdata_min}}) : ''; + $self->{attributes}->{perfdata_max} = (defined($self->{option_results}->{perfdata_max}) && scalar(@{$self->{option_results}->{perfdata_max}}) > 0) ? shift(@{$self->{option_results}->{perfdata_max}}) : ''; if ($self->{attributes}->{type} !~ /^gauge|counter$/i) { $self->{output}->add_option_msg(short_msg => "Wrong --type argument '" . $self->{attributes}->{type} . "' ('gauge' or 'counter')."); @@ -201,9 +209,14 @@ sub find_values { $self->{values} = []; if (defined($options{result})) { - if (!ref($options{result})) { - push @{$self->{values}}, $options{result} if ($options{result} =~ /^[0-9\.,]+$/); - } elsif (defined($self->{jpath})) { + if (defined($self->{option_results}->{attribute})) { + foreach (@{$self->{option_results}->{attribute}}) { + if (defined($options{result}->{$self->{option_results}->{mbean_pattern}}->{$_}) && !ref($options{result}->{$self->{option_results}->{mbean_pattern}}->{$_})) { + push @{$self->{values}}, $options{result}->{$self->{option_results}->{mbean_pattern}}->{$_} if ($options{result}->{$self->{option_results}->{mbean_pattern}}->{$_} =~ /^[0-9\.,]+$/); + } + } + } + if (defined($self->{jpath})) { my @values = (); eval { @@ -218,7 +231,7 @@ sub find_values { } } } - + if (scalar(@{$self->{values}}) == 0) { $self->{output}->output_add(severity => 'UNKNOWN', short_msg => 'Cannot find numeric values'); @@ -232,8 +245,8 @@ sub run { # $options{snmp} = snmp object $self->{connector} = $options{custom}; - my $result = $self->{connector}->get_attributes(mbean_pattern => $self->{option_results}->{mbean_pattern}, attributes => $self->{option_results}->{attribute}, path => $self->{option_results}->{lookup_path}); - + my $result = $self->{connector}->get_attributes(request => $self->{request}, nothing_quit => 1); + $self->find_values(result => $result); for (my $i = 1; $i <= scalar(@{$self->{values}}); $i++) { $self->set_attributes(number => $i); diff --git a/centreon-plugins/centreon/common/protocols/jmx/custom/jolokia.pm b/centreon-plugins/centreon/common/protocols/jmx/custom/jolokia.pm index 3673108a5..63ca2fb03 100644 --- a/centreon-plugins/centreon/common/protocols/jmx/custom/jolokia.pm +++ b/centreon-plugins/centreon/common/protocols/jmx/custom/jolokia.pm @@ -170,29 +170,90 @@ sub connect { $self->{jmx4perl} = new JMX::Jmx4Perl($self->{connect_params}); } +sub _add_request { + my ($self, %options) = @_; + + my $request = JMX::Jmx4Perl::Request->new(READ, $options{object}, + $options{attribute} + ); + if (!$request->is_mbean_pattern($options{object})) { + $request->{path} = $options{path}; + } + + return $request; +} + sub get_attributes { my ($self, %options) = @_; - my $dont_quit = defined($options{dont_quit}) && $options{dont_quit} == 1 ? 1 : 0; + my $nothing_quit = defined($options{nothing_quit}) && $options{nothing_quit} == 1 ? 1 : 0; + #$options{request} = [ + # { mbean => 'java.lang:name=*,type=MemoryPool', attributes => [ { name => 'CollectionUsage', path => 'committed' }, { name => 'Name', path => undef }, + # { name => 'NonHeapMemoryUsagePlop', path => undef } ] }, + # { mbean => 'java.lang:type=Memory', attributes => [ { name => 'NonHeapMemoryUsage' } ] }, + # { mbean => 'java.lang:type=Memory', attributes => [ { name => 'HeapMemoryUsage', path => 'committed' } ] }, + # { mbean => 'java.lang:type=Memory', attributes => [] }, + #]; if (!defined($self->{jmx4perl})) { $self->connect(); } - - my $resp; - eval { - local $SIG{__DIE__} = 'IGNORE'; - my $attributes = $options{attributes}; - if (defined($attributes) && ref($attributes) eq 'ARRAY' && scalar(@{$attributes}) == 1) { - $attributes = $attributes->[0]; + + my @requests = (); + for (my $i = 0; defined($options{request}) && $i < scalar(@{$options{request}}); $i++) { + my $object = $options{request}->[$i]->{mbean}; + for (my $j = 0; defined($options{request}->[$i]->{attributes}) && + $j < scalar(@{$options{request}->[$i]->{attributes}}) ; $j++) { + push @requests, $self->_add_request(object => $object, attribute => $options{request}->[$i]->{attributes}->[$j]->{name}, + path => $options{request}->[$i]->{attributes}->[$j]->{path}); + } + if (!defined($options{request}->[$i]->{attributes}) || scalar(@{$options{request}->[$i]->{attributes}}) == 0) { + push @requests, $self->_add_request(object => $object, path => $options{request}->[$i]->{path}); } - $resp = $self->{jmx4perl}->get_attribute($options{mbean_pattern}, $attributes, $options{path}); - }; - if ($@ && $dont_quit == 0) { - $self->{output}->add_option_msg(short_msg => "protocol issue: " . $@); - $self->{output}->option_exit(); } - return $resp; + my $response = {}; + my @responses = $self->{jmx4perl}->request(@requests); + for (my $i = 0, my $pos = 0; defined($options{request}) && $i < scalar(@{$options{request}}); $i++) { + for (my $j = 0; defined($options{request}->[$i]->{attributes}) && + $j < scalar(@{$options{request}->[$i]->{attributes}}); $j++, $pos++) { + if ($responses[$pos]->is_error()) { + # 500-599 an error. 400 is an attribute not present + if ($responses[$pos]->status() >= 500) { + $self->{output}->add_option_msg(short_msg => "protocol issue: " . $responses[$pos]->error_text()); + $self->{output}->option_exit(); + } + next; + } + + my $mbean = $responses[$pos]->{request}->{mbean}; + my $attribute = $responses[$pos]->{request}->{attribute}; + my $value = $responses[$pos]->{value}; + if ($requests[$pos]->is_mbean_pattern()) { + foreach (keys %{$responses[$pos]->{value}}) { + $response->{$_} = {} if (!defined($response->{$_})); + $response->{$_}->{$attribute} = $responses[$pos]->{value}->{$_}->{$attribute}; + } + } else { + $response->{$mbean} = {} if (!defined($response->{$mbean})); + $response->{$mbean}->{$attribute} = $value; + } + } + + if (!defined($options{request}->[$i]->{attributes}) || scalar(@{$options{request}->[$i]->{attributes}}) == 0) { + my $mbean = $responses[$pos]->{request}->{mbean}; + $response->{$mbean} = {} if (!defined($response->{$mbean})); + foreach (keys %{$responses[$pos]->{value}}) { + $response->{$mbean}->{$_} = $responses[$pos]->{value}->{$_}; + } + $pos++; + } + } + + if ($nothing_quit == 1 && scalar(keys %{$response}) == 0) { + $self->{output}->add_option_msg(short_msg => "JMX Request: Cant get a single value."); + $self->{output}->option_exit(); + } + return $response; } sub list_attributes {