enh(plugin): add mq mode + simplify api calls

This commit is contained in:
thibaults-centreon 2020-07-03 15:39:03 +02:00
parent ba40ef4a66
commit 81682499ab
9 changed files with 451 additions and 54 deletions

View File

@ -98,7 +98,9 @@ sub check_options {
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{authent_endpoint} = (defined($self->{option_results}->{authent_endpoint})) ? $self->{option_results}->{authent_endpoint} : '/accounts/login';
$self->{monitoring_endpoint} = (defined($self->{option_results}->{monitoring_endpoint})) ? $self->{option_results}->{monitoring_endpoint} : '/hybrid/api/v1';
$self->{monitoring_endpoint}->{arm} = (defined($self->{option_results}->{monitoring_endpoint})) ? $self->{option_results}->{monitoring_endpoint} : '/hybrid/api/v1';
$self->{monitoring_endpoint}->{mq_admin} = (defined($self->{option_results}->{monitoring_endpoint})) ? $self->{option_results}->{monitoring_endpoint} : '/mq/admin/api/v1';
$self->{monitoring_endpoint}->{mq_stats} = (defined($self->{option_results}->{monitoring_endpoint})) ? $self->{option_results}->{monitoring_endpoint} : '/mq/stats/api/v1';
$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->{environment_id} = (defined($self->{option_results}->{environment_id})) ? $self->{option_results}->{environment_id} : '';
@ -212,64 +214,108 @@ sub request_api {
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
if (defined($decoded->{error_code})) {
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}, debug => 1);
$self->{output}->add_option_msg(short_msg => "API returns error code '" . $decoded->{error_code} . "' (add --debug option for detailed message)");
$self->{output}->option_exit();
}
# if ( defined($decoded->{error_code}) ) {
# $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}, debug => 1);
# $self->{output}->add_option_msg(short_msg => "API returns error code '" . $decoded->{error_code} . "' (add --debug option for detailed message)");
# $self->{output}->option_exit();
# }
return $decoded;
}
sub list_applications {
sub list_objects {
my ($self, %options) = @_;
my $url_path = $self->{monitoring_endpoint} . '/applications';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
if ($options{api_type} eq 'arm') {
my $url_path = $self->{monitoring_endpoint}->{arm} . $options{endpoint};
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{data};
};
return $response->{data};
if ($options{api_type} eq 'mq') {
my $url_path = $self->{monitoring_endpoint}->{mq_admin} .
'/organizations/' . $self->{organization_id} .
'/environments/' . $self->{environment_id} .
'/regions/' . $options{region_id} .
$options{endpoint};
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response;
};
}
sub get_application_status {
sub get_objects_status {
my ($self, %options) = @_;
my $url_path = $self->{monitoring_endpoint} . '/applications/' . $options{applicationId};
my $response = $self->request_api(method => 'GET', url_path => $url_path);
if ($options{api_type} eq 'arm') {
my $url_path = $self->{monitoring_endpoint}->{arm} . $options{endpoint} . $options{object_id};
my $response = $self->request_api(method => 'GET', url_path => $url_path);
return $response->{data};
};
if ($options{api_type} eq 'mq') {
my $url_path = $self->{monitoring_endpoint}->{mq_stats} .
'/organizations/' . $self->{organization_id} .
'/environments/' . $self->{environment_id} .
'/regions/' . $options{region_id} .
$options{endpoint} . '/' . $options{object_id};
my $response = $self->request_api(method => 'GET', url_path => $url_path, get_param => $options{get_param});
return $response;
};
return $response->{data};
}
sub list_servers {
my ($self, %options) = @_;
my $url_path = $self->{monitoring_endpoint} . '/servers/';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
# sub list_applications {
# my ($self, %options) = @_;
return $response->{data};
}
# my $url_path = $self->{monitoring_endpoint}->{arm} . '/applications';
# my $response = $self->request_api(method => 'GET', url_path => $url_path);
sub get_server_status {
my ($self, %options) = @_;
# return $response->{data};
# }
my $url_path = $self->{monitoring_endpoint} . '/servers/' . $options{serverId};
my $response = $self->request_api(method => 'GET', url_path => $url_path);
# sub get_application_status {
# my ($self, %options) = @_;
return $response->{data};
}
# my $url_path = $self->{monitoring_endpoint} . '/applications/' . $options{applicationId};
# my $response = $self->request_api(method => 'GET', url_path => $url_path);
sub list_clusters {
my ($self, %options) = @_;
# return $response->{data};
# }
my $url_path = $self->{monitoring_endpoint} . '/clusters/';
my $response = $self->request_api(method => 'GET', url_path => $url_path);
# sub list_servers {
# my ($self, %options) = @_;
return $response->{data};
}
# my $url_path = $self->{monitoring_endpoint} . '/servers/';
# my $response = $self->request_api(method => 'GET', url_path => $url_path);
# return $response->{data};
# }
# sub get_server_status {
# my ($self, %options) = @_;
# my $url_path = $self->{monitoring_endpoint} . '/servers/' . $options{serverId};
# my $response = $self->request_api(method => 'GET', url_path => $url_path);
# return $response->{data};
# }
# sub list_clusters {
# my ($self, %options) = @_;
# my $url_path = $self->{monitoring_endpoint} . '/clusters/';
# my $response = $self->request_api(method => 'GET', url_path => $url_path);
# return $response->{data};
# }
sub cache_hosts {
my ($self, %options) = @_;

View File

@ -117,7 +117,8 @@ sub manage_selection {
$self->{global} = { started => 0, stopped => 0, failed => 0 };
$self->{applications} = {};
my $result = $options{custom}->list_applications();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/applications');
foreach ('started', 'stopped', 'failed') { $self->{global}->{$_} = 0; };
foreach my $application (@{$result}) {
next if ( defined($self->{option_results}->{filter_name})

View File

@ -117,7 +117,9 @@ sub manage_selection {
$self->{global} = { started => 0, stopped => 0, failed => 0 };
$self->{clusters} = {};
my $result = $options{custom}->list_clusters();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/clusters');
foreach ('running', 'disconnected') { $self->{global}->{$_} = 0; };
foreach my $cluster (@{$result}) {
next if ( defined($self->{option_results}->{filter_name})
&& $self->{option_results}->{filter_name} ne ''

View File

@ -42,16 +42,11 @@ sub check_options {
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{data} = $options{custom}->list_applications();
}
sub run {
my ($self, %options) = @_;
my $result = $options{custom}->list_applications();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/applications');
foreach my $application (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $application->{name} !~ /$self->{option_results}->{filter_name}/);
@ -80,8 +75,11 @@ sub disco_format {
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach my $application (@{$self->{data}}) {
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/applications');
foreach my $application (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $application->{name} !~ /$self->{option_results}->{filter_name}/);
$self->{output}->add_disco_entry(
id => $application->{id},
name => $application->{name},

View File

@ -0,0 +1,121 @@
#
# Copyright 2020 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::mulesoft::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 => {
'filter-name:s' => { name => 'filter_name' },
'region-id:s' => { name => 'region_id' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{region_id}) || $self->{option_results}->{region_id} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --region-id option.");
$self->{output}->option_exit();
}
}
sub run {
my ($self, %options) = @_;
my $result = $options{custom}->list_objects(
api_type => 'mq',
endpoint => '/destinations',
region_id => $self->{option_results}->{region_id},
);
foreach my $queue (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $queue->{queueId} !~ /$self->{option_results}->{filter_name}/);
$self->{output}->output_add(
long_msg => sprintf(
"[name = %s]",
$queue->{queueId}
)
);
}
$self->{output}->output_add(severity => 'OK', short_msg => 'Mulesoft Anypoint Message Qeues:');
$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']);
}
sub disco_show {
my ($self, %options) = @_;
my $result = $options{custom}->list_objects(
api_type => 'mq',
endpoint => '/destinations',
region_id => $self->{option_results}->{region_id},
);
foreach my $queue (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $queue->{queueId} !~ /$self->{option_results}->{filter_name}/);
$self->{output}->add_disco_entry(name => $queue->{queueId});
}
}
1;
__END__
=head1 MODE
List Mulesoft Anypoint Messages Queues.
=over 8
=item B<--filter-name>
Filter on queue name (Can be a regexp).
=item B<--region-id>
Specify the queue region ID (Mandatory).
Example: --region-id='eu-central-1'
=back
=cut

View File

@ -45,7 +45,7 @@ sub check_options {
sub run {
my ($self, %options) = @_;
my $result = $options{custom}->list_servers();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/servers');
foreach my $server (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $server->{name} !~ /$self->{option_results}->{filter_name}/);
@ -74,8 +74,10 @@ sub disco_format {
sub disco_show {
my ($self, %options) = @_;
my $result = $options{custom}->list_servers();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/servers');
foreach my $server (@{$result}) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $server->{name} !~ /$self->{option_results}->{filter_name}/);
$self->{output}->add_disco_entry(
id => $server->{id},
name => $server->{name},

View File

@ -0,0 +1,223 @@
#
# Copyright 2020 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::mulesoft::restapi::mode::messages;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use DateTime;
use POSIX qw(strftime);
use base qw(centreon::plugins::templates::counter);
sub prefix_queue_output {
my ($self, %options) = @_;
return "Queue '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'queues', type => 1, cb_prefix_output => 'prefix_queue_output', message_multiple => 'All queues are ok' }
];
$self->{maps_counters}->{queues} = [
{ label => 'total', nlabel => 'mulesoft.mq.messages.total.count', set => {
key_values => [ { name => 'total' }, { name => 'display' } ],
output_template => "Total : %s",
perfdatas => [ { value => 'total', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'inflight', nlabel => 'mulesoft.mq.inflight.count', set => {
key_values => [ { name => 'inflight' }, { name => 'display' } ],
output_template => "inflight : %s",
perfdatas => [ { value => 'inflight', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'received', nlabel => 'mulesoft.mq.received.count', set => {
key_values => [ { name => 'received' }, { name => 'display' } ],
output_template => "received : %s",
perfdatas => [ { value => 'received', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'sent', nlabel => 'mulesoft.mq.sent.count', set => {
key_values => [ { name => 'sent' }, { name => 'display' } ],
output_template => "sent : %s",
perfdatas => [ { value => 'sent', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'visible', nlabel => 'mulesoft.mq.visible.count', set => {
key_values => [ { name => 'visible' }, { name => 'display' } ],
output_template => "visible : %s",
perfdatas => [ { value => 'visible', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'acked', nlabel => 'mulesoft.mq.acked.count', set => {
key_values => [ { name => 'acked' }, { name => 'display' } ],
output_template => "acked : %s",
perfdatas => [ { value => 'acked', template => '%d', min => 0, cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ]
}
},
];
}
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 => {
'filter-name:s' => { name => 'filter_name' },
'region-id:s' => { name => 'region_id' },
'timeframe:s' => { name => 'timeframe', default => '600' },
'period:s' => { name => 'period', default => '60' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{region_id}) || $self->{option_results}->{region_id} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --region-id option.");
$self->{output}->option_exit();
}
}
sub manage_selection {
my ($self, %options) = @_;
my $time = time();
my $end_time = strftime('%a, %d %b %Y %H:%M:%S', gmtime()) . ' GMT';
my $start_time = strftime('%a, %d %b %Y %H:%M:%S', gmtime($time - $self->{option_results}->{timeframe})) . ' GMT';
my $destinations = $options{custom}->list_objects(
api_type => 'mq',
endpoint => '/destinations',
region_id => $self->{option_results}->{region_id},
);
my $get_param = [
'startDate=' . $start_time,
'endDate=' . $end_time,
'period=' . $self->{option_results}->{period}
];
foreach my $queue (@{$destinations}) {
next if (defined($self->{option_results}->{filter_name})
&& $self->{option_results}->{filter_name} ne ''
&& $queue->{queueId} !~ /$self->{option_results}->{filter_name}/ );
my $queue_stats = $options{custom}->get_objects_status(
api_type => 'mq',
endpoint => '/queues',
region_id => $self->{option_results}->{region_id},
object_id => $queue->{queueId},
get_param => $get_param
);
my $message_type = {};
my $total_value;
foreach my $message_type (keys %$queue_stats) {
next if ($message_type =~ 'destination');
foreach my $values (@{$queue_stats->{$message_type}} ) {
push @{$self->{raw_results}->{$queue->{queueId}}->{$message_type}}, $values->{value};
$total_value += $values->{value};
}
my $points = scalar(@{$self->{raw_results}->{$queue->{queueId}}->{$message_type}});
$self->{raw_results}->{$queue->{queueId}}{$message_type} = $total_value / $points;
}
$self->{queues}->{$queue->{queueId}} = {
display => $queue->{queueId},
total => $self->{raw_results}->{$queue->{queueId}}{messages},
inflight => $self->{raw_results}->{$queue->{queueId}}{inflightMessages},
received => $self->{raw_results}->{$queue->{queueId}}{messagesReceived},
sent => $self->{raw_results}->{$queue->{queueId}}{messagesSent},
visible => $self->{raw_results}->{$queue->{queueId}}{messagesVisible},
acked => $self->{raw_results}->{$queue->{queueId}}{messagesAcked}
}
}
if (scalar(keys %{$self->{queues}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No queues found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check Mulesoft Messages Queues (MQ).
Example:
perl centreon_plugins.pl --plugin=apps::mulesoft::restapi::plugin --mode=messages
--environment-id='1234abc-56de-78fg-90hi-1234abcdefg' --organization-id='1234abcd-56ef-78fg-90hi-1234abcdefg'
--api-username='myapiuser' --api-password='myapipassword' --warning-total=900 --critical-total=1000 --verbose
More information on 'https://anypoint.mulesoft.com/exchange/portals/anypoint-platform/f1e97bc6-315a-4490-82a7-23abe036327a.anypoint-platform/anypoint-mq-stats/'.
=over 8
=item B<--filter-name>
Filter by queue name (Regexp can be used).
Example: --filter-name='^mymessagequeue1$'
=item B<--region-id>
Specify the queue region ID (Mandatory).
Example: --region-id='eu-central-1'
=item B<--timeframe>
Specify the time window in seconds on which to collect the data.
Default: 600 (5 min)
=item B<--period>
Specify the data granularity in seconds.
Default: 60 (1 value/minute)
=item B<--warning-*>
Warning threshold for queue messages count, by message type where * can be:
total, inflight, received, sent, visible, acked.
=item B<--critical-*>
Critical threshold for queue messages count, by message type where * can be:
total, inflight, received, sent, visible, acked.
=back
=cut

View File

@ -42,19 +42,19 @@ sub set_counters {
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'mulesoft.servers.total.count', set => {
key_values => [ { name => 'total' } ],
output_template => 'Total : %s',
output_template => '%s',
perfdatas => [ { value => 'total', template => '%d', min => 0 } ]
}
},
{ label => 'running', nlabel => 'mulesoft.servers.status.running.count', set => {
key_values => [ { name => 'running' } ],
output_template => 'Running : %s',
output_template => 'running : %s',
perfdatas => [ { value => 'running', template => '%d', min => 0 } ]
}
},
{ label => 'disconnected', nlabel => 'mulesoft.servers.status.disconnected.count', set => {
key_values => [ { name => 'disconnected' } ],
output_template => 'Disconnected : %s',
output_template => 'disconnected : %s',
perfdatas => [ { value => 'disconnected', template => '%d', min => 0 } ]
}
}
@ -96,7 +96,7 @@ sub check_options {
sub prefix_global_output {
my ($self, %options) = @_;
return "Total servers ";
return "Total servers :";
}
sub prefix_server_output {
@ -110,7 +110,9 @@ sub manage_selection {
$self->{global} = { started => 0, stopped => 0, failed => 0 };
$self->{servers} = {};
my $result = $options{custom}->list_servers();
my $result = $options{custom}->list_objects(api_type => 'arm', endpoint => '/servers');
foreach ('running', 'disconnected') { $self->{global}->{$_} = 0; };
foreach my $server (@{$result}) {
next if ( defined($self->{option_results}->{filter_name})
&& $self->{option_results}->{filter_name} ne ''

View File

@ -31,11 +31,13 @@ sub new {
$self->{version} = '1.0';
%{$self->{modes}} = (
'applications' => 'apps::mulesoft::restapi::mode::applications',
'listapplications' => 'apps::mulesoft::restapi::mode::listapplications',
'servers' => 'apps::mulesoft::restapi::mode::servers',
'listservers' => 'apps::mulesoft::restapi::mode::listservers',
'clusters' => 'apps::mulesoft::restapi::mode::clusters'
'applications' => 'apps::mulesoft::restapi::mode::applications',
'clusters' => 'apps::mulesoft::restapi::mode::clusters',
'messages' => 'apps::mulesoft::restapi::mode::messages',
'servers' => 'apps::mulesoft::restapi::mode::servers',
'list-applications' => 'apps::mulesoft::restapi::mode::listapplications',
'list-queues' => 'apps::mulesoft::restapi::mode::listqueues',
'list-servers' => 'apps::mulesoft::restapi::mode::listservers'
);
$self->{custom_modes}{restapi} = 'apps::mulesoft::restapi::custom::api';