(plugin) apps::ipfabric - add list-paths mode and clean code (#4482)

This commit is contained in:
qgarnier 2023-06-16 16:59:45 +02:00 committed by GitHub
parent 17979bb7c0
commit 4b73ff2bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 243 additions and 114 deletions

View File

@ -44,7 +44,7 @@ sub new {
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output}; $self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
return $self; return $self;
} }
@ -60,20 +60,20 @@ sub set_defaults {}
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; $self->{option_results}->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; $self->{option_results}->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; $self->{option_results}->{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->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v6.2/tables';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; $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} : ''; $self->{api_key} = (defined($self->{option_results}->{api_key})) ? $self->{option_results}->{api_key} : '';
$self->{snapshot_id} = (defined($self->{option_results}->{snapshot_id})) ? $self->{option_results}->{snapshot_id} : "\$last"; $self->{snapshot_id} = (defined($self->{option_results}->{snapshot_id})) ? $self->{option_results}->{snapshot_id} : '$last';
if (!defined($self->{hostname}) || $self->{hostname} eq '') { if ($self->{option_results}->{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(); $self->{output}->option_exit();
} }
if (!defined($self->{api_key}) || $self->{api_key} eq '') { if ($self->{api_key} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-key option."); $self->{output}->add_option_msg(short_msg => "Need to specify --api-key option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
@ -83,8 +83,6 @@ sub check_options {
$self->{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; return 0;
} }
@ -94,7 +92,6 @@ sub settings {
$self->{http}->add_header(key => 'Content-Type', value => 'application/json'); $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
$self->{http}->add_header(key => 'X-API-Token', value => $self->{api_key}); $self->{http}->add_header(key => 'X-API-Token', value => $self->{api_key});
$self->{http}->set_options(%{$self->{option_results}}); $self->{http}->set_options(%{$self->{option_results}});
} }
sub request_api { sub request_api {
@ -117,7 +114,7 @@ sub request_api {
my ($content) = $self->{http}->request( my ($content) = $self->{http}->request(
method => 'POST', method => 'POST',
url_path => $self->{url_path} . $options{endpoint}, url_path => $self->{url_path} . $options{endpoint},
query_form_post => $encoded_form_post, query_form_post => $encoded_form_post
); );
my $decoded = $self->json_decode(content => $content); my $decoded = $self->json_decode(content => $content);

View File

@ -31,7 +31,7 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"prettify" => { name => 'prettify' }, 'prettify' => { name => 'prettify' }
}); });
return $self; return $self;
@ -88,17 +88,17 @@ sub manage_selection {
} }
my $snmp_req_data_raw = { my $snmp_req_data_raw = {
"columns" => [ columns => [
"id", 'id',
"hostname", 'hostname',
"name" 'name'
], ],
"filters" => {}, filters => {},
"pagination" => { pagination => {
"limit" => undef, limit => undef,
"start" => 0 start => 0
}, },
"reports" => "/technology/management/snmp/communities" reports => '/technology/management/snmp/communities'
}; };
my $snmp_community_api_results = $options{custom}->request_api( my $snmp_community_api_results = $options{custom}->request_api(

View File

@ -0,0 +1,126 @@
#
# Copyright 2023 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::listpaths;
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 => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
my $mapping = ['id', 'src', 'srcPorts', 'dst', 'dstPorts', 'protocol'];
sub manage_selection {
my ($self, %options) = @_;
my $path_raw_form_post = {
columns => [
"id",
"src",
"srcPorts",
"dst",
"dstPorts",
"protocol"
],
filters => {},
pagination => {
limit => undef,
start => 0
},
reports => "/technology/routing/path-verifications"
};
my $paths = $options{custom}->request_api(
method => 'POST',
endpoint => '/networks/path-lookup-checks',
query_form_post => $path_raw_form_post
);
my $results = [];
foreach my $path (@{$paths->{data}}) {
$path->{dstPorts} = (defined($path->{dstPorts})) ? $path->{dstPorts} : '-';
$path->{srcPorts} = (defined($path->{srcPorts})) ? $path->{srcPorts} : '-';
push @$results, $path;
}
return $results;
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(custom => $options{custom});
foreach my $entry (@$results) {
$self->{output}->output_add(long_msg =>
join('', map("[$_: " . $entry->{$_} . ']', @$mapping))
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List paths:'
);
$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 => $mapping);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(custom => $options{custom});
foreach my $entry (@$results) {
$self->{output}->add_disco_entry(%$entry);
}
}
1;
__END__
=head1 MODE
List GRE tunnels.
=over 8
=back
=cut

View File

@ -30,11 +30,10 @@ sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return sprintf( return sprintf(
"%s:%s_%s:%s [State: %s], [Expected State: %s]", "source %s destination %s [protocol: %s] state: %s [expected state: %s]",
$self->{result_values}->{src_ip}, $self->{result_values}->{src_ip} . ($self->{result_values}->{src_port} ne '' ? ':' . $self->{result_values}->{src_port} : ''),
$self->{result_values}->{src_port}, $self->{result_values}->{dst_ip} . ($self->{result_values}->{dst_port} ne '' ? ':' . $self->{result_values}->{dst_port} : ''),
$self->{result_values}->{dest_ip}, $self->{result_values}->{protocol},
$self->{result_values}->{dest_port},
$self->{result_values}->{state}, $self->{result_values}->{state},
$self->{result_values}->{expected_state} $self->{result_values}->{expected_state}
); );
@ -44,72 +43,72 @@ sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'global', type => 0,cb_prefix_output => undef, cb_init => undef }, { name => 'global', type => 0 },
{ name => 'status', type => 1, cb_prefix_output => 'prefix_status_output', message_multiple => 'All paths are OK. ' } { name => 'paths', type => 1, message_multiple => 'All paths are ok' }
]; ];
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'total-path', nlabel => 'total.path.count', set => { { label => 'paths-detected', nlabel => 'paths.detected.count', set => {
key_values => [ { name => 'total_path' }], key_values => [ { name => 'detected' }],
output_template => 'Total number of paths: %s', output_template => 'Number of paths detected: %s',
perfdatas => [ perfdatas => [
{ label => 'total_path', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
}, },
{ label => 'total-mismatch', nlabel => 'total.path.mismatch.count', set => { { label => 'paths-mismatch', nlabel => 'paths.mismatch.count', set => {
key_values => [ { name => 'total_mismatch' } ], key_values => [ { name => 'total_mismatch' } ],
output_template => 'Total mismatch: %s', output_template => 'mismatch: %s',
perfdatas => [ perfdatas => [
{ label => 'total_mismatch', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
}, },
{ label => 'all-path', nlabel => 'total.path.all.count', set => { { label => 'paths-state-all', nlabel => 'paths.state.all.count', set => {
key_values => [ { name => 'all_path' } ], key_values => [ { name => 'all_path' } ],
output_template => 'Number of paths in All state: %s', output_template => 'all state: %s',
perfdatas => [ perfdatas => [
{ label => 'all_path', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
}, },
{ label => 'part-path', nlabel => 'total.path.part.count', set => { { label => 'paths-state-part', nlabel => 'paths.state.part.count', set => {
key_values => [ { name => 'part_path' } ], key_values => [ { name => 'part_path' } ],
output_template => 'Number of paths in Part state: %s', output_template => 'part state: %s',
perfdatas => [ perfdatas => [
{ label => 'part_path', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
}, },
{ label => 'none-path', nlabel => 'total.path.none.count', set => { { label => 'paths-state-none', nlabel => 'paths.state.none.count', set => {
key_values => [ { name => 'none_path' } ], key_values => [ { name => 'none_path' } ],
output_template => 'Number of paths in None state: %s', output_template => 'none state: %s',
perfdatas => [ perfdatas => [
{ label => 'none_path', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
}, },
{ label => 'error-path', nlabel => 'total.path.error.count', set => { { label => 'paths-state-error', nlabel => 'paths.state.error.count', set => {
key_values => [ { name => 'error_path' } ], key_values => [ { name => 'error_path' } ],
output_template => 'Number of paths in Error state: %s', output_template => 'error state: %s',
perfdatas => [ perfdatas => [
{ label => 'error_path', template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
} }
]; ];
$self->{maps_counters}->{status} = [ $self->{maps_counters}->{paths} = [
{ {
label => 'status', label => 'status',
type => 2, type => 2,
critical_default => '%{expected_state} ne %{state}', critical_default => '%{expected_state} ne %{state}',
set => { set => {
key_values => [ key_values => [
{ name => 'src_ip' }, { name => 'src_ip' }, { name => 'src_port' },
{ name => 'src_port' }, { name => 'dest_ip' }, { name => 'dst_ip' }, { name => 'dst_port' },
{ name => 'dest_port' }, { name => 'state' }, { name => 'protocol' },
{ name => 'expected_state' } { name => 'state' }, { name => 'expected_state' }
], ],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
@ -125,6 +124,10 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'filter-src-ip:s' => { name => 'filter_src_ip' },
'filter-src-port:s' => { name => 'filter_src_port' },
'filter-dst-ip:s' => { name => 'filter_dst_ip' },
'filter-dst-port:s' => { name => 'filter_dst_port' }
}); });
return $self; return $self;
@ -133,30 +136,23 @@ sub new {
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; 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 = { my $path_raw_form_post = {
"columns" => [ columns => [
"id", "id",
"src", "src",
"srcPorts", "srcPorts",
"dst", "dst",
"dstPorts", "dstPorts",
"protocol",
"expectedPassingTraffic", "expectedPassingTraffic",
"passingTraffic" "passingTraffic"
], ],
"filters" => {}, filters => {},
"pagination" => { pagination => {
"limit" => undef, limit => undef,
"start" => 0 start => 0
}, },
"reports" => "/technology/routing/path-verifications" reports => "/technology/routing/path-verifications"
}; };
my $path_state_results = $options{custom}->request_api( my $path_state_results = $options{custom}->request_api(
@ -165,56 +161,50 @@ sub manage_selection {
query_form_post => $path_raw_form_post query_form_post => $path_raw_form_post
); );
$self->{global} = { detected => 0, all_path => 0, error_path => 0, none_path => 0, part_path => 0, total_mismatch => 0 };
my $path_state = {};
$self->{paths} = {};
foreach my $route (@{$path_state_results->{data}}) { foreach my $route (@{$path_state_results->{data}}) {
$path_state->{$route->{id}} = { my $dst_port = (defined($route->{dstPorts})) ? $route->{dstPorts} : '-';
id => $route->{id}, my $src_port = (defined($route->{srcPorts})) ? $route->{srcPorts} : '-';
dest_ip => $route->{dst},
dest_port => $route->{dstPorts}, next if (defined($self->{option_results}->{filter_src_ip}) && $self->{option_results}->{filter_src_ip} ne '' &&
$route->{src} !~ /$self->{option_results}->{filter_src_ip}/);
next if (defined($self->{option_results}->{filter_src_port}) && $self->{option_results}->{filter_src_port} ne '' &&
$src_port !~ /$self->{option_results}->{filter_src_port}/);
next if (defined($self->{option_results}->{filter_dst_ip}) && $self->{option_results}->{filter_dst_ip} ne '' &&
$route->{dst} !~ /$self->{option_results}->{filter_dst_ip}/);
next if (defined($self->{option_results}->{filter_dst_port}) && $self->{option_results}->{filter_dst_port} ne '' &&
$dst_port !~ /$self->{option_results}->{filter_dst_port}/);
$self->{paths}->{ $route->{id} } = {
dst_ip => $route->{dst},
dst_port => $dst_port,
expected_state => $route->{expectedPassingTraffic}, expected_state => $route->{expectedPassingTraffic},
src_ip => $route->{src}, src_ip => $route->{src},
src_port => $route->{srcPorts}, src_port => $src_port,
protocol => $route->{protocol},
state => $route->{passingTraffic}->{data} state => $route->{passingTraffic}->{data}
}; };
if ($path_state->{$route->{id}}->{expected_state} ne $path_state->{$route->{id}}->{state}){
$self->{global}->{detected}++;
if ($route->{passingTraffic}->{data} eq 'none') {
$self->{global}->{none_path}++;
}
if ($route->{passingTraffic}->{data} eq 'all') {
$self->{global}->{all_path}++;
}
if ($route->{passingTraffic}->{data} eq 'part') {
$self->{global}->{part_path}++;
}
if ($route->{passingTraffic}->{data} eq 'error') {
$self->{global}->{error_path}++;
}
if ($route->{expectedPassingTraffic} ne $route->{passingTraffic}->{data}) {
$self->{global}->{total_mismatch}++; $self->{global}->{total_mismatch}++;
} }
} }
if (scalar(keys %$path_state) <= 0) {
$self->{output}->add_option_msg(short_msg => "No path found.");
$self->{output}->option_exit();
}
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; 1;
@ -227,9 +217,25 @@ Check end-to-end path's result against predefined expected state in IP Fabric.
=over 8 =over 8
=item B<--filter-src-ip>
Filter paths by source ip (regexp can be used).
=item B<--filter-src-port>
Filter paths by source port (regexp can be used).
=item B<--filter-dst-ip>
Filter paths by destionation ip (regexp can be used).
=item B<--filter-dst-port>
Filter paths by destionation port (regexp can be used).
=item B<--warning-status> =item B<--warning-status>
Set warning threshold for status. (Default: ''). Set warning threshold for status.
Can use special variables like: %{state}, %{expected_state} Can use special variables like: %{state}, %{expected_state}
For example, if you want a warning alert when the path state is in 'error' then For example, if you want a warning alert when the path state is in 'error' then
@ -248,9 +254,8 @@ the option would be:
=item B<--warning-*> B<--critical-*> =item B<--warning-*> B<--critical-*>
Thresholds. Thresholds.
Can be: 'total-path', 'total-mismatch', Can be: 'paths-detected', 'paths-mismatch', 'paths-state-all',
'error-path', 'none-path', 'part-path', 'paths-state-part', 'paths-state-none', 'paths-state-error'.
'all-path'
=back =back

View File

@ -31,6 +31,7 @@ sub new {
$self->{modes} = { $self->{modes} = {
'discovery' => 'apps::ipfabric::mode::discovery', 'discovery' => 'apps::ipfabric::mode::discovery',
'list-paths' => 'apps::ipfabric::mode::listpaths',
'path-verification' => 'apps::ipfabric::mode::pathverification' 'path-verification' => 'apps::ipfabric::mode::pathverification'
}; };