From c01470e857a42f9f082fb46cdcab22e1a0e1fe4b Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 22 Oct 2021 14:29:19 +0200 Subject: [PATCH] enh(infoblox/snmp): dhcp-usage mode - add cache option (#3215) --- network/infoblox/snmp/mode/dhcpusage.pm | 107 ++++++++++++---- network/infoblox/snmp/mode/listdhcpsubnets.pm | 116 ++++++++++++++++++ network/infoblox/snmp/plugin.pm | 13 +- 3 files changed, 208 insertions(+), 28 deletions(-) create mode 100644 network/infoblox/snmp/mode/listdhcpsubnets.pm diff --git a/network/infoblox/snmp/mode/dhcpusage.pm b/network/infoblox/snmp/mode/dhcpusage.pm index fcdd21d96..e51f98404 100644 --- a/network/infoblox/snmp/mode/dhcpusage.pm +++ b/network/infoblox/snmp/mode/dhcpusage.pm @@ -24,18 +24,19 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use centreon::plugins::statefile; use Digest::MD5 qw(md5_hex); sub prefix_global_output { my ($self, %options) = @_; - return "Total "; + return 'Total '; } sub prefix_dhcp_output { my ($self, %options) = @_; - return "Subnet '" . $options{instance_value}->{display} . "' "; + return "Subnet '" . $options{instance_value}->{name} . "' "; } sub set_counters { @@ -73,10 +74,10 @@ sub set_counters { $self->{maps_counters}->{dhcp} = [ { label => 'subnet-used', nlabel => 'subnet.addresses.usage.percentage', set => { - key_values => [ { name => 'ibDHCPSubnetPercentUsed' }, { name => 'display' } ], + key_values => [ { name => 'subnet_used' }, { name => 'name' } ], output_template => 'used: %.2f %%', perfdatas => [ - { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' } ] } } @@ -89,12 +90,25 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-name:s' => { name => 'filter_name' } + 'filter-name:s' => { name => 'filter_name' }, + 'cache' => { name => 'cache' }, + 'cache-time:s' => { name => 'cache_time', default => 180 } }); + $self->{lcache} = centreon::plugins::statefile->new(%options); + return $self; } +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (defined($self->{option_results}->{cache})) { + $self->{lcache}->check_options(option_results => $self->{option_results}); + } +} + my $mapping = { ibDhcpTotalNoOfDiscovers => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.1' }, ibDhcpTotalNoOfRequests => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.2' }, @@ -107,32 +121,57 @@ my $mapping = { ibDhcpTotalNoOfOthers => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.9' } }; my $mapping2 = { - ibDHCPSubnetNetworkAddress => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.1' }, - ibDHCPSubnetNetworkMask => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.2' }, - ibDHCPSubnetPercentUsed => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.3' } + address => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.1' }, # ibDHCPSubnetNetworkAddress + mask => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.2' } # ibDHCPSubnetNetworkMask }; -my $oid_ibDHCPStatistics = '.1.3.6.1.4.1.7779.3.1.1.4.1.3'; my $oid_ibDHCPSubnetEntry = '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1'; +my $oid_subnet_prct_used = '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.3'; # ibDHCPSubnetPercentUsed + +sub get_snmp_subnets { + my ($self, %options) = @_; + + return $options{snmp}->get_table( + oid => $oid_ibDHCPSubnetEntry, + end => $mapping2->{mask}->{oid}, + nothing_quit => 1 + ); +} + +sub get_subnets { + my ($self, %options) = @_; + + my $subnets; + if (defined($self->{option_results}->{cache})) { + my $has_cache_file = $self->{lcache}->read(statefile => 'infoblox_cache_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port()); + my $infos = $self->{lcache}->get(name => 'infos'); + if ($has_cache_file == 0 || + !defined($infos->{updated}) || + ((time() - $infos->{updated}) > (($self->{option_results}->{cache_time}) * 60))) { + $subnets = $self->get_snmp_subnets(snmp => $options{snmp}); + $self->{lcache}->write(data => { updated => time(), snmp_result => $subnets }); + } else { + $subnets = $infos->{snmp_result}; + } + } else { + $subnets = $self->get_snmp_subnets(snmp => $options{snmp}); + } + + return $subnets; +} sub manage_selection { my ($self, %options) = @_; - my $snmp_result = $options{snmp}->get_multiple_table( - oids => [ - { oid => $oid_ibDHCPStatistics }, - { oid => $oid_ibDHCPSubnetEntry } - ], - nothing_quit => 1 - ); + my $subnets = $self->get_subnets(snmp => $options{snmp}); $self->{dhcp} = {}; - foreach my $oid (keys %{$snmp_result->{$oid_ibDHCPSubnetEntry}}) { - next if ($oid !~ /^$mapping2->{ibDHCPSubnetNetworkAddress}->{oid}\.(.*)$/); + foreach my $oid (keys %$subnets) { + next if ($oid !~ /^$mapping2->{address}->{oid}\.(.*)$/); my $instance = $1; - my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result->{$oid_ibDHCPSubnetEntry}, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $subnets, instance => $instance); - my $name = $result->{ibDHCPSubnetNetworkAddress} . '/' . $result->{ibDHCPSubnetNetworkMask}; + my $name = $result->{address} . '/' . $result->{mask}; if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $name !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); @@ -140,12 +179,28 @@ sub manage_selection { } $self->{dhcp}->{$instance} = { - display => $name, + name => $name, %$result }; } - $self->{global} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result->{$oid_ibDHCPStatistics}, instance => 0); + $options{snmp}->load( + oids => [ map($_->{oid}, values(%$mapping)) ], + instances => [0], + instance_regexp => '^(.*)$' + ); + $options{snmp}->load( + oids => [$oid_subnet_prct_used], + instances => [ map($_, keys(%{$self->{dhcp}})) ], + instance_regexp => '^(.*)$' + ); + my $snmp_result = $options{snmp}->get_leef(); + foreach (keys %{$self->{dhcp}}) { + $self->{dhcp}->{$_}->{subnet_used} = $snmp_result->{ $oid_subnet_prct_used . '.' . $_ } + if (defined($snmp_result->{ $oid_subnet_prct_used . '.' . $_ })); + } + + $self->{global} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => 0); $self->{cache_name} = 'infoblox_' . $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')) . '_' . @@ -171,6 +226,14 @@ Example: --filter-counters='total-requests' Filter dhcp subnet name (can be a regexp). +=item B<--cache> + +Use cache file to store subnets. + +=item B<--cache-time> + +Time in minutes before reloading cache file (default: 180). + =item B<--warning-*> ibDhcpTotalNoOfDiscovers diff --git a/network/infoblox/snmp/mode/listdhcpsubnets.pm b/network/infoblox/snmp/mode/listdhcpsubnets.pm new file mode 100644 index 000000000..d87333d92 --- /dev/null +++ b/network/infoblox/snmp/mode/listdhcpsubnets.pm @@ -0,0 +1,116 @@ +# +# Copyright 2021 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 network::infoblox::snmp::mode::listdhcpsubnets; + +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 = { + address => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.1' }, # ibDHCPSubnetNetworkAddress + mask => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.2' } # ibDHCPSubnetNetworkMask +}; +my $oid_ibDHCPSubnetEntry = '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1'; + +sub manage_selection { + my ($self, %options) = @_; + + my $snmp_result = $options{snmp}->get_table( + oid => $oid_ibDHCPSubnetEntry, + end => $mapping->{mask}->{oid}, + ); + + my $subnets = {}; + foreach my $oid (keys %$snmp_result) { + next if ($oid !~ /^$mapping->{address}->{oid}\.(.*)$/); + $subnets->{$1} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $1); + } + + return $subnets; +} + +sub run { + my ($self, %options) = @_; + + my $subnets = $self->manage_selection(%options); + foreach (keys %$subnets) { + $self->{output}->output_add( + long_msg => sprintf( + '[address: %s][mask: %s]', + $subnets->{$_}->{address}, + $subnets->{$_}->{mask} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List subnets:' + ); + $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 => ['address', 'mask']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $subnets = $self->manage_selection(%options); + foreach (keys %$subnets) { + $self->{output}->add_disco_entry(%{$subnets->{$_}}); + } +} + +1; + +__END__ + +=head1 MODE + +List DHCP subnets. + +=over 8 + +=back + +=cut diff --git a/network/infoblox/snmp/plugin.pm b/network/infoblox/snmp/plugin.pm index 6560c6e11..6c672d1b5 100644 --- a/network/infoblox/snmp/plugin.pm +++ b/network/infoblox/snmp/plugin.pm @@ -31,12 +31,13 @@ sub new { $self->{version} = '1.0'; $self->{modes} = { - 'dhcp-usage' => 'network::infoblox::snmp::mode::dhcpusage', - 'dns-usage' => 'network::infoblox::snmp::mode::dnsusage', - 'interfaces' => 'snmp_standard::mode::interfaces', - 'list-interfaces' => 'snmp_standard::mode::listinterfaces', - 'services' => 'network::infoblox::snmp::mode::services', - 'system' => 'network::infoblox::snmp::mode::system' + 'dhcp-usage' => 'network::infoblox::snmp::mode::dhcpusage', + 'dns-usage' => 'network::infoblox::snmp::mode::dnsusage', + 'interfaces' => 'snmp_standard::mode::interfaces', + 'list-dhcp-subnets' => 'network::infoblox::snmp::mode::listdhcpsubnets', + 'list-interfaces' => 'snmp_standard::mode::listinterfaces', + 'services' => 'network::infoblox::snmp::mode::services', + 'system' => 'network::infoblox::snmp::mode::system' }; return $self;