add cisco fabric aci plugin

This commit is contained in:
Sims24 2019-05-29 15:01:30 +02:00
parent 6c88762bc1
commit cd66bfe378
5 changed files with 780 additions and 0 deletions

View File

@ -0,0 +1,296 @@
#
# Copyright 2019 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::cisco::fabric::aci::restapi::custom::api;
use strict;
use warnings;
use DateTime;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
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 => {
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"hostname:s" => { name => 'hostname' },
"timeout:s" => { name => 'timeout' },
"port:s" => { name => 'port'},
"proto:s" => { name => 'proto'}
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
sub check_options {
my ($self, %options) = @_;
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef;
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
if (!defined($self->{hostname}) || $self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{username}) || $self->{username} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --username option.");
$self->{output}->option_exit();
}
if (!defined($self->{password}) || $self->{password} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --password option.");
$self->{output}->option_exit();
}
$self->{cache}->check_options(option_results => $self->{option_results});
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}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
$self->{option_results}->{unknown_status} = '';
}
sub settings {
my ($self, %options) = @_;
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
if (defined($self->{access_token})) {
$self->{http}->add_header(key => 'Cookie', value => "APIC-Cookie=" . $self->{access_token});
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub refresh_token {
my ($self, %options) = @_;
$self->settings();
my $content = $self->{http}->request(method => 'GET', url_path => '/api/aaaRefresh.json');
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if (defined($decoded->{imdata}->[0]->{error}->{attributes}) && $decoded->{imdata}->[0]->{error}->{attributes}->{code} == 403
&& $decoded->{imdata}->[0]->{error}->{attributes}->{text} eq 'Token was invalid (Error: Token timeout)') {
return 1
}
return 0
}
sub get_access_token {
my ($self, %options) = @_;
my $has_cache_file = $options{statefile}->read(statefile => 'fabric_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{username}));
my $expires_on = $options{statefile}->get(name => 'expires_on');
my $access_token = $options{statefile}->get(name => 'access_token');
my $refresh_timeout = $options{statefile}->get(name => 'refresh_timeout');
my $last_timestamp = $options{statefile}->get(name => 'last_timestamp');
my $renew_token = 0;
if (defined($last_timestamp) && (time() - $last_timestamp > 400) && (time() - $last_timestamp < $refresh_timeout)) {
$renew_token = $self->refresh_token();
}
if ($has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10) || $renew_token == 1) {
my $login = { aaaUser => { attributes => { name => $self->{username}, pwd => $self->{password} } } };
my $post_json = JSON::XS->new->utf8->encode($login);
$self->settings();
my $content = $self->{http}->request(method => 'POST', query_form_post => $post_json,
url_path => '/api/aaaLogin.json');
my $decoded;
$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 get token from API");
$self->{output}->option_exit();
}
if (defined($decoded->{imdata}->[0]->{error}->{attributes})) {
$self->{output}->add_option_msg(short_msg => "Error '" . uc($decoded->{imdata}->[0]->{error}->{attributes}->{code}) . " "
. $decoded->{imdata}->[0]->{error}->{attributes}->{text} . "'");
$self->{output}->option_exit();
}
$access_token = $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token};
my $datas = {last_timestamp => time(),
refresh_timeout => $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{refreshTimeoutSeconds},
access_token => $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{token},
expires_on => $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{creationTime}
+ $decoded->{imdata}->[0]->{aaaLogin}->{attributes}->{maximumLifetimeSeconds} };
$options{statefile}->write(data => $datas);
}
return $access_token;
}
sub request_api {
my ($self, %options) = @_;
if (!defined($self->{access_token})) {
$self->{access_token} = $self->get_access_token(statefile => $self->{cache});
}
$self->settings();
my $content = $self->{http}->request(%options);
my $decoded;
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 json response: $@");
$self->{output}->option_exit();
}
if (defined($decoded->{imdata}->[0]->{error}->{attributes})) {
$self->{output}->add_option_msg(short_msg => "Error '" . uc($decoded->{imdata}->[0]->{error}->{attributes}->{code}) . " " . $decoded->{imdata}->[0]->{error}->{attributes}->{text} . "'");
$self->{output}->option_exit();
}
return $decoded;
}
sub get_fabric_health {
my ($self, %options) = @_;
my $response = $self->request_api(method => 'GET', url_path => "/api/class/fabricHealthTotal.json");
return $response;
}
sub get_node_health_5m {
my ($self, %options) = @_;
my $response = $self->request_api(method => 'GET', url_path => "/api/class/fabricNodeHealth5min.json");
return $response;
}
sub get_tenant_health {
my ($self, %options) = @_;
my $response = $self->request_api(method => 'GET', url_path => "/api/class/fvTenant.json?rsp-subtree-include=health,required");
return $response;
}
1;
__END__
=head1 NAME
Cisco Fabric ACI API Interface
=head1 REST API OPTIONS
Cisco Fabric ACI API Interface
=over 8
=item B<--hostname>
IP/FQDN of the Cisco Fabric ACI
=item B<--username>
Username to connect to ACI
=item B<--hostname>
Password to connect to ACI
=item B<--timeout>
Set timeout in seconds (Default: 10).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,139 @@
#
# Copyright 2019 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::cisco::fabric::aci::restapi::mode::fabric;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
sub custom_health_output {
my ($self, %options) = @_;
my $msg = sprintf("Current: '%d'%% Previous '%d'%%", $self->{result_values}->{current}, $self->{result_values}->{previous});
return $msg;
}
sub custom_health_calc {
my ($self, %options) = @_;
$self->{result_values}->{dn} = $options{new_datas}->{$self->{instance} . '_dn'};
$self->{result_values}->{previous} = $options{new_datas}->{$self->{instance} . '_previous'};
$self->{result_values}->{current} = $options{new_datas}->{$self->{instance} . '_current'};
return 0;
}
sub custom_health_perfdata {
my ($self, %options) = @_;
foreach ('current', 'previous') {
$self->{output}->perfdata_add(label => $_ . '_' . $self->{result_values}->{dn},
value => $self->{result_values}->{$_},
unit => '%', min => 0, max => 100);
}
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'fabric', type => 1, cb_prefix_output => 'prefix_fabric_output',
message_multiple => 'Global health is OK' },
];
$self->{maps_counters}->{fabric} = [
{ label => 'health', set => {
key_values => [ { name => 'current' }, { name => 'previous' }, { name => 'dn'} ],
closure_custom_calc => $self->can('custom_health_calc'),
closure_custom_output => $self->can('custom_health_output'),
closure_custom_threshold_check => \&catalog_status_threshold,
closure_custom_perfdata => $self->can('custom_health_perfdata')
}
},
];
}
sub prefix_fabric_output {
my ($self, %options) = @_;
return "Fabric '" . $options{instance_value}->{dn} . "' Health ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"warning-health:s" => { name => 'warning_health' },
"critical-health:s" => { name => 'critical_health' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_health', 'critical_health']);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{fabric} = {};
my $result_global = $options{custom}->get_fabric_health();
foreach my $object (@{$result_global->{imdata}}) {
my $dn = $object->{fabricHealthTotal}->{attributes}->{dn};
$self->{fabric}->{$dn} = {
current => $object->{fabricHealthTotal}->{attributes}->{cur},
previous => $object->{fabricHealthTotal}->{attributes}->{prev},
dn => $dn
};
}
}
1;
__END__
=head1 MODE
=item B<--warning-health>
Set warning for the health level
Can used special variables like: %{current}, %{previous}.
example: --warning-health='%{previous} < %{current}'
=item B<--critical-health>
Set critical for the health level
Can used special variables like: %{current}, %{previous}.
example: --critical-health='%{current} < 98'
=back
=cut

View File

@ -0,0 +1,140 @@
#
# Copyright 2019 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::cisco::fabric::aci::restapi::mode::node;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output',
message_multiple => 'All nodes are OK' },
];
$self->{maps_counters}->{nodes} = [
{ label => 'health-current', set => {
key_values => [ { name => 'current' }, { name => 'dn' } ],
output_template => 'Current: %s %%', output_error_template => "current: %s %%",
perfdatas => [
{ label => 'health_current', value => 'current_absolute', template => '%d',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'dn_absolute' },
],
}
},
{ label => 'health-minimum', set => {
key_values => [ { name => 'min' }, { name => 'dn' } ],
output_template => 'Min: %s %%', output_error_template => "min: %s %%",
perfdatas => [
{ label => 'health_min', value => 'min_absolute', template => '%d',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'dn_absolute' },
],
}
},
{ label => 'health-average', set => {
key_values => [ { name => 'avg' }, { name => 'dn' } ],
output_template => 'Average: %s %%', output_error_template => "average %s %%",
perfdatas => [
{ label => 'health_avg', value => 'avg_absolute', template => '%d',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'dn_absolute' },
],
}
},
];
}
sub prefix_nodes_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{dn} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments => {});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
$self->{nodes} = {};
my $result_nodes = $options{custom}->get_node_health_5m();
foreach my $node (@{$result_nodes->{imdata}}) {
$node->{fabricNodeHealth5min}->{attributes}->{dn} =~ /^topology\/(.*)\/sys\/CDfabricNodeHealth5min$/;
my $node_dn = $1;
if (defined($self->{option_results}->{filter_node}) && $self->{option_results}->{filter_node} ne '' &&
$node_dn =~ /$self->{option_results}->{filter_node}/) {
$self->{output}->output_add(long_msg => "skipping '" . $node_dn . "': no matching filter node '" . $node . "'", debug => 1);
next;
}
$self->{nodes}->{$node_dn} = {
min => $node->{fabricNodeHealth5min}->{attributes}->{healthMin},
current => $node->{fabricNodeHealth5min}->{attributes}->{healthLast},
avg => $node->{fabricNodeHealth5min}->{attributes}->{healthAvg},
dn => $node_dn
};
}
if (scalar(keys %{$self->{nodes}}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No nodes found (try --debug)");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
=item B<--filter-node>
Regexp filter on the pod / node name
=item B<--warning-*>
Set warning for each health percentage value. Can be :
--warning-health-average=90:
--warning-health-current
--warning-health-minimum
=item B<--critical-*>
Set criticai for each health percentage value. Can be :
--critical-health-average=90:
--critical-health-current=95:
--critical-health-minimum
=back
=cut

View File

@ -0,0 +1,155 @@
#
# Copyright 2019 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::cisco::fabric::aci::restapi::mode::tenant;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
sub custom_health_output {
my ($self, %options) = @_;
my $msg = sprintf("Current: '%d'%% Previous '%d'%%", $self->{result_values}->{current}, $self->{result_values}->{previous});
return $msg;
}
sub custom_health_calc {
my ($self, %options) = @_;
$self->{result_values}->{dn} = $options{new_datas}->{$self->{instance} . '_dn'};
$self->{result_values}->{previous} = $options{new_datas}->{$self->{instance} . '_previous'};
$self->{result_values}->{current} = $options{new_datas}->{$self->{instance} . '_current'};
return 0;
}
sub custom_health_perfdata {
my ($self, %options) = @_;
foreach ('current', 'previous') {
$self->{output}->perfdata_add(label => $_ . '_' . $self->{result_values}->{dn},
value => $self->{result_values}->{$_},
unit => '%', min => 0, max => 100);
}
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'tenant', type => 1, cb_prefix_output => 'prefix_tenant_output',
message_multiple => 'All tenants are OK' },
];
$self->{maps_counters}->{tenant} = [
{ label => 'health', set => {
key_values => [ { name => 'current' }, { name => 'previous' }, { name => 'dn'} ],
closure_custom_calc => $self->can('custom_health_calc'),
closure_custom_output => $self->can('custom_health_output'),
closure_custom_threshold_check => \&catalog_status_threshold,
closure_custom_perfdata => $self->can('custom_health_perfdata')
}
},
];
}
sub prefix_tenant_output {
my ($self, %options) = @_;
return "Tenant '" . $options{instance_value}->{dn} . "' Health ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"filter-tenant" => { name => 'filter_tenant' },
"warning-health:s" => { name => 'warning_health' },
"critical-health:s" => { name => 'critical_health' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_health', 'critical_health']);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{tenant} = {};
my $result = $options{custom}->get_tenant_health();
foreach my $object (@{$result->{imdata}}) {
my $dn = $object->{fvTenant}->{attributes}->{name};
if (defined($self->{option_results}->{filter_tenant}) && $self->{option_results}->{filter_tenant} ne '' &&
$dn =~ /$self->{option_results}->{filter_tenant}/) {
$self->{output}->output_add(long_msg => "skipping '" . $dn . "': no matching filter ", debug => 1);
next;
}
$self->{tenant}->{$dn} = {
current => $object->{fvTenant}->{children}->[0]->{healthInst}->{attributes}->{cur},
previous => $object->{fvTenant}->{children}->[0]->{healthInst}->{attributes}->{prev},
dn => $dn
};
}
if (scalar(keys %{$self->{tenant}}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No tenants found (try --debug)");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
=item B<--filter-tenant>
Regexp filter on the tenant name
=item B<--warning-health>
Set warning for the health level
Can used special variables like: %{current}, %{previous}.
example: --warning-health='%{previous} < %{current}'
=item B<--critical-health>
Set critical for the health level
Can used special variables like: %{current}, %{previous}.
example: --critical-health='%{current} < 98'
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2019 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::cisco::fabric::aci::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} } = (
'node' => 'network::cisco::fabric::aci::restapi::mode::node',
'tenant' => 'network::cisco::fabric::aci::restapi::mode::tenant',
'fabric' => 'network::cisco::fabric::aci::restapi::mode::fabric',
);
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Cisco Fabric ACI
=cut