(plugin) apps::ipfabric - initial release (#3571)

This commit is contained in:
lchrdn 2022-03-29 18:30:02 +02:00 committed by GitHub
parent 9014b5abf3
commit 79ebd94c65
4 changed files with 645 additions and 0 deletions

View File

@ -0,0 +1,184 @@
#
# 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 apps::ipfabric::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{noptions})) {
$options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' },
'url-path:s' => { name => 'url_path' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'api-key:s' => { name => 'api_key' },
'timeout:s' => { name => 'timeout' }
});
}
$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} : 443;
$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} : '/api/v1/tables';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{api_key} = (defined($self->{option_results}->{api_key})) ? $self->{option_results}->{api_key} : '';
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->{api_key}) || $self->{api_key} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-key option.");
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{curl_opt})) {
$self->{option_results}->{curl_opt} = ['CURLOPT_POSTREDIR => CURL_REDIR_POST_ALL'];
$self->{curl_opt} = 'CURLOPT_POSTREDIR => CURL_REDIR_POST_ALL';
}
$self->{http}->set_options(%{$self->{option_results}});
return 0;
}
sub settings {
my ($self, %options) = @_;
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$self->{http}->add_header(key => 'X-API-Token', value => $self->{api_key});
$self->{http}->set_options(%{$self->{option_results}});
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
my $encoded_form_post;
if (defined($options{query_form_post})) {
eval {
$encoded_form_post = JSON::XS->new->utf8->encode($options{query_form_post});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot encode json request");
$self->{output}->option_exit();
}
}
my ($content) = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . $options{endpoint},
query_form_post => $encoded_form_post,
);
my $decoded = $self->json_decode(content => $content);
if (!defined($decoded)) {
$self->{output}->add_option_msg(short_msg => 'error while retrieving data (add --debug option for detailed message)');
$self->{output}->option_exit();
}
return $decoded;
}
sub json_decode {
my ($self, %options) = @_;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($options{content});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
1;
__END__
=head1 NAME
IP Fabric API module.
=head1 REST API OPTIONS
IP Fabric API module.
=over 8
=item B<--hostname>
Set hostname, it is mandatory.
=item B<--port>
Port used (Default: 443)
=item B<--proto>
Specify http if needed (Default: 'https')
=item B<--api-key>
Set API key to request IP Fabric API.
=item B<--timeout>
Set timeout in seconds (Default: 10).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,158 @@
#
# 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 apps::ipfabric::mode::discovery;
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 => {
"prettify" => { name => 'prettify' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $disco_data = {};
my $disco_stats;
$disco_stats->{start_time} = time();
my $disco_raw_form_post = {
"columns" => [
"id",
"hostname",
"siteName",
"loginIp",
"vendor",
"family",
"platform"
],
"filters" => {},
"pagination" => {
"limit" => undef,
"start" => 0
},
"snapshot" => "\$last",
"reports" => "/inventory/devices"
};
my $disco_api_results = $options{custom}->request_api(
method => 'POST',
endpoint => '/inventory/devices',
query_form_post => $disco_raw_form_post
);
foreach my $device (@{$disco_api_results->{data}}) {
$disco_data->{$device->{id}} = {
id => $device->{id},
hostname => $device->{hostname},
siteName => $device->{siteName},
vendor => $device->{vendor},
family => $device->{family},
snmp_community => undef
};
}
my $snmp_req_data_raw = {
"columns" => [
"id",
"hostname",
"name"
],
"filters" => {},
"pagination" => {
"limit" => undef,
"start" => 0
},
"snapshot" => "\$last",
"reports" => "/technology/management/snmp/communities"
};
my $snmp_community_api_results = $options{custom}->request_api(
endpoint => '/management/snmp/communities',
query_form_post => $snmp_req_data_raw
);
foreach my $snmp_device (@{$snmp_community_api_results->{data}}) {
foreach my $id (keys %$disco_data) {
if ($snmp_device->{hostname} eq $disco_data->{$id}->{hostname}){
$disco_data->{$id}->{snmp_community} = $snmp_device->{name}->{data};
}
}
}
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = $disco_data;
$disco_stats->{results} = $disco_data;
return $disco_stats;
}
sub run {
my ($self, %options) = @_;
my $encoded_data;
eval {
if (defined($self->{option_results}->{prettify})) {
$encoded_data = JSON::XS->new->utf8->pretty->encode($self->manage_selection(%options));
} else {
$encoded_data = JSON::XS->new->utf8->encode($self->manage_selection(%options));
}
};
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
IP Fabric devices discovery.
=over 8
=back
=cut

View File

@ -0,0 +1,253 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infraspathture 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::ipfabric::mode::pathverification;
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(
"%s:%s_%s:%s [State: %s], [Expected State: %s]",
$self->{result_values}->{src_ip},
$self->{result_values}->{src_port},
$self->{result_values}->{dest_ip},
$self->{result_values}->{dest_port},
$self->{result_values}->{state},
$self->{result_values}->{expected_state}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0,cb_prefix_output => undef, cb_init => undef },
{ name => 'status', type => 1, cb_prefix_output => 'prefix_status_output', message_multiple => 'All paths are OK. ' }
];
$self->{maps_counters}->{global} = [
{ label => 'total-path', nlabel => 'total.path.count', set => {
key_values => [ { name => 'total_path' }],
output_template => 'Total number of paths: %s',
perfdatas => [
{ label => 'total_path', template => '%s', min => 0 }
]
}
},
{ label => 'total-mismatch', nlabel => 'total.path.mismatch.count', set => {
key_values => [ { name => 'total_mismatch' } ],
output_template => 'Total mismatch: %s',
perfdatas => [
{ label => 'total_mismatch', template => '%s', min => 0 }
]
}
},
{ label => 'all-path', nlabel => 'total.path.all.count', set => {
key_values => [ { name => 'all_path' } ],
output_template => 'Number of paths in All state: %s',
perfdatas => [
{ label => 'all_path', template => '%s', min => 0 }
]
}
},
{ label => 'part-path', nlabel => 'total.path.part.count', set => {
key_values => [ { name => 'part_path' } ],
output_template => 'Number of paths in Part state: %s',
perfdatas => [
{ label => 'part_path', template => '%s', min => 0 }
]
}
},
{ label => 'none-path', nlabel => 'total.path.none.count', set => {
key_values => [ { name => 'none_path' } ],
output_template => 'Number of paths in None state: %s',
perfdatas => [
{ label => 'none_path', template => '%s', min => 0 }
]
}
},
{ label => 'error-path', nlabel => 'total.path.error.count', set => {
key_values => [ { name => 'error_path' } ],
output_template => 'Number of paths in Error state: %s',
perfdatas => [
{ label => 'error_path', template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{status} = [
{
label => 'status',
type => 2,
critical_default => '%{expected_state} ne %{state}',
set => {
key_values => [
{ name => 'src_ip' },
{ name => 'src_port' }, { name => 'dest_ip' },
{ name => 'dest_port' }, { name => 'state' },
{ name => 'expected_state' }
],
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 => {
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $path_state;
$self->{global}->{all_path} = 0;
$self->{global}->{error_path} = 0;
$self->{global}->{none_path} = 0;
$self->{global}->{part_path} = 0;
$self->{global}->{total_mismatch} = 0;
my $path_raw_form_post = {
"columns" => [
"id",
"src",
"srcPorts",
"dst",
"dstPorts",
"expectedPassingTraffic",
"passingTraffic"
],
"filters" => {},
"pagination" => {
"limit" => undef,
"start" => 0
},
"snapshot" => "\$last",
"reports" => "/technology/routing/path-verifications"
};
my $path_state_results = $options{custom}->request_api(
method => 'POST',
endpoint => '/networks/path-lookup-checks',
query_form_post => $path_raw_form_post
);
foreach my $route (@{$path_state_results->{data}}) {
$path_state->{$route->{id}} = {
id => $route->{id},
dest_ip => $route->{dst},
dest_port => $route->{dstPorts},
expected_state => $route->{expectedPassingTraffic},
src_ip => $route->{src},
src_port => $route->{srcPorts},
state => $route->{passingTraffic}->{data}
};
if ($path_state->{$route->{id}}->{expected_state} ne $path_state->{$route->{id}}->{state}){
$self->{global}->{total_mismatch}++;
}
}
foreach my $id (keys %$path_state) {
my $dest_port = (defined($path_state->{$id}->{dest_port})) ? $path_state->{$id}->{dest_port} : 'empty';
my $src_port = (defined($path_state->{$id}->{src_port})) ? $path_state->{$id}->{src_port} : 'empty';
my $instance = $path_state->{$id}->{src_ip} . ":" . $src_port . "_" . $path_state->{$id}->{dest_ip} . ":" . $dest_port;
$self->{status}->{$instance} = {
dest_ip => $path_state->{$id}->{dest_ip},
dest_port => $dest_port,
expected_state => $path_state->{$id}->{expected_state},
src_ip => $path_state->{$id}->{src_ip},
src_port => $src_port,
state => $path_state->{$id}->{state}
};
$self->{global}->{total_path}++;
if ($path_state->{$id}->{state} eq "none"){
$self->{global}->{none_path}++;
}
if ($path_state->{$id}->{state} eq "all"){
$self->{global}->{all_path}++;
}
if ($path_state->{$id}->{state} eq "part"){
$self->{global}->{part_path}++;
}
if ($path_state->{$id}->{state} eq "error"){
$self->{global}->{error_path}++;
}
}
}
1;
__END__
=head1 MODE
Check end-to-end path's result against predefined expected state in IP Fabric.
=over 8
=item B<--warning-status>
Set warning threshold for status. (Default: '').
Can use special variables like: %{state}, %{expected_state}
For example, if you want a warning alert when the path state is in 'error' then
the option would be:
--warning-status="%{state} eq 'all'"
=item B<--critical-status>
Set critical threshold for status. (Default: '%{expected_state} ne %{state}').
Can use special variables like: %{state}, %{expected_state}
For example, if you want a critical alert when the path state is in 'error' then
the option would be:
--critical-status="%{state} eq 'all'"
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'total-path', 'total-mismatch',
'error-path', 'none-path', 'part-path',
'all-path'
=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 apps::ipfabric::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} = {
'discovery' => 'apps::ipfabric::mode::discovery',
'path-verification' => 'apps::ipfabric::mode::pathverification'
};
$self->{custom_modes}->{api} = 'apps::ipfabric::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check network devices through IP Fabric API.
=cut