From e7a05cbec7c69c215a4bf7762f6caa62b81a2e62 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 22 Feb 2019 14:27:55 +0100 Subject: [PATCH] add bind9 plugin --- apps/bind9/web/mode/serverusage.pm | 2 +- apps/bind9/web/mode/zoneusage.pm | 151 +++++++++++++++++++++++++++++ apps/bind9/web/plugin.pm | 1 + 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 apps/bind9/web/mode/zoneusage.pm diff --git a/apps/bind9/web/mode/serverusage.pm b/apps/bind9/web/mode/serverusage.pm index 9dfe87b83..9f996c75d 100644 --- a/apps/bind9/web/mode/serverusage.pm +++ b/apps/bind9/web/mode/serverusage.pm @@ -30,7 +30,7 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'server', type => 0, skipped_code => { -10 => 1, 11 => -1 } } + { name => 'server', type => 0, skipped_code => { -1 => 1, -10 => 1, 11 => -1 } } ]; $self->{maps_counters}->{server} = []; diff --git a/apps/bind9/web/mode/zoneusage.pm b/apps/bind9/web/mode/zoneusage.pm new file mode 100644 index 000000000..be327882f --- /dev/null +++ b/apps/bind9/web/mode/zoneusage.pm @@ -0,0 +1,151 @@ +# +# Copyright 2019 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 apps::bind9::web::mode::zoneusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'zones', type => 1, cb_prefix_output => 'prefix_zone_output', message_multiple => 'All zone counters are ok', skipped_code => { -1 => 1, -10 => 1, 11 => -1 } } + ]; + + $self->{maps_counters}->{zones} = [ + { label => 'message', threshold => 0, set => { + key_values => [ { name => 'display' } ], + closure_custom_calc => sub { return 0; }, + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => sub { return 'ok'; }, + closure_custom_output => sub { return 'counters are ok' }, + } + }, + ]; + + my @map = ( + ['qtype_a', 'qtype A : %s', 'qtype-a'], + ['qtype_cname', 'qtype CNAME : %s', 'qtype-cname'], + ['qtype_mx', 'qtype MX : %s', 'qtype-mx'], + ['qtype_txt', 'qtype TXT : %s', 'qtype-txt'], + ['qtype_soa', 'qtype SOA : %s', 'qtype-soa'], + ['qtype_ptr', 'qtype PTR : %s', 'qtype-ptr'], + ['qtype_ns', 'qtype NS : %s', 'qtype-ns'], + ['rcode_requestv4', 'rcode request v4 : %s', 'rcode-requestv4'], + ['rcode_requestv6', 'rcode request v6 : %s', 'rcode-requestv6'], + ); + + for (my $i = 0; $i < scalar(@map); $i++) { + my $perf_label = $map[$i]->[2]; + $perf_label =~ s/-/_/g; + push @{$self->{maps_counters}->{zones}}, { label => $map[$i]->[2], display_ok => 0, set => { + key_values => [ { name => $map[$i]->[0], diff => 1 }, { name => 'display' } ], + output_template => $map[$i]->[1], + perfdatas => [ + { label => $perf_label, value => $map[$i]->[0] . '_absolute', template => '%s', min => 0, + label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }; + } +} + +sub prefix_zone_output { + my ($self, %options) = @_; + + return "Zone '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + "filter-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->get_zones(); + $self->{zones} = { }; + + foreach my $zone_name (keys %{$result}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $zone_name !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $zone_name . "': no matching filter.", debug => 1); + next; + } + + $self->{zones}->{$zone_name} = { display => $zone_name }; + foreach my $type (keys %{$result->{$zone_name}->{counters}}) { + foreach my $counter (keys %{$result->{$zone_name}->{counters}->{$type}}) { + $self->{zones}->{$zone_name}->{lc($type) . '_' . lc($counter)} = $result->{$zone_name}->{counters}->{$type}->{$counter}; + } + } + } + + if (scalar(keys %{$self->{zones}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No zone found."); + $self->{output}->option_exit(); + } + + $self->{cache_name} = "bind9_" . $self->{mode} . '_' . $options{custom}->get_uniq_id() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check bind zone usage. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='request' + +=item B<--filter-name> + +Filter zone name (can be a regexp). + +=item B<--warning-*> <--critical-*> + +Thresholds. +Can be: qtype-a', 'qtype-cname', 'qtype-mx', 'qtype-txt', 'qtype-soa', 'qtype-ptr', 'qtype-ns', 'qtype-any', +'nsstat-requestv4', 'nsstat-requestv6'. + +=back + +=cut diff --git a/apps/bind9/web/plugin.pm b/apps/bind9/web/plugin.pm index 11a4e0111..d28545fd3 100644 --- a/apps/bind9/web/plugin.pm +++ b/apps/bind9/web/plugin.pm @@ -33,6 +33,7 @@ sub new { %{$self->{modes}} = ( 'memory-usage' => 'apps::bind9::web::mode::memoryusage', 'server-usage' => 'apps::bind9::web::mode::serverusage', + 'zone-usage' => 'apps::bind9::web::mode::zoneusage', ); $self->{custom_modes}{api} = 'apps::bind9::web::custom::api';