This commit is contained in:
garnier-quentin 2019-06-06 16:01:27 +02:00
parent 8f4c95d85b
commit f44426cfb4
1 changed files with 79 additions and 37 deletions

View File

@ -20,13 +20,40 @@
package apps::protocols::ldap::mode::search; package apps::protocols::ldap::mode::search;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use Time::HiRes qw(gettimeofday tv_interval); use Time::HiRes qw(gettimeofday tv_interval);
use centreon::common::protocols::ldap::lib::ldap; 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 { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
@ -44,9 +71,8 @@ sub new {
'tls' => { name => 'use_tls' }, 'tls' => { name => 'use_tls' },
'username:s' => { name => 'username' }, 'username:s' => { name => 'username' },
'password:s' => { name => 'password' }, 'password:s' => { name => 'password' },
'warning:s' => { name => 'warning' },
'critical:s' => { name => 'critical' },
'timeout:s' => { name => 'timeout', default => '30' }, 'timeout:s' => { name => 'timeout', default => '30' },
'display-entry:s' => { name => 'display_entry' },
}); });
return $self; return $self;
@ -54,16 +80,7 @@ sub new {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::check_options(%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();
}
if (!defined($self->{option_results}->{hostname})) { if (!defined($self->{option_results}->{hostname})) {
$self->{output}->add_option_msg(short_msg => 'Please set the hostname option'); $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}->add_option_msg(short_msg => 'Please set the search-filter option');
$self->{output}->option_exit(); $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 { 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 ($self, %options) = @_;
my $timing0 = [gettimeofday]; my $timing0 = [gettimeofday];
@ -122,25 +165,12 @@ sub run {
$self->ldap_error(code => $code, err_msg => $err_msg); $self->ldap_error(code => $code, err_msg => $err_msg);
centreon::common::protocols::ldap::lib::ldap::quit(ldap_handle => $ldap_handle); centreon::common::protocols::ldap::lib::ldap::quit(ldap_handle => $ldap_handle);
my $timeelapsed = tv_interval ($timing0, [gettimeofday]); $self->{global} = {
time => tv_interval($timing0, [gettimeofday]),
entries => scalar($search_result->entries)
};
my $num_entries = scalar($search_result->entries); $self->display_entries(result => $search_result);
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->{output}->display();
$self->{output}->exit();
} }
1; 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). 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> =item B<--username>
Specify username for authentification (can be a DN) Specify username for authentification (can be a DN)
@ -219,11 +253,19 @@ Specify password for authentification
Connection timeout in seconds (Default: 30) 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) Threshold warning (number of results)
=item B<--critical> =item B<--critical-entries>
Threshold critical (number of results) Threshold critical (number of results)