(plugin) network:🇦🇼:orchestrator::restapi - new (#3913)

* wip

* wip

* wip
This commit is contained in:
qgarnier 2022-09-22 16:35:00 +02:00 committed by GitHub
parent 4771c10510
commit af11385083
5 changed files with 792 additions and 0 deletions

View File

@ -0,0 +1,202 @@
#
# Copyright 2022 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 network::aruba::orchestrator::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 => {
'access-token:s' => { name => 'access_token' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'timeout:s' => { name => 'timeout' },
'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, default_backend => 'curl');
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} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
$self->{access_token} = (defined($self->{option_results}->{access_token})) ? $self->{option_results}->{access_token} : '';
$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->{access_token} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --access-token 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};
}
sub settings {
my ($self, %options) = @_;
return if (defined($self->{settings_done}));
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$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 get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub request_api {
my ($self, %options) = @_;
if (defined($options{query_form_post})) {
$options{query_form_post} = JSON::XS->new->encode($options{query_form_post});
}
$self->settings();
my $content = $self->{http}->request(
method => defined($options{method}) ? $options{method} : 'GET',
url_path => '/gms/rest' . $options{endpoint},
get_param => $options{get_param},
header => ['X-Auth-Token: ' . $self->{access_token}],
query_form_post => $options{query_form_post},
unknown_status => $self->{unknown_http_status},
warning_status => $self->{warning_http_status},
critical_status => $self->{critical_http_status}
);
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->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
Aruba Orchestrator Rest API
=head1 REST API OPTIONS
Aruba Orchestrator Rest API
=over 8
=item B<--hostname>
Set hostname.
=item B<--port>
Port used (Default: 443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--access-token>
API token.
=item B<--timeout>
Set timeout in seconds (Default: 50).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,186 @@
#
# Copyright 2018 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 network::aruba::orchestrator::restapi::mode::alarms;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use DateTime;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output {
my ($self, %options) = @_;
return sprintf(
"alarm [severity: %s] [name: %s] [hostname: %s] %s",
$self->{result_values}->{severity},
$self->{result_values}->{name},
$self->{result_values}->{hostname},
$self->{result_values}->{timeraised}
);
}
sub prefix_global_output {
my ($self, %options) = @_;
return 'Alarms severity ';
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
{ name => 'alarms', type => 2, message_multiple => '0 alarm detected', format_output => '%s alarms detected', display_counter_problem => { nlabel => 'alerms.problems.current.count', min => 0 },
group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
}
];
$self->{maps_counters}->{global} = [];
foreach ('minor', 'warning', 'major', 'critical') {
push @{$self->{maps_counters}->{global}}, {
label => 'severity-' . $_, nlabel => 'alarms.severity.' . $_ . '.count', display_ok => 0, set => {
key_values => [ { name => $_ } ],
output_template => $_ . ': %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
};
}
$self->{maps_counters}->{alarm} = [
{
label => 'status',
type => 2,
warning_default => '%{severity} =~ /minor|warning/i',
critical_default => '%{severity} =~ /major|critical/i',
set => {
key_values => [
{ name => 'name' }, { name => 'hostname' },
{ name => 'severity' }, { name => 'timeraised' }
],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
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-hostname:s' => { name => 'filter_hostname' },
'timezone:s' => { name => 'timezone' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->{option_results}->{timezone} = 'UTC' if (!defined($self->{option_results}->{timezone}) || $self->{option_results}->{timezone} eq '');
}
sub manage_selection {
my ($self, %options) = @_;
$self->{global} = {
minor => 0, warning => 0, major => 0, critical => 0
};
$self->{alarms}->{global} = { alarm => {} };
my $appliances = $options{custom}->request_api(endpoint => '/appliance');
my $post = { ids => [] };
foreach my $appliance (@$appliances) {
next if (defined($self->{option_results}->{filter_hostname}) && $self->{option_results}->{filter_hostname} ne '' &&
$appliance->{hostname} !~ /$self->{option_results}->{filter_hostname}/);
push @{$post->{ids}}, $appliance->{id};
}
my $results = $options{custom}->request_api(
method => 'POST',
endpoint => '/alarm/appliance',
get_param => ['view=active'],
query_form_post => $post
);
foreach my $entry (@$results) {
my $dt = DateTime->from_epoch(epoch => $entry->{timeOccurredInMills} / 1000, time_zone => $self->{option_results}->{timezone});
my $timeraised = sprintf(
'%02d-%02d-%02dT%02d:%02d:%02d (%s)', $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second, $self->{option_results}->{timezone}
);
$self->{global}->{ lc($entry->{severity}) }++;
$self->{alarms}->{global}->{alarm}->{ $entry->{id} } = {
hostname => $entry->{hostName},
name => $entry->{name},
severity => lc($entry->{severity}),
timeraised => $timeraised
};
}
}
1;
__END__
=head1 MODE
Check alarms.
=over 8
=item B<--filter-hostname>
Filter alarms by hostname (can be a regexp).
=item B<--timezone>
Set timezone for creation time (Default is 'UTC').
=item B<--warning-status>
Set warning threshold for status (Default: '%{severity} =~ /minor|warning/i')
Can used special variables like: %{severity}, %{hostname}, %{name}, %{timeraised}
=item B<--critical-status>
Set critical threshold for status (Default: '%{severity} =~ /major|critical/i').
Can used special variables like: %{severity}, %{hostname}, %{name}, %{timeraised}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'severity-minor', 'severity-warning', 'severity-major', 'severity-critical'.
=back
=cut

View File

@ -0,0 +1,191 @@
#
# Copyright 2022 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 network::aruba::orchestrator::restapi::mode::appliances;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub prefix_appliance_output {
my ($self, %options) = @_;
return sprintf(
"appliance '%s' [group: %s] ",
$options{instance_value}->{hostname},
$options{instance_value}->{group}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, skipped_code => { -10 => 1 } },
{ name => 'appliances', type => 1, cb_prefix_output => 'prefix_appliance_output', message_multiple => 'All appliances are ok' }
];
$self->{maps_counters}->{global} = [
{ label => 'appliances-detected', nlabel => 'appliances.detected.count', set => {
key_values => [ { name => 'num_appliances' } ],
output_template => 'appliances detected: %s',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{appliances} = [
{
label => 'status',
type => 2,
unknown_default => '%{state} =~ /unknown|unreachable/i',
warning_default => '%{state} =~ /unsupportedVersion|outOfSynchronization/i',
set => {
key_values => [
{ name => 'state' }, { name => 'hostname' }
],
output_template => "state: %s",
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
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-hostname:s' => { name => 'filter_hostname' },
'filter-group:s' => { name => 'filter_group' }
});
return $self;
}
sub get_groups {
my ($self, %options) = @_;
my $results = $options{custom}->request_api(
endpoint => '/group'
);
my $groups = {};
foreach (@$results) {
$groups->{ $_->{id} } = { name => $_->{name}, parentId => $_->{parentId} };
}
return $groups;
}
sub get_group {
my ($self, %options) = @_;
my @groups = ();
my $groupId = $options{groupId};
while (defined($groupId)) {
unshift(@groups, $options{groups}->{$groupId}->{name});
$groupId = $options{groups}->{$groupId}->{parentId};
}
return join(' > ', @groups);
}
sub manage_selection {
my ($self, %options) = @_;
my $map_state = {
0 => 'unknown', 1 => 'normal', 2 => 'unreachable',
3 => 'unsupportedVersion', 4 => 'outOfSynchronization', 5 => 'synchronizationInProgress'
};
my $appliances = $options{custom}->request_api(endpoint => '/appliance');
my $groups = $self->get_groups(custom => $options{custom});
$self->{global} = { num_appliances => 0 };
$self->{appliances} = {};
foreach my $appliance (@$appliances) {
next if (defined($self->{option_results}->{filter_hostname}) && $self->{option_results}->{filter_hostname} ne '' &&
$appliance->{hostname} !~ /$self->{option_results}->{filter_hostname}/);
my $group = $self->get_group(groups => $groups, groupId => $appliance->{groupId});
next if (defined($self->{option_results}->{filter_group}) && $self->{option_results}->{filter_group} ne '' &&
$group !~ /$self->{option_results}->{filter_group}/);
$self->{global}->{num_appliances}++;
$self->{appliances}->{ $appliance->{uuid} } = {
hostname => $appliance->{hostName},
group => $group,
state => $map_state->{ $appliance->{state} }
};
}
}
1;
__END__
=head1 MODE
Check appliances.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='status'
=item B<--filter-hostname>
Filter appliances by hostname.
=item B<--filter-group>
Filter appliances by group.
=item B<--unknown-status>
Set unknown threshold for status (Default: '%{state} =~ /unknown|unreachable/i').
Can used special variables like: %{state}, %{hostname}
=item B<--warning-status>
Set warning threshold for status (Default: '%{state} =~ /unsupportedVersion|outOfSynchronization/i').
Can used special variables like: %{state}, %{hostname}
=item B<--critical-status>
Set critical threshold for status.
Can used special variables like: %{state}, %{hostname}
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'appliances-detected'.
=back
=cut

View File

@ -0,0 +1,163 @@
#
# Copyright 2022 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 network::aruba::orchestrator::restapi::mode::discovery;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'resource-type:s' => { name => 'resource_type' },
'prettify' => { name => 'prettify' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') {
$self->{option_results}->{resource_type} = 'appliance';
}
if ($self->{option_results}->{resource_type} !~ /^appliance$/) {
$self->{output}->add_option_msg(short_msg => 'unknown resource type');
$self->{output}->option_exit();
}
}
sub get_groups {
my ($self, %options) = @_;
my $results = $options{custom}->request_api(
endpoint => '/group'
);
my $groups = {};
foreach (@$results) {
$groups->{ $_->{id} } = { name => $_->{name}, parentId => $_->{parentId} };
}
return $groups;
}
sub get_group {
my ($self, %options) = @_;
my @groups = ();
my $groupId = $options{groupId};
while (defined($groupId)) {
unshift(@groups, $options{groups}->{$groupId}->{name});
$groupId = $options{groups}->{$groupId}->{parentId};
}
return join(' > ', @groups);
}
sub discovery_appliance {
my ($self, %options) = @_;
my $appliances = $options{custom}->request_api(endpoint => '/appliance');
my $groups = $self->get_groups(custom => $options{custom});
my $map_state = {
0 => 'unknown', 1 => 'normal', 2 => 'unreachable',
3 => 'unsupportedVersion', 4 => 'outOfSynchronization', 5 => 'synchronizationInProgress'
};
my $disco_data = [];
foreach my $appliance (@$appliances) {
my $node = {};
$node->{uuid} = $appliance->{uuid};
$node->{serial} = $appliance->{serial};
$node->{hostname} = $appliance->{hostName};
$node->{site} = $appliance->{site};
$node->{model} = $appliance->{model};
$node->{platform} = $appliance->{platform};
$node->{ip} = $appliance->{ip};
$node->{state} = $map_state->{ $appliance->{state} };
$node->{group} = $self->get_group(groups => $groups, groupId => $appliance->{groupId});
push @$disco_data, $node;
}
return $disco_data;
}
sub run {
my ($self, %options) = @_;
my $disco_stats;
$disco_stats->{start_time} = time();
my $results = [];
if ($self->{option_results}->{resource_type} eq 'appliance') {
$results = $self->discovery_appliance(
custom => $options{custom}
);
}
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = scalar(@$results);
$disco_stats->{results} = $results;
my $encoded_data;
eval {
if (defined($self->{option_results}->{prettify})) {
$encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
} else {
$encoded_data = JSON::XS->new->utf8->encode($disco_stats);
}
};
if ($@) {
$encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
}
$self->{output}->output_add(short_msg => $encoded_data);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Resources discovery.
=over 8
=item B<--resource-type>
Choose the type of resources to discover (Can be: 'appliance').
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2022 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 network::aruba::orchestrator::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->{modes} = {
'alarms' => 'network::aruba::orchestrator::restapi::mode::alarms',
'appliances' => 'network::aruba::orchestrator::restapi::mode::appliances',
'discovery' => 'network::aruba::orchestrator::restapi::mode::discovery'
};
$self->{custom_modes}->{api} = 'network::aruba::orchestrator::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Aruba Orchestrator using Rest API.
=cut