enh(protocol/ospf): mode neighbor - harden code (#3258)

This commit is contained in:
qgarnier 2021-11-16 14:43:13 +01:00 committed by GitHub
parent 45174c68a8
commit f6bcff4414
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 18 deletions

View File

@ -30,13 +30,13 @@ use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return 'state : ' . $self->{result_values}->{NbrState}; return 'state: ' . $self->{result_values}->{NbrState};
} }
sub custom_change_output { sub custom_change_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return 'Neighbors current : ' . $self->{result_values}->{Total} . ' (last : ' . $self->{result_values}->{TotalLast} . ')'; return 'Neighbors current: ' . $self->{result_values}->{Total} . ' (last: ' . $self->{result_values}->{TotalLast} . ')';
} }
sub custom_change_calc { sub custom_change_calc {
@ -64,7 +64,7 @@ sub set_counters {
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'neighbors.total.count',set => { { label => 'total', nlabel => 'neighbors.total.count',set => {
key_values => [ { name => 'total' } ], key_values => [ { name => 'total' } ],
output_template => 'Total neighbors : %s', output_template => 'Total neighbors: %s',
perfdatas => [ perfdatas => [
{ label => 'total', template => '%s', min => 0 } { label => 'total', template => '%s', min => 0 }
] ]
@ -99,22 +99,18 @@ sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
}); });
return $self; return $self;
} }
my %map_state = ( my %map_state = (
1 => 'down', 1 => 'down', 2 => 'attempt',
2 => 'attempt', 3 => 'init', 4 => 'twoWay',
3 => 'init', 5 => 'exchangeStart', 6 => 'exchange',
4 => 'twoWay', 7 => 'loading', 8 => 'full'
5 => 'exchangeStart',
6 => 'exchange',
7 => 'loading',
8 => 'full'
); );
my $mapping = { my $mapping = {
NbrIpAddr => { oid => '.1.3.6.1.2.1.14.10.1.1' }, NbrIpAddr => { oid => '.1.3.6.1.2.1.14.10.1.1' },
@ -127,24 +123,27 @@ my $oid_ospfNbrEntry = '.1.3.6.1.2.1.14.10.1';
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{nb} = {};
$self->{global} = { total => 0 }; $self->{global} = { total => 0 };
my $snmp_result = $options{snmp}->get_table( my $snmp_result = $options{snmp}->get_table(
oid => $oid_ospfNbrEntry, oid => $oid_ospfNbrEntry,
nothing_quit => 1 nothing_quit => 1
); );
$self->{nb} = {};
foreach my $oid (keys %{$snmp_result}) { foreach my $oid (keys %{$snmp_result}) {
next if ($oid !~ /^$mapping->{NbrState}->{oid}\.(.*)$/); next if ($oid !~ /^$mapping->{NbrState}->{oid}\.(.*)$/);
my $instance = $1; my $instance = $1;
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
$self->{nb}->{$instance} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
if (!defined($self->{nb}->{$instance}->{NbrIpAddr})) {
$instance =~ /^(.*)\.(\d+)$/;
$self->{nb}->{$instance}->{NbrIpAddr} = $1;
}
$self->{global}->{total}++; $self->{global}->{total}++;
$self->{nb}->{$instance} = { %$result };
} }
if (scalar(keys %{$self->{nb}}) <= 0) { if (scalar(keys %{$self->{nb}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No neighbors found."); $self->{output}->add_option_msg(short_msg => 'No neighbors found.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }