Fix Cisco ACI APIC Rest api

This commit is contained in:
garnier-quentin 2019-05-31 14:34:01 +02:00
parent 88380495e1
commit 6174ecafdd
5 changed files with 132 additions and 129 deletions

View File

@ -18,7 +18,7 @@
# limitations under the License.
#
package network::cisco::fabric::aci::restapi::custom::api;
package network::cisco::aci::apic::restapi::custom::api;
use strict;
use warnings;
@ -44,12 +44,12 @@ sub new {
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'}
'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);
@ -95,20 +95,19 @@ sub check_options {
$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}->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}->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}->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;
}
@ -131,11 +130,9 @@ sub settings {
$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}->add_header(key => 'Cookie', value => 'APIC-Cookie=' . $self->{access_token});
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub refresh_token {
@ -150,8 +147,8 @@ sub refresh_token {
$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
&& $decoded->{imdata}->[0]->{error}->{attributes}->{text} eq 'Token was invalid (Error: Token timeout)') {
return 1
}
return 0
@ -160,7 +157,7 @@ sub refresh_token {
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 $has_cache_file = $options{statefile}->read(statefile => 'cisco_aci_apic_' . 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');
@ -177,28 +174,35 @@ sub get_access_token {
$self->settings();
my $content = $self->{http}->request(method => 'POST', query_form_post => $post_json,
url_path => '/api/aaaLogin.json');
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);
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 get token from API");
$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} . "'");
. $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} };
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);
}
@ -213,7 +217,6 @@ sub request_api {
}
$self->settings();
my $content = $self->{http}->request(%options);
my $decoded;
@ -236,24 +239,21 @@ sub request_api {
sub get_fabric_health {
my ($self, %options) = @_;
my $response = $self->request_api(method => 'GET', url_path => "/api/class/fabricHealthTotal.json");
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");
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");
my $response = $self->request_api(method => 'GET', url_path => '/api/class/fvTenant.json?rsp-subtree-include=health,required');
return $response;
}
@ -263,25 +263,25 @@ __END__
=head1 NAME
Cisco Fabric ACI API Interface
Cisco ACI APIC API Interface
=head1 REST API OPTIONS
Cisco Fabric ACI API Interface
Cisco ACI APIC Interface
=over 8
=item B<--hostname>
IP/FQDN of the Cisco Fabric ACI
IP/FQDN of the Cisco ACI APIC
=item B<--username>
Username to connect to ACI
Username to connect to ACI APIC
=item B<--hostname>
Password to connect to ACI
Password to connect to ACI APIC
=item B<--timeout>

View File

@ -18,37 +18,32 @@
# limitations under the License.
#
package network::cisco::fabric::aci::restapi::mode::fabric;
package network::cisco::aci::apic::restapi::mode::fabric;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
sub custom_health_output {
my ($self, %options) = @_;
my $msg = sprintf("Current: '%d'%% Previous '%d'%%", $self->{result_values}->{current}, $self->{result_values}->{previous});
my $msg = sprintf("health 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);
$self->{output}->perfdata_add(
label => $_,
nlabel => 'fabric.health.' . $_,
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{dn} : undef,
value => $self->{result_values}->{$_},
unit => '%', min => 0, max => 100
);
}
}
@ -56,17 +51,16 @@ 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' },
{ name => 'fabric', type => 1, cb_prefix_output => 'prefix_fabric_output', message_multiple => 'all fabrics are ok' },
];
$self->{maps_counters}->{fabric} = [
{ label => 'health', set => {
key_values => [ { name => 'current' }, { name => 'previous' }, { name => 'dn'} ],
closure_custom_calc => $self->can('custom_health_calc'),
{ label => 'health', threshold => 0, set => {
key_values => [ { name => 'current' }, { name => 'previous' }, { name => 'dn' } ],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_health_output'),
closure_custom_threshold_check => \&catalog_status_threshold,
closure_custom_perfdata => $self->can('custom_health_perfdata')
closure_custom_perfdata => $self->can('custom_health_perfdata'),
}
},
];
@ -75,20 +69,19 @@ sub set_counters {
sub prefix_fabric_output {
my ($self, %options) = @_;
return "Fabric '" . $options{instance_value}->{dn} . "' Health ";
return "Fabric '" . $options{instance_value}->{dn} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"warning-health:s" => { name => 'warning_health' },
"critical-health:s" => { name => 'critical_health' },
});
$options{options}->add_options(arguments => {
'warning-health:s' => { name => 'warning_health' },
'critical-health:s' => { name => 'critical_health' },
});
return $self;
}
@ -107,13 +100,18 @@ sub manage_selection {
my $result_global = $options{custom}->get_fabric_health();
foreach my $object (@{$result_global->{imdata}}) {
my $dn = $object->{fabricHealthTotal}->{attributes}->{dn};
my $dn = $object->{fabricHealthTotal}->{attributes}->{dn};
$self->{fabric}->{$dn} = {
current => $object->{fabricHealthTotal}->{attributes}->{cur},
previous => $object->{fabricHealthTotal}->{attributes}->{prev},
dn => $dn
};
}
if (scalar(keys %{$self->{fabric}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No fabric found (try --debug)");
$self->{output}->option_exit();
}
}
1;
@ -122,6 +120,10 @@ __END__
=head1 MODE
Check fabrics.
=over 8
=item B<--warning-health>
Set warning for the health level

View File

@ -18,7 +18,7 @@
# limitations under the License.
#
package network::cisco::fabric::aci::restapi::mode::node;
package network::cisco::aci::apic::restapi::mode::node;
use base qw(centreon::plugins::templates::counter);
@ -30,32 +30,31 @@ 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' },
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All fabric nodes are ok' },
];
$self->{maps_counters}->{nodes} = [
{ label => 'health-current', set => {
{ label => 'health-current', nlabel => 'node.health.current', set => {
key_values => [ { name => 'current' }, { name => 'dn' } ],
output_template => 'Current: %s %%', output_error_template => "current: %s %%",
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 => {
{ label => 'health-minimum', nlabel => 'node.health.minimum', set => {
key_values => [ { name => 'min' }, { name => 'dn' } ],
output_template => 'Min: %s %%', output_error_template => "min: %s %%",
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 => {
{ label => 'health-average', nlabel => 'node.health.average', set => {
key_values => [ { name => 'avg' }, { name => 'dn' } ],
output_template => 'Average: %s %%', output_error_template => "average %s %%",
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' },
@ -68,12 +67,12 @@ sub set_counters {
sub prefix_nodes_output {
my ($self, %options) = @_;
return "Node '" . $options{instance_value}->{dn} . "' ";
return "Node '" . $options{instance_value}->{dn} . "' health ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$self->{version} = '1.0';
@ -90,22 +89,22 @@ sub manage_selection {
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;
$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} = {
$self->{nodes}->{$node_dn} = {
min => $node->{fabricNodeHealth5min}->{attributes}->{healthMin},
current => $node->{fabricNodeHealth5min}->{attributes}->{healthLast},
avg => $node->{fabricNodeHealth5min}->{attributes}->{healthAvg},
dn => $node_dn
current => $node->{fabricNodeHealth5min}->{attributes}->{healthLast},
avg => $node->{fabricNodeHealth5min}->{attributes}->{healthAvg},
dn => $node_dn
};
}
if (scalar(keys %{$self->{nodes}}}) <= 0) {
if (scalar(keys %{$self->{nodes}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No nodes found (try --debug)");
$self->{output}->option_exit();
}
@ -117,6 +116,10 @@ __END__
=head1 MODE
Check fabric nodes.
=over 8
=item B<--filter-node>
Regexp filter on the pod / node name

View File

@ -18,37 +18,32 @@
# limitations under the License.
#
package network::cisco::fabric::aci::restapi::mode::tenant;
package network::cisco::aci::apic::restapi::mode::tenant;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
sub custom_health_output {
my ($self, %options) = @_;
my $msg = sprintf("Current: '%d'%% Previous '%d'%%", $self->{result_values}->{current}, $self->{result_values}->{previous});
my $msg = sprintf("health 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);
$self->{output}->perfdata_add(
label => $_,
nlabel => 'tenant.health.' . $_,
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{dn} : undef,
value => $self->{result_values}->{$_},
unit => '%', min => 0, max => 100
);
}
}
@ -56,17 +51,16 @@ 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' },
{ 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'),
{ label => 'health', threshold => 0, set => {
key_values => [ { name => 'current' }, { name => 'previous' }, { name => 'dn' } ],
closure_custom_calc => \&catalog_status_calc,
closure_custom_output => $self->can('custom_health_output'),
closure_custom_threshold_check => \&catalog_status_threshold,
closure_custom_perfdata => $self->can('custom_health_perfdata')
closure_custom_perfdata => $self->can('custom_health_perfdata')
}
},
];
@ -75,21 +69,20 @@ sub set_counters {
sub prefix_tenant_output {
my ($self, %options) = @_;
return "Tenant '" . $options{instance_value}->{dn} . "' Health ";
return "Tenant '" . $options{instance_value}->{dn} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
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' },
});
$options{options}->add_options(arguments => {
'filter-tenant:s' => { name => 'filter_tenant' },
'warning-health:s' => { name => 'warning_health' },
'critical-health:s' => { name => 'critical_health' },
});
return $self;
}
@ -109,20 +102,20 @@ sub manage_selection {
my $result = $options{custom}->get_tenant_health();
foreach my $object (@{$result->{imdata}}) {
my $dn = $object->{fvTenant}->{attributes}->{name};
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
};
$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) {
if (scalar(keys %{$self->{tenant}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No tenants found (try --debug)");
$self->{output}->option_exit();
}
@ -134,6 +127,10 @@ __END__
=head1 MODE
Check tenants.
=over 8
=item B<--filter-tenant>
Regexp filter on the tenant name

View File

@ -18,24 +18,25 @@
# limitations under the License.
#
package network::cisco::fabric::aci::restapi::plugin;
package network::cisco::aci::apic::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 );
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',
'node' => 'network::cisco::aci::apic::restapi::mode::node',
'tenant' => 'network::cisco::aci::apic::restapi::mode::tenant',
'fabric' => 'network::cisco::aci::apic::restapi::mode::fabric',
);
$self->{custom_modes}{api} = 'network::cisco::aci::apic::restapi::custom::api';
return $self;
}
@ -45,6 +46,6 @@ __END__
=head1 PLUGIN DESCRIPTION
Check Cisco Fabric ACI
Check Cisco ACI with APIC Rest API
=cut