Ctor 1452 plugin network fortinet fortigate snmp new modes switch usage and switches discovery (#5514)

Co-authored-by: Roman Morandell <46994680+rmorandell-pgum@users.noreply.github.com>
Co-authored-by: Lucie Dubrunfaut <ldubrunfaut@CNTR-PORT-A198>
This commit is contained in:
sfarouq-ext 2025-03-24 14:33:08 +01:00 committed by GitHub
parent 9274e641d8
commit 9dedf6425a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 2505 additions and 1 deletions

View File

@ -0,0 +1,203 @@
#
# Copyright 2024 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 centreon::common::fortinet::fortigate::snmp::mode::listswitches;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
my $mapping_status = { 0 => 'down', 1 => 'up' };
my $map_admin_connection_state = { 0 => 'discovered', 1 => 'disable', 2 => 'authorized' };
my $mapping = {
serial => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.3' },# fgSwDeviceSerialNum
name => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.4' },# fgSwDeviceName
version => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.5' },# fgSwDeviceVersion
admin => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.6', map => $map_admin_connection_state },# fgSwDeviceAuthorized
state => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.7', map => $mapping_status },# fgSwDeviceStatus
ip => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.9' },# fgSwDeviceIp
};
my $fgSwDeviceEntry = '.1.3.6.1.4.1.12356.101.24.1.1.1';
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(
arguments => {
'filter-name:s' => { name => 'filter_name' },
'filter-status:s' => { name => 'filter_status' },
'filter-admin:s' => { name => 'filter_admin' },
'filter-ip:s' => { name => 'filter_ip' }
}
);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $snmp_result = $options{snmp}->get_table(
oid => $fgSwDeviceEntry,
start => $mapping->{serial},
end => $mapping->{ip},
nothing_quit => 1
);
foreach my $oid ($options{snmp}->oid_lex_sort(sort keys %{$snmp_result})) {
next if ($oid !~ /^$mapping->{serial}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$result->{name} !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(
long_msg => "skipping '" . $result->{name} . "': no matching filter.",
debug => 1
);
next;
}
if (defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status} ne '' &&
$result->{state} !~ /$self->{option_results}->{filter_status}/) {
$self->{output}->output_add(
long_msg => "skipping '" . $result->{state} . "': no matching filter.",
debug => 1
);
next;
}
if (defined($self->{option_results}->{filter_admin}) && $self->{option_results}->{filter_admin} ne '' &&
$result->{admin} !~ /$self->{option_results}->{filter_admin}/) {
$self->{output}->output_add(
long_msg => "skipping '" . $result->{admin} . "': no matching filter.",
debug => 1
);
next;
}
if (defined($self->{option_results}->{filter_ip}) && $self->{option_results}->{filter_ip} ne '' &&
$result->{ip} !~ /$self->{option_results}->{filter_ip}/) {
$self->{output}->output_add(
long_msg => "skipping '" . $result->{ip} . "': no matching filter.",
debug => 1
);
next;
}
push @{$self->{switch}}, $result;
}
}
sub run {
my ($self, %options) = @_;
$self->manage_selection(%options);
if (scalar(keys @{$self->{switch}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No switch found matching.");
$self->{output}->option_exit();
}
foreach (sort @{$self->{switch}}) {
$self->{output}->output_add(
long_msg =>
sprintf(
"[Name = %s] [Serial = %s] [IP = %s] [Version = %s] [State = %s] [Admin = %s]",
$_->{name},
$_->{serial},
$_->{ip},
$_->{version},
$_->{state},
$_->{admin},
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List switches:'
);
$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 => [ 'name', 'serial', 'ip', 'version', 'state', 'admin' ]);
}
sub disco_show {
my ($self, %options) = @_;
$self->manage_selection(%options);
foreach (@{$self->{switch}}) {
$self->{output}->add_disco_entry(
name => $_->{name},
serial => $_->{serial},
ip => $_->{ip},
version => $_->{version},
state => $_->{state},
admin => $_->{admin}
);
}
}
1;
__END__
=head1 MODE
List switches managed through Fortigate Switch Controller.
=over 8
=item B<--filter-name>
Filter switch by name (can be a regexp).
=item B<--filter-status>
Filter switch by status
=item B<--filter-admin>
Filter switch by admin connection state
=item B<--filter-ip>
Filter switch by IP (can be a regexp).
=back
=cut

View File

@ -0,0 +1,252 @@
#
# Copyright 2024 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 centreon::common::fortinet::fortigate::snmp::mode::switchusage;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
use Digest::MD5 qw(md5_hex);
sub custom_status_output {
my ($self, %options) = @_;
return 'status: ' . $self->{result_values}->{status} . ' [admin: ' . $self->{result_values}->{admin} . ']';
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{
name => 'switch',
type => 1,
cb_prefix_output => 'prefix_ap_output',
message_multiple => 'All switches are ok',
skipped_code => { -10 => 1 }
}
];
$self->{maps_counters}->{switch} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'admin' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub {return 0;},
closure_custom_threshold_check => \&catalog_status_threshold
}
},
{ label => 'cpu', nlabel => 'switch.cpu.utilization.percentage', set => {
key_values => [ { name => 'cpu' }, { name => 'display' } ],
output_template => 'cpu usage: %.2f %%',
perfdatas => [
{ label => 'cpu', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
]
}
},
{ label => 'memory', nlabel => 'switch.memory.usage.bytes', set => {
key_values => [ { name => 'memory' }, { name => 'display' } ],
output_template => 'memory usage: %.2f %%',
perfdatas => [
{ label => 'memory', template => '%.2f',
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
]
}
}
];
}
sub prefix_ap_output {
my ($self, %options) = @_;
return "Switch '" . $options{instance_value}->{display} . "' ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class;
$options{options}->add_options(
arguments => {
'filter-name:s' => { name => 'filter_name' },
'filter-ip:s' => { name => 'filter_ip' },
'unknown-status:s' => { name => 'unknown_status', default => '' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => {
name => 'critical_status',
default => '%{admin} eq "authorized" and %{status} eq "down"'
}
}
);
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => [ 'unknown_status', 'warning_status', 'critical_status' ]);
}
my %map_admin_connection_state = (
0 => 'discovered', 1 => 'disable', 2 => 'authorized',
);
my %map_switch_status = (
0 => 'down', 1 => 'up'
);
my $mapping = {
fgSwDeviceAuthorized => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.6', map => \%map_admin_connection_state },
fgSwDeviceName => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.4' },
fgSwDeviceIp => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.9' }
};
my $mapping2 = {
fgSwDeviceStatus => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.7', map => \%map_switch_status },
fgSwCpu => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.11' },
fgSwMemory => { oid => '.1.3.6.1.4.1.12356.101.24.1.1.1.12' }
};
my $oid_fgSwDeviceTable = '.1.3.6.1.4.1.12356.101.24.1.1.1';
sub manage_selection {
my ($self, %options) = @_;
my $snmp_result = $options{snmp}->get_table(
oid => $oid_fgSwDeviceTable,
start => $mapping->{fgSwDeviceName}->{oid},
end => $mapping->{fgSwDeviceIp}->{oid},
nothing_quit => 1
);
$self->{switch} = {};
foreach my $oid (sort keys %{$snmp_result}) {
next if ($oid !~ /^$mapping->{fgSwDeviceName}->{oid}\.(.*)$/);
my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$result->{fgSwDeviceName} !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(
long_msg => "skipping switch '" . $result->{fgSwDeviceName} . "'.",
debug => 1
);
next;
}
if (defined($self->{option_results}->{filter_ip}) && $self->{option_results}->{filter_ip} ne '' &&
$result->{fgSwDeviceIp} !~ /$self->{option_results}->{filter_ip}/) {
$self->{output}->output_add(
long_msg => "skipping switch '" . $result->{fgSwDeviceIp} . "'.",
debug => 1
);
next;
}
$self->{switch}->{$instance} = {
display => $result->{fgSwDeviceName},
admin => $result->{fgSwDeviceAuthorized},
status => 'n/a',
};
}
if (scalar(keys %{$self->{switch}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No switch found.");
$self->{output}->option_exit();
}
$options{snmp}->load(
oids => [
$mapping2->{fgSwDeviceStatus}->{oid},
$mapping2->{fgSwCpu}->{oid},
$mapping2->{fgSwMemory}->{oid},
],
instances => [ keys %{$self->{switch}} ],
instance_regexp => '^(.*)$'
);
$snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
foreach (keys %{$self->{switch}}) {
my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => $_);
$self->{switch}->{$_}->{status} = $result->{fgSwDeviceStatus};
$self->{switch}->{$_}->{cpu} = $result->{fgSwCpu};
$self->{switch}->{$_}->{memory} = $result->{fgSwMemory};
}
$self->{cache_name} = 'fortigate_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ?
md5_hex($self->{option_results}->{filter_counters}) :
md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_name}) ?
md5_hex($self->{option_results}->{filter_name}) :
md5_hex('all'));
}
1;
__END__
=head1 MODE
Check switch usage through Fortigate Switch Controller.
=over 8
=item B<--warning-*>
Warning threshold.
Can be: 'cpu' (%), 'memory' (%).
=item B<--critical-*>
Critical threshold.
Can be: 'cpu' (%), 'memory' (%).
=item B<--filter-name>
Filter by switch name (can be a regexp).
=item B<--filter-IP>
Filter by switch IP (can be a regexp).
=item B<--unknown-status>
Define the conditions to match for the status to be UNKNOWN (default: '').
You can use the following variables: %{admin}, %{status}, %{display}
=item B<--warning-status>
Define the conditions to match for the status to be WARNING (default: '').
You can use the following variables: %{admin}, %{status}, %{display}
=item B<--critical-status>
Define the conditions to match for the status to be CRITICAL (default: '%{admin} eq "authorized" and %{status} eq "down"').
You can use the following variables: %{admin}, %{status}, %{display}
=back
=cut

View File

@ -36,17 +36,19 @@ sub new {
'cpu' => 'centreon::common::fortinet::fortigate::snmp::mode::cpu',
'disk' => 'centreon::common::fortinet::fortigate::snmp::mode::disk',
'hardware' => 'centreon::common::fortinet::fortigate::snmp::mode::hardware',
'interfaces' => 'centreon::common::fortinet::fortigate::snmp::mode::interfaces',
'interfaces' => 'centreon::common::fortinet::fortigate::snmp::mode::interfaces',
'ips-stats' => 'centreon::common::fortinet::fortigate::snmp::mode::ipsstats',
'link-monitor' => 'centreon::common::fortinet::fortigate::snmp::mode::linkmonitor',
'list-interfaces' => 'snmp_standard::mode::listinterfaces',
'list-link-monitors' => 'centreon::common::fortinet::fortigate::snmp::mode::listlinkmonitors',
'list-virtualdomains' => 'centreon::common::fortinet::fortigate::snmp::mode::listvirtualdomains',
'list-switches' => 'centreon::common::fortinet::fortigate::snmp::mode::listswitches',
'memory' => 'centreon::common::fortinet::fortigate::snmp::mode::memory',
'sessions' => 'centreon::common::fortinet::fortigate::snmp::mode::sessions',
'signatures' => 'centreon::common::fortinet::fortigate::snmp::mode::signatures',
'uptime' => 'snmp_standard::mode::uptime',
'sdwan' => 'centreon::common::fortinet::fortigate::snmp::mode::sdwan',
'switch-usage' => 'centreon::common::fortinet::fortigate::snmp::mode::switchusage',
'vdom-usage' => 'centreon::common::fortinet::fortigate::snmp::mode::vdomusage',
'virus' => 'centreon::common::fortinet::fortigate::snmp::mode::virus',
'vpn' => 'centreon::common::fortinet::fortigate::snmp::mode::vpn'

View File

@ -0,0 +1,31 @@
*** Settings ***
Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resource
Suite Setup Ctn Generic Suite Setup
Test Timeout 120s
*** Variables ***
${CMD} ${CENTREON_PLUGINS} --plugin=network::fortinet::fortigate::snmp::plugin
*** Test Cases ***
list-switches ${tc}
[Tags] network list-switches
${command} Catenate
... ${CMD}
... --mode=list-switches
... --hostname=${HOSTNAME}
... --snmp-version=${SNMPVERSION}
... --snmp-port=${SNMPPORT}
... --snmp-community=network/fortinet/fortigate/snmp/slim_fortigate-switches
... ${extra_options}
Ctn Run Command And Check Result As Strings ${command} ${expected_result}
Examples: tc extra_options expected_result --
... 1 ${EMPTY} List switches: [Name = Anonymized 188] [Serial = Anonymized 209] [IP = 10.255.1.2] [Version = Anonymized 103] [State = up] [Admin = authorized] [Name = Anonymized 152] [Serial = Anonymized 146] [IP = 10.255.1.3] [Version = Anonymized 056] [State = up] [Admin = authorized]
... 2 --filter-name='Anonymized 188' List switches: [Name = Anonymized 188] [Serial = Anonymized 209] [IP = 10.255.1.2] [Version = Anonymized 103] [State = up] [Admin = authorized]
... 3 --filter-status='up' List switches: [Name = Anonymized 188] [Serial = Anonymized 209] [IP = 10.255.1.2] [Version = Anonymized 103] [State = up] [Admin = authorized] [Name = Anonymized 152] [Serial = Anonymized 146] [IP = 10.255.1.3] [Version = Anonymized 056] [State = up] [Admin = authorized]
... 4 --filter-admin='toto' UNKNOWN: No switch found matching.
... 5 --filter-ip='10.255.1.3' List switches: [Name = Anonymized 152] [Serial = Anonymized 146] [IP = 10.255.1.3] [Version = Anonymized 056] [State = up] [Admin = authorized]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,36 @@
*** Settings ***
Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resource
Suite Setup Ctn Generic Suite Setup
Test Timeout 120s
*** Variables ***
${CMD} ${CENTREON_PLUGINS} --plugin=network::fortinet::fortigate::snmp::plugin
*** Test Cases ***
switch-usage ${tc}
[Tags] network switch-usage
${command} Catenate
... ${CMD}
... --mode=switch-usage
... --hostname=${HOSTNAME}
... --snmp-version=${SNMPVERSION}
... --snmp-port=${SNMPPORT}
... --snmp-community=network/fortinet/fortigate/snmp/slim_fortigate-switches
... ${extra_options}
Ctn Run Command And Check Result As Strings ${command} ${expected_result}
Examples: tc extra_options expected_result --
... 1 ${EMPTY} OK: All switches are ok | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 2 --warning-cpu=10 WARNING: Switch 'Anonymized 188' cpu usage: 21.00 % | 'cpu_Anonymized 188'=21.00%;0:10;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;0:10;;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 3 --critical-cpu=10 CRITICAL: Switch 'Anonymized 188' cpu usage: 21.00 % | 'cpu_Anonymized 188'=21.00%;;0:10;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;0:10;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 4 --warning-memory=10 WARNING: Switch 'Anonymized 188' memory usage: 38.00 % - Switch 'Anonymized 152' memory usage: 65.00 % | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;0:10;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;0:10;;0;100
... 5 --critical-memory=10 CRITICAL: Switch 'Anonymized 188' memory usage: 38.00 % - Switch 'Anonymized 152' memory usage: 65.00 % | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;0:10;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;0:10;0;100
... 6 --filter-name='Anonymized 188' OK: Switch 'Anonymized 188' status: up [admin: authorized], cpu usage: 21.00 %, memory usage: 38.00 % | 'cpu'=21.00%;;;0;100 'memory'=38.00%;;;0;100
... 7 --filter-ip='' OK: All switches are ok | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 8 --unknown-status='\\\%{admin} eq "authorized"' UNKNOWN: Switch 'Anonymized 188' status: up [admin: authorized] - Switch 'Anonymized 152' status: up [admin: authorized] | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 9 --warning-status='\\\%{status} eq "up"' WARNING: Switch 'Anonymized 188' status: up [admin: authorized] - Switch 'Anonymized 152' status: up [admin: authorized] | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;;0;100
... 10 --critical-status='\\\%{status} ne "down"' CRITICAL: Switch 'Anonymized 188' status: up [admin: authorized] - Switch 'Anonymized 152' status: up [admin: authorized] | 'cpu_Anonymized 188'=21.00%;;;0;100 'memory_Anonymized 188'=38.00%;;;0;100 'cpu_Anonymized 152'=7.00%;;;0;100 'memory_Anonymized 152'=65.00%;;;0;100