From e0513b160829701f223408704232dd7d14a167de Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 14 Apr 2021 14:19:21 +0200 Subject: [PATCH] Fix dhcp snmp disable (#2706) --- apps/microsoft/dhcp/snmp/mode/listsubnets.pm | 33 +++++++++------ apps/microsoft/dhcp/snmp/mode/subnets.pm | 44 +++++++++++++++++++- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/apps/microsoft/dhcp/snmp/mode/listsubnets.pm b/apps/microsoft/dhcp/snmp/mode/listsubnets.pm index 3cf7502ef..5448ccf05 100644 --- a/apps/microsoft/dhcp/snmp/mode/listsubnets.pm +++ b/apps/microsoft/dhcp/snmp/mode/listsubnets.pm @@ -41,22 +41,30 @@ sub check_options { $self->SUPER::init(%options); } -my $oid_subnetAdd = '.1.3.6.1.4.1.311.1.3.2.1.1.1'; +my $mapping = { + address => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.1' }, # subnetAdd + used => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.2' }, # noAddInUse + free => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.3' }, # noAddFree + pending_offers => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.4' } # noPendingOffers +}; +my $oid_scope_table = '.1.3.6.1.4.1.311.1.3.2.1.1'; sub manage_selection { my ($self, %options) = @_; - my $snmp_result = $options{snmp}->get_table( - oid => $oid_subnetAdd, - nothing_quit => 1 - ); + my $snmp_result = $options{snmp}->get_table(oid => $oid_scope_table); my $results = {}; foreach (keys %$snmp_result) { - /^$oid_subnetAdd\.(.*)$/; + next if (! /^$mapping->{address}->{oid}\.(.*)$/); my $instance = $1; + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $1); - $results->{$instance} = { name => $snmp_result->{$_} }; + my $status = 'enabled'; + if ($result->{free} == 0 && $result->{used} == 0 && $result->{pending_offers} == 0) { + $status = 'disabled'; + } + $results->{$instance} = { address => $result->{address}, status => $status }; } return $results; @@ -69,8 +77,9 @@ sub run { foreach (sort keys %$results) { $self->{output}->output_add(long_msg => sprintf( - '[address = %s]', - $results->{$_}->{name} + '[address: %s][status: %s]', + $results->{$_}->{address}, + $results->{$_}->{status} ) ); } @@ -86,7 +95,7 @@ sub run { sub disco_format { my ($self, %options) = @_; - $self->{output}->add_disco_format(elements => ['address']); + $self->{output}->add_disco_format(elements => ['address', 'status']); } sub disco_show { @@ -94,9 +103,7 @@ sub disco_show { my $results = $self->manage_selection(snmp => $options{snmp}); foreach (sort keys %$results) { - $self->{output}->add_disco_entry( - address => $results->{$_}->{name} - ); + $self->{output}->add_disco_entry(%{$results->{$_}}); } } diff --git a/apps/microsoft/dhcp/snmp/mode/subnets.pm b/apps/microsoft/dhcp/snmp/mode/subnets.pm index 430680432..7342cb8e2 100644 --- a/apps/microsoft/dhcp/snmp/mode/subnets.pm +++ b/apps/microsoft/dhcp/snmp/mode/subnets.pm @@ -24,6 +24,16 @@ 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( + 'status: %s', + $self->{result_values}->{status} + ); +} sub custom_usage_output { my ($self, %options) = @_; @@ -49,10 +59,20 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'subnets', type => 1, cb_prefix_output => 'prefix_subnet_output', message_multiple => 'All subnets are ok' } + { name => 'subnets', type => 1, cb_prefix_output => 'prefix_subnet_output', message_multiple => 'All subnets are ok', skipped_code => { -10 => 1 } } ]; $self->{maps_counters}->{subnets} = [ + { + label => 'status', type => 2, set => { + key_values => [ + { name => 'status' }, { name => 'name' } + ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, { label => 'addresses-usage', nlabel => 'subnet.addresses.usage.count', set => { key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], closure_custom_output => $self->can('custom_usage_output'), @@ -101,7 +121,7 @@ sub new { } my $mapping = { - used => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.2' }, # noAddInUse + used => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.2' }, # noAddInUse free => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.3' }, # noAddFree pending_offers => { oid => '.1.3.6.1.4.1.311.1.3.2.1.1.4' } # noPendingOffers }; @@ -146,6 +166,11 @@ sub manage_selection { foreach (keys %{$self->{subnets}}) { my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); + $self->{subnets}->{$_}->{status} = 'enabled'; + if ($result->{free} == 0 && $result->{used} == 0 && $result->{pending_offers} == 0) { + $self->{subnets}->{$_}->{status} = 'disabled'; + next; + } $self->{subnets}->{$_}->{pending_offers} = $result->{pending_offers}; $self->{subnets}->{$_}->{free} = $result->{free}; $self->{subnets}->{$_}->{used} = $result->{used}; @@ -174,6 +199,21 @@ Example: --filter-counters='pending' Filter subnets by address (can be a regexp). +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{name} + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{name} + +=item B<--critical-status> + +Set critical threshold for status. +Can used special variables like: %{status}, %{name} + =item B<--warning-*> B<--critical-*> Thresholds.