From 8546509b669a7e9f3c01faf8ad2f0df2d64d3453 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 6 Jun 2019 16:01:27 +0200 Subject: [PATCH] Fix #1023 --- apps/protocols/ldap/mode/search.pm | 116 ++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 37 deletions(-) diff --git a/apps/protocols/ldap/mode/search.pm b/apps/protocols/ldap/mode/search.pm index be09b3428..5a8bad0e1 100644 --- a/apps/protocols/ldap/mode/search.pm +++ b/apps/protocols/ldap/mode/search.pm @@ -20,13 +20,40 @@ package apps::protocols::ldap::mode::search; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; use Time::HiRes qw(gettimeofday tv_interval); use centreon::common::protocols::ldap::lib::ldap; +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'entries', nlabel => 'ldap.request.entries.count', set => { + key_values => [ { name => 'entries' } ], + output_template => 'Number of results returned: %s', + perfdatas => [ + { value => 'entries_absolute', template => '%s', min => 0 }, + ], + } + }, + { label => 'time', display_ok => 0, nlabel => 'ldap.request.time.second', set => { + key_values => [ { name => 'time' } ], + output_template => 'Response time : %.3fs', + perfdatas => [ + { label => 'time', value => 'time_absolute', template => '%.3f', min => 0, unit => 's' }, + ], + } + }, + ]; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -42,11 +69,10 @@ sub new { 'ldap-bind-options:s@' => { name => 'ldap_bind_options' }, 'ldap-search-options:s@' => { name => 'ldap_search_options' }, 'tls' => { name => 'use_tls' }, - 'username:s' => { name => 'username' }, - 'password:s' => { name => 'password' }, - 'warning:s' => { name => 'warning' }, - 'critical:s' => { name => 'critical' }, - 'timeout:s' => { name => 'timeout', default => '30' }, + 'username:s' => { name => 'username' }, + 'password:s' => { name => 'password' }, + 'timeout:s' => { name => 'timeout', default => '30' }, + 'display-entry:s' => { name => 'display_entry' }, }); return $self; @@ -54,16 +80,7 @@ sub new { sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } + $self->SUPER::check_options(%options); if (!defined($self->{option_results}->{hostname})) { $self->{output}->add_option_msg(short_msg => 'Please set the hostname option'); @@ -82,6 +99,14 @@ sub check_options { $self->{output}->add_option_msg(short_msg => 'Please set the search-filter option'); $self->{output}->option_exit(); } + + $self->{option_results}->{ldap_search_options} = [] if (!defined($self->{option_results}->{ldap_search_options})); + + if (defined($self->{option_results}->{display_entry}) && $self->{option_results}->{display_entry} ne '') { + while ($self->{option_results}->{display_entry} =~ /%\{(.*?)\}/g) { + push @{$self->{option_results}->{ldap_search_options}}, 'attrs=' . $1; + } + } } sub ldap_error { @@ -97,7 +122,25 @@ sub ldap_error { } } -sub run { +sub display_entries { + my ($self, %options) = @_; + + return if (!defined($self->{option_results}->{display_entry}) || $self->{option_results}->{display_entry} eq ''); + + foreach my $entry ($options{result}->entries()) { + my $display = $self->{option_results}->{display_entry}; + while ($display =~ /%\{(.*?)\}/g) { + my $attr = $1; + my $value = $entry->get_value($attr); + $value = '' if (!defined($value)); + $display =~ s/%\{$attr\}/$value/g; + } + + $self->{output}->output_add(long_msg => $display); + } +} + +sub manage_selection { my ($self, %options) = @_; my $timing0 = [gettimeofday]; @@ -122,25 +165,12 @@ sub run { $self->ldap_error(code => $code, err_msg => $err_msg); centreon::common::protocols::ldap::lib::ldap::quit(ldap_handle => $ldap_handle); - my $timeelapsed = tv_interval ($timing0, [gettimeofday]); - - my $num_entries = scalar($search_result->entries); - my $exit = $self->{perfdata}->threshold_check(value => $num_entries, - threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf('Number of results returned: %s', $num_entries)); - - $self->{output}->perfdata_add(label => 'time', unit => 's', - value => sprintf('%.3f', $timeelapsed), - min => 0); - $self->{output}->perfdata_add(label => 'entries', - value => $num_entries, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); + $self->{global} = { + time => tv_interval($timing0, [gettimeofday]), + entries => scalar($search_result->entries) + }; - $self->{output}->display(); - $self->{output}->exit(); + $self->display_entries(result => $search_result); } 1; @@ -207,6 +237,10 @@ Add custom bind options (can force noauth) (not really useful now). Add custom search options (can change the scope for example). +=item B<--display-entry> + +Display ldap entries (with --verbose option) (Example: '%{cn} account locked') + =item B<--username> Specify username for authentification (can be a DN) @@ -219,11 +253,19 @@ Specify password for authentification Connection timeout in seconds (Default: 30) -=item B<--warning> +=item B<--warning-time> + +Threshold warning in seconds + +=item B<--critical-time> + +Threshold critical in seconds + +=item B<--warning-entries> Threshold warning (number of results) -=item B<--critical> +=item B<--critical-entries> Threshold critical (number of results)