Debian Codegraph @

src/network/infoblox/snmp/mode/dnsusage.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::dnsusage;
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_dns_output {
31 my ($self, %options) = @_;
32
33 return "zone '" . $options{instance_value}->{name} . "' ";
34 }
35
36 sub prefix_aa_output {
37 my ($self, %options) = @_;
38
39 return 'autoritative average latency ';
40 }
41
42 sub prefix_naa_output {
43 my ($self, %options) = @_;
44
45 return 'non autoritative average latency ';
46 }
47
48 sub set_counters {
49 my ($self, %options) = @_;
50
51 $self->{maps_counters_type} = [
52 { name => 'global', type => 0 },
53 { name => 'aa', type => 0, cb_prefix_output => 'prefix_aa_output' },
54 { name => 'naa', type => 0, cb_prefix_output => 'prefix_naa_output' },
55 { name => 'dns', type => 1, cb_prefix_output => 'prefix_dns_output', message_multiple => 'All dns zones are ok' }
56 ];
57
58 $self->{maps_counters}->{global} = [
59 { label => 'total-query-rate', nlabel => 'dns.queries.persecond', set => {
60 key_values => [ { name => 'dns_query' } ],
61 output_template => 'query rate: %s/s',
62 perfdatas => [
63 { template => '%s', min => 0 }
64 ]
65 }
66 },
67 { label => 'total-hit-ratio', nlabel => 'dns.hits.percentage', set => {
68 key_values => [ { name => 'dns_hit' } ],
69 output_template => 'hit ratio: %.2f %%',
70 perfdatas => [
71 { template => '%.2f', min => 0, max => 100, unit => '%' }
72 ]
73 }
74 }
75 ];
76
77 my $nlabels = { aa => 'authoritative', naa => 'non_authoritative' };
78 foreach (('aa', 'naa')) {
79 $self->{maps_counters}->{$_} = [];
80 foreach my $timeframe (('1m', '5m', '15m')) {
81 push @{$self->{maps_counters}->{$_}}, {
82 label => $_ . '-latency-' . $timeframe, nlabel => 'dns.queries.' . $nlabels->{$_} . '.latency.' . $timeframe . '.microseconds', set => {
83 key_values => [ { name => $_ . '_latency_' . $timeframe } ],
84 output_template => '%s (' . $timeframe . ')',
85 perfdatas => [
86 { template => '%s', min => 0, unit => 'us' }
87 ]
88 }
89 };
90 }
91 }
92
93 $self->{maps_counters}->{dns} = [
94 { label => 'success-count', nlabel => 'zone.responses.succeeded.count', set => {
95 key_values => [ { name => 'success', diff => 1 }, { name => 'name' } ],
96 output_template => 'success responses: %s',
97 perfdatas => [
98 { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' }
99 ]
100 }
101 },
102 { label => 'referral-count', nlabel => 'zone.referrals.count', set => {
103 key_values => [ { name => 'referral', diff => 1 }, { name => 'name' } ],
104 output_template => 'referrals: %s',
105 perfdatas => [
106 { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' }
107 ]
108 }
109 },
110 { label => 'nxrrset-count', nlabel => 'zone.queries.nxrrset.count', set => {
111 key_values => [ { name => 'nxrrset', diff => 1 }, { name => 'name' } ],
112 output_template => 'non-existent record: %s',
113 perfdatas => [
114 { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' }
115 ]
116 }
117 },
118 { label => 'failure-count', nlabel => 'zone.queries.failed.count', set => {
119 key_values => [ { name => 'failure', diff => 1 }, { name => 'name' } ],
120 output_template => 'failed queries: %s',
121 perfdatas => [
122 { template => '%s', min => 0, label_extra_instance => 1, instance_use => 'name' }
123 ]
124 }
125 }
126 ];
127 }
128
129 sub new {
130 my ($class, %options) = @_;
131 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
132 bless $self, $class;
133
134 $options{options}->add_options(arguments => {
135 'filter-name:s' => { name => 'filter_name' },
136 'cache' => { name => 'cache' },
137 'cache-time:s' => { name => 'cache_time', default => 180 }
138 });
139
140 $self->{lcache} = centreon::plugins::statefile->new(%options);
141
142 return $self;
143 }
144
145 sub check_options {
146 my ($self, %options) = @_;
147 $self->SUPER::check_options(%options);
148
149 if (defined($self->{option_results}->{cache})) {
150 $self->{lcache}->check_options(option_results => $self->{option_results});
151 }
152 }
153
154 my $mapping = {
155 success => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.1.1.2' }, # ibBindZoneSuccess
156 referral => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.1.1.3' }, # ibBindZoneReferral
157 nxrrset => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.1.1.4' }, # ibBindZoneNxRRset
158 failure => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.1.1.7' } # ibBindZoneFailure
159 };
160 my $mapping2 = {
161 naa_latency_1m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.2.1.1' }, # ibNetworkMonitorDNSNonAAT1AvgLatency
162 naa_latency_5m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.2.2.1' }, # ibNetworkMonitorDNSNonAAT5AvgLatency
163 naa_latency_15m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.2.3.1' }, # ibNetworkMonitorDNSNonAAT15AvgLatency
164 aa_latency_1m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.3.1.1' }, # ibNetworkMonitorDNSAAT1AvgLatency
165 aa_latency_5m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.3.2.1' }, # ibNetworkMonitorDNSAAT5AvgLatency
166 aa_latency_15m => { oid => '.1.3.6.1.4.1.7779.3.1.1.2.1.3.1.3.3.1' }, # ibNetworkMonitorDNSAAT15AvgLatency
167 dns_hit => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.5' }, # ibDnsHitRatio
168 dns_query => { oid => '.1.3.6.1.4.1.7779.3.1.1.3.1.6' } # ibDnsQueryRate
169 };
170 my $oid_name = '.1.3.6.1.4.1.7779.3.1.1.3.1.1.1.1'; # ibBindZoneName
171
172 sub get_snmp_zones {
173 my ($self, %options) = @_;
174
175 return $options{snmp}->get_table(
176 oid => $oid_name,
177 nothing_quit => 1
178 );
179 }
180
181 sub get_zones {
182 my ($self, %options) = @_;
183
184 my $zones;
185 if (defined($self->{option_results}->{cache})) {
186 my $has_cache_file = $self->{lcache}->read(statefile => 'infoblox_cache_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port());
187 my $infos = $self->{lcache}->get(name => 'infos');
188 if ($has_cache_file == 0 ||
189 !defined($infos->{updated}) ||
190 ((time() - $infos->{updated}) > (($self->{option_results}->{cache_time}) * 60))) {
191 $zones = $self->get_snmp_zones(snmp => $options{snmp});
192 $self->{lcache}->write(data => { infos => { updated => time(), snmp_result => $zones } });
193 } else {
194 $zones = $infos->{snmp_result};
195 }
196 } else {
197 $zones = $self->get_snmp_zones(snmp => $options{snmp});
198 }
199
200 return $zones;
201 }
202
203 sub manage_selection {
204 my ($self, %options) = @_;
205
206 if ($options{snmp}->is_snmpv1()) {
207 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
208 $self->{output}->option_exit();
209 }
210
211 my $snmp_result = $options{snmp}->get_leef(
212 oids => [ map($_->{oid} . '.0', values(%$mapping2)) ],
213 nothing_quit => 1
214 );
215 $self->{global} = $options{snmp}->map_instance(mapping => $mapping2, results => $snmp_result, instance => 0);
216 $self->{aa} = $self->{global};
217 $self->{naa} = $self->{global};
218
219 my $zones = $self->get_zones(snmp => $options{snmp});
220 $self->{dns} = {};
221 foreach my $oid (keys %$zones) {
222 $oid =~ /^$oid_name\.(.*)$/;
223 my $instance = $1;
224 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
225 $zones->{$oid} !~ /$self->{option_results}->{filter_name}/) {
226 $self->{output}->output_add(long_msg => "skipping '" . $zones->{$oid} . "': no matching filter.", debug => 1);
227 next;
228 }
229
230 $self->{dns}->{$instance} = {
231 name => $zones->{$oid}
232 };
233 }
234
235 $options{snmp}->load(
236 oids => [ map($_->{oid}, values(%$mapping)) ],
237 instances => [ map($_, keys(%{$self->{dns}})) ],
238 instance_regexp => '^(.*)$'
239 );
240 $snmp_result = $options{snmp}->get_leef();
241 foreach my $instance (keys %{$self->{dns}}) {
242 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
243 foreach (keys %$result) {
244 $self->{dns}->{$instance}->{$_} = $result->{$_};
245 }
246 }
247
248 $self->{cache_name} = 'infoblox_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
249 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
250 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
251 }
252
253 1;
254
255 __END__
256
257 =head1 MODE
258
259 Check dns usage.
260
261 =over 8
262
263 =item B<--filter-counters>
264
265 Only display some counters (regexp can be used).
266 Example: --filter-counters='success'
267
268 =item B<--filter-name>
269
270 Filter dns zone name (can be a regexp).
271
272 =item B<--cache>
273
274 Use cache file to store dns zone.
275
276 =item B<--cache-time>
277
278 Time in minutes before reloading cache file (default: 180).
279
280 =item B<--warning-*>
281
282 Warning threshold.
283 Can be: 'total-query-rate', 'total-hit-ratio', 'success-count', 'referral-count', 'nxrrset-count',
284 'failure-count'.
285
286 =item B<--critical-*>
287
288 Critical threshold.
289 Can be: 'total-query-rate', 'total-hit-ratio',
290 'success-count', 'referral-count', 'nxrrset-count', 'failure-count',
291 'aa-latency-1m', 'aa-latency-5m', 'aa-latency-15m',
292 'naa-latency-1m', 'naa-latency-5m', 'naa-latency-15m'.
293
294 =back
295
296 =cut