add(plugin): ibmmq restapi (#3144)

This commit is contained in:
qgarnier 2021-09-27 13:57:16 +02:00 committed by GitHub
parent f6e0428792
commit 699112618c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 854 additions and 0 deletions

View File

@ -0,0 +1,205 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'timeout:s' => { name => 'timeout' },
'url-path:s' => { name => 'url_path' },
'unknown-http-status:s' => { name => 'unknown_http_status' },
'warning-http-status:s' => { name => 'warning_http_status' },
'critical-http-status:s' => { name => 'critical_http_status' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {}
sub check_options {
my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 9443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/ibmmq/rest/v1/admin';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
$self->{output}->option_exit();
}
if ($self->{api_username} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
$self->{output}->option_exit();
}
return 0;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{credentials} = 1;
$self->{option_results}->{basic} = 1;
$self->{option_results}->{username} = $self->{api_username};
$self->{option_results}->{password} = $self->{api_password};
}
sub settings {
my ($self, %options) = @_;
return if (defined($self->{settings_done}));
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}});
$self->{settings_done} = 1;
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
my $content = $self->{http}->request(
url_path => $self->{url_path} . $options{endpoint},
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status},
get_param => $options{get_param}
);
if (!defined($content) || $content eq '') {
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
return $decoded;
}
1;
__END__
=head1 NAME
IBM MQ Rest API
=head1 REST API OPTIONS
IBM MQ Rest API
=over 8
=item B<--hostname>
Administrative server hostname.
=item B<--port>
Port used (Default: 9443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--url-path>
API url path (Default: '/ibmmq/rest/v1/admin')
=item B<--api-username>
API username.
=item B<--api-password>
API password.
=item B<--timeout>
Set timeout in seconds (Default: 50).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,107 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::mode::listqueuemanagers;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $infos = $options{custom}->request_api(
endpoint => '/qmgr/'
);
my $results = {};
foreach my $entry (@{$infos->{qmgr}}) {
$results->{ $entry->{name} } = $entry;
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (sort keys %$results) {
$self->{output}->output_add(long_msg =>
sprintf(
'[name: %s][state: %s]',
$results->{$_}->{name},
$results->{$_}->{state}
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List queue managers:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['name', 'state']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (sort keys %$results) {
$self->{output}->add_disco_entry(%{$results->{$_}});
}
}
1;
__END__
=head1 MODE
List queue managers.
=over 8
=back
=cut

View File

@ -0,0 +1,117 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::mode::listqueues;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $list_qmgr = $options{custom}->request_api(
endpoint => '/qmgr/'
);
my $results = {};
foreach my $qmgr (@{$list_qmgr->{qmgr}}) {
my $queues = $options{custom}->request_api(
endpoint => '/qmgr/' . $qmgr->{name} . '/queue'
);
foreach my $queue (@{$queues->{queue}}) {
$results->{ $qmgr->{name} . ':' . $queue->{name} } = {
qmgr => $qmgr->{name},
name => $queue->{name},
type => $queue->{type}
};
}
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (sort keys %$results) {
$self->{output}->output_add(long_msg =>
sprintf(
'[qmgr: %s][name: %s][types: %s]',
$results->{$_}->{qmgr},
$results->{$_}->{name},
$results->{$_}->{type}
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List queues:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['qmgr', 'name', 'type']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (sort keys %$results) {
$self->{output}->add_disco_entry(%{$results->{$_}});
}
}
1;
__END__
=head1 MODE
List queues.
=over 8
=back
=cut

View File

@ -0,0 +1,169 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::mode::queuemanagers;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output {
my ($self, %options) = @_;
return sprintf(
'status: %s [channel initiator: %s]',
$self->{result_values}->{mgr_status},
$self->{result_values}->{channel_initiator_status}
);
}
sub prefix_qmgr_output {
my ($self, %options) = @_;
return "Queue manager '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'qmgr', type => 1, cb_prefix_output => 'prefix_qmgr_output', skipped_code => { -10 => 1 } }
];
$self->{maps_counters}->{qmgr} = [
{ label => 'status', type => 2, critical_default => '%{mgr_status} !~ /running/i', set => {
key_values => [
{ name => 'mgr_status' }, { name => 'channel_initiator_status' },
{ name => 'display' }
],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'connections', nlabel => 'queuemanager.connections.count', set => {
key_values => [ { name => 'connection_count' } ],
output_template => 'current number of connections: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'qmgr-name:s' => { name => 'qmgr_name' },
'filter-qmgr-name:s' => { name => 'filter_qmgr_name' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my @lists = ();
if (defined($self->{option_results}->{qmgr_name}) && $self->{option_results}->{qmgr_name} ne '') {
@lists = ($self->{option_results}->{qmgr_name});
} else {
my $names = $options{custom}->request_api(
endpoint => '/qmgr/'
);
foreach (@{$names->{qmgr}}) {
push @lists, $_->{name};
}
}
$self->{qmgr} = {};
foreach my $name (@lists) {
if (defined($self->{option_results}->{filter_qmgr_name}) && $self->{option_results}->{filter_qmgr_name} ne '' &&
$name !~ /$self->{option_results}->{filter_qmgr_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next;
}
my $infos = $options{custom}->request_api(
endpoint => '/qmgr/' . $name,
get_param => ['status=*']
);
$self->{qmgr}->{$name} = {
display => $name,
channel_initiator_status => lc($infos->{qmgr}->[0]->{status}->{channelInitiatorState}),
mgr_status => lc($infos->{qmgr}->[0]->{state}),
connection_count => $infos->{qmgr}->[0]->{status}->{connectionCount}
};
}
if (scalar(keys %{$self->{qmgr}}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'No queue managers found');
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check queue managers.
=over 8
=item B<--qmgr-name>
Check exact queue manager (no API listing call).
=item B<--filter-qmgr-name>
Filter queue managers by name (can be a regexp).
=item B<--unknown-status>
Set unknown threshold for status.
Can used special variables like: %{mgr_status}, %{channel_initiator_status}
=item B<--warning-status>
Set warning threshold for status.
Can used special variables like: %{mgr_status}, %{channel_initiator_status}
=item B<--critical-status>
Set critical threshold for status (Default: '%{mgr_status} !~ /running/i').
Can used special variables like: %{mgr_status}, %{channel_initiator_status}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'connections'.
=back
=cut

View File

@ -0,0 +1,204 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::mode::queues;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::misc;
sub custom_oldest_output {
my ($self, %options) = @_;
return sprintf(
'oldest message: %s',
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{oldest_msg_age})
);
}
sub qmgr_long_output {
my ($self, %options) = @_;
return "checking queue manager '" . $options{instance_value}->{name} . "'";
}
sub prefix_qmgr_output {
my ($self, %options) = @_;
return "queue manager '" . $options{instance_value}->{name} . "' ";
}
sub prefix_queue_output {
my ($self, %options) = @_;
return "queue '" . $options{instance_value}->{name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'qmgr', type => 3, cb_prefix_output => 'prefix_qmgr_output', cb_long_output => 'qmgr_long_output', indent_long_output => ' ', message_multiple => 'All queue managers are ok',
group => [
{ name => 'queues', display_long => 1, cb_prefix_output => 'prefix_queue_output', message_multiple => 'queues are ok', type => 1, skipped_code => { -10 => 1 } }
]
}
];
$self->{maps_counters}->{queues} = [
{ label => 'connections-input', nlabel => 'queue.connections.input.count', set => {
key_values => [ { name => 'open_input_count' } ],
output_template => 'current input connections: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
},
{ label => 'messages-depth', nlabel => 'queue.messages.depth.count', set => {
key_values => [ { name => 'current_qdepth' }],
output_template => 'current messages depth: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1 }
]
}
},
{ label => 'message-oldest', nlabel => 'queue.message.oldest.seconds', set => {
key_values => [ { name => 'oldest_msg_age' } ],
closure_custom_output => $self->can('custom_oldest_output'),
closure_custom_perfdata => $self->can('custom_oldest_perfdata')
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'qmgr-name:s' => { name => 'qmgr_name' },
'queue-name:s' => { name => 'queue_name' },
'filter-qmgr-name:s' => { name => 'filter_qmgr_name' },
'filter-queue-name:s' => { name => 'filter_queue_name' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my @list_qmgr = ();
if (defined($self->{option_results}->{qmgr_name}) && $self->{option_results}->{qmgr_name} ne '') {
@list_qmgr = ($self->{option_results}->{qmgr_name});
} else {
my $names = $options{custom}->request_api(
endpoint => '/qmgr/'
);
foreach (@{$names->{qmgr}}) {
push @list_qmgr, $_->{name};
}
}
my $found = 0;
$self->{qmgr} = {};
foreach my $qmgr_name (@list_qmgr) {
if (defined($self->{option_results}->{filter_qmgr_name}) && $self->{option_results}->{filter_qmgr_name} ne '' &&
$qmgr_name !~ /$self->{option_results}->{filter_qmgr_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $qmgr_name . "': no matching filter.", debug => 1);
next;
}
my $endpoint = '/qmgr/' . $qmgr_name . '/queue';
if (defined($self->{option_results}->{queue_name}) && $self->{option_results}->{queue_name} ne '') {
$endpoint .= '/' . $self->{option_results}->{queue_name};
}
my $queues = $options{custom}->request_api(
endpoint => $endpoint,
get_param => ['status=*']
);
foreach my $queue (@{$queues->{queue}}) {
if (defined($self->{option_results}->{filter_queue_name}) && $self->{option_results}->{filter_queue_name} ne '' &&
$queue->{name} !~ /$self->{option_results}->{filter_queue_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $queue->{name} . "': no matching filter.", debug => 1);
next;
}
unless (defined($self->{qmgr}->{$qmgr_name})) {
$self->{qmgr}->{$qmgr_name} = {
name => $qmgr_name,
queues => {}
};
}
$self->{qmgr}->{$qmgr_name}->{queues}->{ $queue->{name} } = {
name => $queue->{name},
open_input_count => $queue->{status}->{openInputCount},
current_qdepth => $queue->{status}->{currentDepth},
oldest_msg_age => $queue->{status}->{oldestMessageAge}
};
$found = 1;
}
}
unless ($found) {
$self->{output}->add_option_msg(short_msg => 'No queue found');
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check queues.
=over 8
=item B<--qmgr-name>
Check exact queue manager.
=item B<--queue-name>
Check exact queue.
=item B<--filter-qmgr-name>
Filter queue managers by name (can be a regexp).
=item B<--filter-queue-name>
Filter queues by name (can be a regexp).
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'connections-input', 'messages-depth', 'message-oldest'.
=back
=cut

View File

@ -0,0 +1,52 @@
#
# Copyright 2021 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package apps::mq::ibmmq::restapi::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '0.1';
$self->{modes} = {
'list-queue-managers' => 'apps::mq::ibmmq::restapi::mode::listqueuemanagers',
'list-queues' => 'apps::mq::ibmmq::restapi::mode::listqueues',
'queues' => 'apps::mq::ibmmq::restapi::mode::queues',
'queue-managers' => 'apps::mq::ibmmq::restapi::mode::queuemanagers'
};
$self->{custom_modes}->{api} = 'apps::mq::ibmmq::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check IBM MQ using Rest API.
=cut