Debian Codegraph @

src/network/infoblox/snmp/mode/dhcpusage.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package network::infoblox::snmp::mode::dhcpusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::statefile;
28 use Digest::MD5 qw(md5_hex);
29
30 sub prefix_global_output {
31 my ($self, %options) = @_;
32
33 return 'Total ';
34 }
35
36 sub prefix_dhcp_output {
37 my ($self, %options) = @_;
38
39 return "Subnet '" . $options{instance_value}->{name} . "' ";
40 }
41
42 sub set_counters {
43 my ($self, %options) = @_;
44
45 $self->{maps_counters_type} = [
46 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
47 { name => 'dhcp', type => 1, cb_prefix_output => 'prefix_dhcp_output', message_multiple => 'All dhcp subnets are ok' }
48 ];
49
50 my @map = (
51 ['ibDhcpTotalNoOfDiscovers', 'discovers: %s', 'total-discovers', 'dhcp.discovers.count'],
52 ['ibDhcpTotalNoOfRequests', 'requests: %s', 'total-requests', 'dhcp.requests.count'],
53 ['ibDhcpTotalNoOfReleases', 'releases: %s', 'total-releases', 'dhcp.releases.count'],
54 ['ibDhcpTotalNoOfOffers', 'offers: %s', 'total-offers', 'dhcp.offers.count'],
55 ['ibDhcpTotalNoOfAcks', 'acks: %s', 'total-acks', 'dhcp.acks.count'],
56 ['ibDhcpTotalNoOfNacks', 'nacks: %s', 'total-nacks', 'dhcp.nacks.count'],
57 ['ibDhcpTotalNoOfDeclines', 'declines: %s', 'total-declines', 'dhcp.declines.count'],
58 ['ibDhcpTotalNoOfInforms', 'informs: %s', 'total-informs', 'dhcp.informs.count'],
59 ['ibDhcpTotalNoOfOthers', 'others: %s', 'total-others', 'dhcp.others.count']
60 );
61
62 $self->{maps_counters}->{global} = [];
63 for (my $i = 0; $i < scalar(@map); $i++) {
64 push @{$self->{maps_counters}->{global}}, {
65 label => $map[$i]->[2], nlabel => $map[$i]->[3], set => {
66 key_values => [ { name => $map[$i]->[0], diff => 1 } ],
67 output_template => $map[$i]->[1],
68 perfdatas => [
69 { template => '%s', min => 0 }
70 ]
71 }
72 };
73 }
74
75 $self->{maps_counters}->{dhcp} = [
76 { label => 'subnet-used', nlabel => 'subnet.addresses.usage.percentage', set => {
77 key_values => [ { name => 'subnet_used' }, { name => 'name' } ],
78 output_template => 'used: %.2f %%',
79 perfdatas => [
80 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' }
81 ]
82 }
83 }
84 ];
85 }
86
87 sub new {
88 my ($class, %options) = @_;
89 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
90 bless $self, $class;
91
92 $options{options}->add_options(arguments => {
93 'filter-name:s' => { name => 'filter_name' },
94 'cache' => { name => 'cache' },
95 'cache-time:s' => { name => 'cache_time', default => 180 }
96 });
97
98 $self->{lcache} = centreon::plugins::statefile->new(%options);
99
100 return $self;
101 }
102
103 sub check_options {
104 my ($self, %options) = @_;
105 $self->SUPER::check_options(%options);
106
107 if (defined($self->{option_results}->{cache})) {
108 $self->{lcache}->check_options(option_results => $self->{option_results});
109 }
110 }
111
112 my $mapping = {
113 ibDhcpTotalNoOfDiscovers => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.1' },
114 ibDhcpTotalNoOfRequests => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.2' },
115 ibDhcpTotalNoOfReleases => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.3' },
116 ibDhcpTotalNoOfOffers => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.4' },
117 ibDhcpTotalNoOfAcks => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.5' },
118 ibDhcpTotalNoOfNacks => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.6' },
119 ibDhcpTotalNoOfDeclines => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.7' },
120 ibDhcpTotalNoOfInforms => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.8' },
121 ibDhcpTotalNoOfOthers => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.3.9' }
122 };
123 my $mapping2 = {
124 address => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.1' }, # ibDHCPSubnetNetworkAddress
125 mask => { oid => '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.2' } # ibDHCPSubnetNetworkMask
126 };
127
128 my $oid_ibDHCPSubnetEntry = '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1';
129 my $oid_subnet_prct_used = '.1.3.6.1.4.1.7779.3.1.1.4.1.1.1.3'; # ibDHCPSubnetPercentUsed
130
131 sub get_snmp_subnets {
132 my ($self, %options) = @_;
133
134 return $options{snmp}->get_table(
135 oid => $oid_ibDHCPSubnetEntry,
136 end => $mapping2->{mask}->{oid},
137 nothing_quit => 1
138 );
139 }
140
141 sub get_subnets {
142 my ($self, %options) = @_;
143
144 my $subnets;
145 if (defined($self->{option_results}->{cache})) {
146 my $has_cache_file = $self->{lcache}->read(statefile => 'infoblox_cache_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port());
147 my $infos = $self->{lcache}->get(name => 'infos');
148 if ($has_cache_file == 0 ||
149 !defined($infos->{updated}) ||
150 ((time() - $infos->{updated}) > (($self->{option_results}->{cache_time}) * 60))) {
151 $subnets = $self->get_snmp_subnets(snmp => $options{snmp});
152 $self->{lcache}->write(data => { infos => { updated => time(), snmp_result => $subnets } });
153 } else {
154 $subnets = $infos->{snmp_result};
155 }
156 } else {
157 $subnets = $self->get_snmp_subnets(snmp => $options{snmp});
158 }
159
160 return $subnets;
161 }
162
163 sub manage_selection {
164 my ($self, %options) = @_;
165
166 my $subnets = $self->get_subnets(snmp => $options{snmp});
167
168 $self->{dhcp} = {};
169 foreach my $oid (keys %$subnets) {
170 next if ($oid !~ /^$mapping2->{address}->{oid}\.(.*)$/);
171 my $instance = $1;
172 my $result = $options{snmp}->map_instance(mapping => $mapping2, results => $subnets, instance => $instance);
173
174 my $name = $result->{address} . '/' . $result->{mask};
175 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
176 $name !~ /$self->{option_results}->{filter_name}/) {
177 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
178 next;
179 }
180
181 $self->{dhcp}->{$instance} = {
182 name => $name,
183 %$result
184 };
185 }
186
187 $options{snmp}->load(
188 oids => [ map($_->{oid}, values(%$mapping)) ],
189 instances => [0],
190 instance_regexp => '^(.*)$'
191 );
192 $options{snmp}->load(
193 oids => [$oid_subnet_prct_used],
194 instances => [ map($_, keys(%{$self->{dhcp}})) ],
195 instance_regexp => '^(.*)$'
196 );
197 my $snmp_result = $options{snmp}->get_leef();
198 foreach (keys %{$self->{dhcp}}) {
199 $self->{dhcp}->{$_}->{subnet_used} = $snmp_result->{ $oid_subnet_prct_used . '.' . $_ }
200 if (defined($snmp_result->{ $oid_subnet_prct_used . '.' . $_ }));
201 }
202
203 $self->{global} = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => 0);
204
205 $self->{cache_name} = 'infoblox_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
206 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
207 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
208 }
209
210 1;
211
212 __END__
213
214 =head1 MODE
215
216 Check dhcp usage.
217
218 =over 8
219
220 =item B<--filter-counters>
221
222 Only display some counters (regexp can be used).
223 Example: --filter-counters='total-requests'
224
225 =item B<--filter-name>
226
227 Filter dhcp subnet name (can be a regexp).
228
229 =item B<--cache>
230
231 Use cache file to store subnets.
232
233 =item B<--cache-time>
234
235 Time in minutes before reloading cache file (default: 180).
236
237 =item B<--warning-*>
238
239 ibDhcpTotalNoOfDiscovers
240
241 Warning threshold.
242 Can be: 'total-discovers', 'total-requests', 'total-releases',
243 'total-offers', 'total-acks', 'total-nacks', 'total-declines',
244 'total-informs', 'total-others', 'subnet-used' (%).
245
246 =item B<--critical-*>
247
248 Critical threshold.
249 Can be: 'total-discovers', 'total-requests', 'total-releases',
250 'total-offers', 'total-acks', 'total-nacks', 'total-declines',
251 'total-informs', 'total-others', 'subnet-used' (%).
252
253 =back
254
255 =cut