diff --git a/apps/openldap/ldap/mode/systemusage.pm b/apps/openldap/ldap/mode/systemusage.pm new file mode 100644 index 000000000..42c907829 --- /dev/null +++ b/apps/openldap/ldap/mode/systemusage.pm @@ -0,0 +1,270 @@ +# +# 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::openldap::ldap::mode::systemusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); +use centreon::common::protocols::ldap::lib::ldap; + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } }, + { name => 'operation', type => 0, cb_prefix_output => 'prefix_operation_output', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{operation} = []; + foreach ('search', 'add', 'bind', 'unbind', 'delete') { + push @{$self->{maps_counters}->{operation}}, + { label => 'op-' . $_, nlabel => 'system.operations.' . $_ . '.completed.count', set => { + key_values => [ { name => 'operations_completed_' . $_, diff => 1 } ], + output_template => $_ . ' %s', + perfdatas => [ + { label => 'operations_' . $_, value => 'operations_completed_' . $_ . '_absolute', template => '%.2f', + min => 0 }, + ], + } + }; + } + + + $self->{maps_counters}->{global} = [ + { label => 'con-current', nlabel => 'system.connections.current.count', set => { + key_values => [ { name => 'connections_current' } ], + output_template => 'Current connections %s', + perfdatas => [ + { label => 'connections_current', value => 'connections_current_absolute', template => '%s', + min => 0 }, + ], + } + }, + { label => 'con-total', nlabel => 'system.connections.total.count', set => { + key_values => [ { name => 'connections_total', diff => 1 } ], + output_template => 'Total connections %s', + perfdatas => [ + { label => 'connections_total', value => 'connections_total_absolute', template => '%s', + min => 0 }, + ], + } + }, + { label => 'threads-active', nlabel => 'system.threads.active.percentage', set => { + key_values => [ { name => 'threads_active_prct' } ], + output_template => 'Current active threads %.2f %%', + perfdatas => [ + { label => 'threads_active', value => 'threads_active_prct_absolute', template => '%.2f', + min => 0, max => 100, unit => '%' }, + ], + } + }, + { label => 'traffic', nlabel => 'system.traffic.bytespersecond', set => { + key_values => [ { name => 'traffic', diff => 1 } ], + output_change_bytes => 1, per_second => 1, + output_template => 'traffic %s %s/s', + perfdatas => [ + { label => 'traffic', value => 'traffic_per_second', template => '%s', + min => 0, unit => 'B/s', cast_int => 1 }, + ], + } + }, + ]; +} + +sub prefix_operation_output { + my ($self, %options) = @_; + + return 'Operation completed '; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'search-base:s' => { name => 'search_base', default => 'cn=monitor' }, + 'ldap-connect-options:s@' => { name => 'ldap_connect_options' }, + 'ldap-starttls-options:s@' => { name => 'ldap_starttls_options' }, + 'ldap-bind-options:s@' => { name => 'ldap_bind_options' }, + 'tls' => { name => 'use_tls' }, + 'username:s' => { name => 'username' }, + 'password:s' => { name => 'password' }, + 'timeout:s' => { name => 'timeout', default => '30' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => 'Please set the hostname option'); + $self->{output}->option_exit(); + } + if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' && + !defined($self->{option_results}->{password})) { + $self->{output}->add_option_msg(short_msg => "Please set --password option."); + $self->{output}->option_exit(); + } +} + +sub ldap_error { + my ($self, %options) = @_; + + if ($options{code} == 1) { + $self->{output}->output_add( + severity => 'unknown', + short_msg => $options{err_msg} + ); + $self->{output}->display(); + $self->{output}->exit(); + } +} + +sub search_monitor { + my ($self, %options) = @_; + + my ($ldap_handle, $code, $err_msg) = centreon::common::protocols::ldap::lib::ldap::connect( + hostname => $self->{option_results}->{hostname}, + username => $self->{option_results}->{username}, + password => $self->{option_results}->{password}, + timeout => $self->{option_results}->{timeout}, + ldap_connect_options => $self->{option_results}->{ldap_connect_options}, + use_tls => $self->{option_results}->{use_tls}, + ldap_starttls_options => $self->{option_results}->{ldap_starttls_options}, + ldap_bind_options => $self->{option_results}->{ldap_bind_options}, + ); + $self->ldap_error(code => $code, err_msg => $err_msg); + (my $search_result, $code, $err_msg) = centreon::common::protocols::ldap::lib::ldap::search( + ldap_handle => $ldap_handle, + search_base => $self->{option_results}->{search_base}, + search_filter => '(objectclass=*)', + ldap_search_options => ['attrs=monitoredInfo', 'attrs=monitorCounter', 'attrs=MonitorOpCompleted'], + ); + $self->ldap_error(code => $code, err_msg => $err_msg); + centreon::common::protocols::ldap::lib::ldap::quit(ldap_handle => $ldap_handle); + + return $search_result; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{operation} = {}; + $self->{global} = {}; + my $search_result = $self->search_monitor(); + foreach my $entry ($search_result->entries()) { + my $dn = $entry->dn(); + if ($dn =~ /cn=(Current|Total),cn=Connections/i) { + $self->{global}->{'connections_' . lc($1)} = $entry->get_value('monitorCounter'); + } elsif ($dn =~ /cn=(.*?),cn=Operations/i) { + $self->{operation}->{'operations_completed_' . lc($1)} = $entry->get_value('MonitorOpCompleted'); + } elsif ($dn =~ /cn=(Max|Active),cn=Threads/i) { + $self->{global}->{'threads_' . lc($1)} = $entry->get_value('monitoredInfo'); + } elsif ($dn =~ /cn=Bytes,cn=Statistics/i) { + $self->{global}->{traffic} = $entry->get_value('monitorCounter'); + } + } + + $self->{global}->{threads_active_prct} = $self->{global}->{threads_active} * 100 / $self->{global}->{threads_max}; + + $self->{cache_name} = "openldap_" . $self->{mode} . '_' . $self->{option_results}->{hostname} . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Check system usage (connections, threads, requests). + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of the openldap host (required). + +=item B<--search-base> + +Set the DN that is the base object entry relative to the backend monitor (Default: cn=monitor). + +=item B<--ldap-connect-options> + +Add custom ldap connect options: + +=over 16 + +=item B + +--ldap-connect-options='scheme=ldaps' + +=item B + +--ldap-connect-options='version=2' + +=back + +=item B<--ldap-starttls-options> + +Add custom start tls options (need --tls option): + +=over 16 + +=item B + +--ldap-starttls-options='verify=none' + +=back + +=item B<--ldap-bind-options> + +Add custom bind options (can force noauth) (not really useful now). + +=item B<--username> + +Specify username for authentification (can be a DN) + +=item B<--password> + +Specify password for authentification + +=item B<--timeout> + +Connection timeout in seconds (Default: 30) + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'con-current', 'con-total', 'threads-active', 'traffic', +'op-add', 'op-search', 'op-bind', 'op-unbind', 'op-delete'. + +=back + +=cut diff --git a/apps/openldap/ldap/plugin.pm b/apps/openldap/ldap/plugin.pm new file mode 100644 index 000000000..46cc4cd6f --- /dev/null +++ b/apps/openldap/ldap/plugin.pm @@ -0,0 +1,48 @@ +# +# 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::openldap::ldap::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_simple); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'system-usage' => 'apps::openldap::ldap::mode::systemusage', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check OpenLDAP through the monitor backend. + +=cut diff --git a/apps/protocols/ldap/lib/ldap.pm b/apps/protocols/ldap/lib/ldap.pm deleted file mode 100644 index b68fb5c20..000000000 --- a/apps/protocols/ldap/lib/ldap.pm +++ /dev/null @@ -1,132 +0,0 @@ -# -# 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::protocols::ldap::lib::ldap; - -use strict; -use warnings; -use Net::LDAP; - -my $ldap_handle; -my $connected = 0; - -sub quit { - if ($connected == 1) { - $ldap_handle->unbind; - } -} - -sub search { - my ($self, %options) = @_; - my %ldap_search_options = (); - - $ldap_search_options{base} = $self->{option_results}->{search_base}; - $ldap_search_options{filter} = $self->{option_results}->{search_filter}; - my $attrs; - foreach my $option (@{$self->{option_results}->{ldap_search_options}}) { - next if ($option !~ /^(.+?)=(.+)$/); - if ($1 =~ /attrs/) { - $attrs = [] if (!defined($attrs)); - push @$attrs, $2; - } else { - $ldap_search_options{$1} = $2; - } - } - $ldap_search_options{attrs} = $attrs if (defined($attrs)); - my $search_result = $ldap_handle->search(%ldap_search_options); - if ($search_result->code) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Search operation error: ' . $search_result->error); - $self->{output}->display(); - $self->{output}->exit(); - } - - return $search_result; -} - -sub connect { - my ($self, %options) = @_; - my %ldap_connect_options = (); - my %ldap_bind_options = (); - - if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' && - !defined($self->{option_results}->{password})) { - $self->{output}->add_option_msg(short_msg => "Please set --password option."); - $self->{output}->option_exit(); - } - - my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown'; - $ldap_connect_options{timeout} = $self->{option_results}->{timeout} if (defined($self->{option_results}->{timeout})); - foreach my $option (@{$self->{option_results}->{ldap_connect_options}}) { - next if ($option !~ /^(.+?)=(.+)$/); - $ldap_connect_options{$1} = $2; - } - - $ldap_handle = Net::LDAP->new($self->{option_results}->{hostname}, %ldap_connect_options); - - if (!defined($ldap_handle)) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Unable to connect to LDAP: ' . $@); - $self->{output}->display(); - $self->{output}->exit(); - } - - # TLS Process - if (defined($self->{option_results}->{use_tls})) { - my %ldap_starttls_options = (); - - foreach my $option (@{$self->{option_results}->{ldap_starttls_options}}) { - next if ($option !~ /^(.+?)=(.+)$/); - $ldap_starttls_options{$1} = $2; - } - - my $tls_result = $ldap_handle->start_tls(%ldap_starttls_options); - if ($tls_result->code) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Start TLS operation error: ' . $tls_result->error); - $self->{output}->display(); - $self->{output}->exit(); - } - } - - # Bind process - my $username; - if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '') { - $ldap_bind_options{password} = $self->{option_results}->{password}; - $username = $self->{option_results}->{username}; - } - - foreach my $option (@{$self->{option_results}->{ldap_bind_options}}) { - next if ($option !~ /^(.+?)=(.+)$/); - $ldap_bind_options{$1} = $2; - } - - my $bind_result = $ldap_handle->bind($username, %ldap_bind_options); - if ($bind_result->code) { - $self->{output}->output_add(severity => $connection_exit, - short_msg => 'Bind operation error: ' . $bind_result->error); - $self->{output}->display(); - $self->{output}->exit(); - } - - $connected = 1; -} - -1; diff --git a/apps/protocols/ldap/mode/login.pm b/apps/protocols/ldap/mode/login.pm index 2fb5ec758..74c7d3eff 100644 --- a/apps/protocols/ldap/mode/login.pm +++ b/apps/protocols/ldap/mode/login.pm @@ -25,7 +25,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; use Time::HiRes qw(gettimeofday tv_interval); -use apps::protocols::ldap::lib::ldap; +use centreon::common::protocols::ldap::lib::ldap; sub new { my ($class, %options) = @_; @@ -33,19 +33,19 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "ldap-connect-options:s@" => { name => 'ldap_connect_options' }, - "ldap-starttls-options:s@" => { name => 'ldap_starttls_options' }, - "ldap-bind-options:s@" => { name => 'ldap_bind_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' }, - }); + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'ldap-connect-options:s@' => { name => 'ldap_connect_options' }, + 'ldap-starttls-options:s@' => { name => 'ldap_starttls_options' }, + 'ldap-bind-options:s@' => { name => 'ldap_bind_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' }, + }); + return $self; } @@ -63,7 +63,13 @@ sub check_options { } 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'); + $self->{output}->option_exit(); + } + + if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' && + !defined($self->{option_results}->{password})) { + $self->{output}->add_option_msg(short_msg => "Please set --password option."); $self->{output}->option_exit(); } } @@ -73,16 +79,31 @@ sub run { my $timing0 = [gettimeofday]; - apps::protocols::ldap::lib::ldap::connect($self, connection_exit => 'critical'); - apps::protocols::ldap::lib::ldap::quit(); + my ($ldap_handle, $code, $err_msg) = centreon::common::protocols::ldap::lib::ldap::connect( + hostname => $self->{option_results}->{hostname}, + username => $self->{option_results}->{username}, + password => $self->{option_results}->{password}, + timeout => $self->{option_results}->{timeout}, + ldap_connect_options => $self->{option_results}->{ldap_connect_options}, + use_tls => $self->{option_results}->{use_tls}, + ldap_starttls_options => $self->{option_results}->{ldap_starttls_options}, + ldap_bind_options => $self->{option_results}->{ldap_bind_options}, + ); + if ($code == 1) { + $self->{output}->output_add(severity => 'critical', + short_msg => $err_msg); + $self->{output}->display(); + $self->{output}->exit(); + } + centreon::common::protocols::ldap::lib::ldap::quit(ldap_handle => $ldap_handle); my $timeelapsed = tv_interval ($timing0, [gettimeofday]); my $exit = $self->{perfdata}->threshold_check(value => $timeelapsed, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Response time %.3f second(s)", $timeelapsed)); - $self->{output}->perfdata_add(label => "time", unit => 's', + short_msg => sprintf('Response time %.3f second(s)', $timeelapsed)); + $self->{output}->perfdata_add(label => 'time', unit => 's', value => sprintf('%.3f', $timeelapsed), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical')); diff --git a/apps/protocols/ldap/mode/search.pm b/apps/protocols/ldap/mode/search.pm index 9eb2f6860..be09b3428 100644 --- a/apps/protocols/ldap/mode/search.pm +++ b/apps/protocols/ldap/mode/search.pm @@ -25,7 +25,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; use Time::HiRes qw(gettimeofday tv_interval); -use apps::protocols::ldap::lib::ldap; +use centreon::common::protocols::ldap::lib::ldap; sub new { my ($class, %options) = @_; @@ -33,22 +33,22 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "search-base:s" => { name => 'search_base' }, - "search-filter:s" => { name => 'search_filter' }, - "ldap-connect-options:s@" => { name => 'ldap_connect_options' }, - "ldap-starttls-options:s@" => { name => 'ldap_starttls_options' }, - "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' }, - }); + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'search-base:s' => { name => 'search_base' }, + 'search-filter:s' => { name => 'search_filter' }, + 'ldap-connect-options:s@' => { name => 'ldap_connect_options' }, + 'ldap-starttls-options:s@' => { name => 'ldap_starttls_options' }, + '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' }, + }); + return $self; } @@ -66,27 +66,61 @@ sub check_options { } 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'); + $self->{output}->option_exit(); + } + if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' && + !defined($self->{option_results}->{password})) { + $self->{output}->add_option_msg(short_msg => "Please set --password option."); $self->{output}->option_exit(); } if (!defined($self->{option_results}->{search_base})) { - $self->{output}->add_option_msg(short_msg => "Please set the search-base option"); + $self->{output}->add_option_msg(short_msg => 'Please set the search-base option'); $self->{output}->option_exit(); } if (!defined($self->{option_results}->{search_filter})) { - $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(); } } +sub ldap_error { + my ($self, %options) = @_; + + if ($options{code} == 1) { + $self->{output}->output_add( + severity => 'unknown', + short_msg => $options{err_msg} + ); + $self->{output}->display(); + $self->{output}->exit(); + } +} + sub run { my ($self, %options) = @_; my $timing0 = [gettimeofday]; - apps::protocols::ldap::lib::ldap::connect($self); - my $search_result = apps::protocols::ldap::lib::ldap::search($self); - apps::protocols::ldap::lib::ldap::quit(); + my ($ldap_handle, $code, $err_msg) = centreon::common::protocols::ldap::lib::ldap::connect( + hostname => $self->{option_results}->{hostname}, + username => $self->{option_results}->{username}, + password => $self->{option_results}->{password}, + timeout => $self->{option_results}->{timeout}, + ldap_connect_options => $self->{option_results}->{ldap_connect_options}, + use_tls => $self->{option_results}->{use_tls}, + ldap_starttls_options => $self->{option_results}->{ldap_starttls_options}, + ldap_bind_options => $self->{option_results}->{ldap_bind_options}, + ); + $self->ldap_error(code => $code, err_msg => $err_msg); + (my $search_result, $code, $err_msg) = centreon::common::protocols::ldap::lib::ldap::search( + ldap_handle => $ldap_handle, + search_base => $self->{option_results}->{search_base}, + search_filter => $self->{option_results}->{search_filter}, + ldap_search_options => $self->{option_results}->{ldap_search_options}, + ); + $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]); @@ -94,12 +128,12 @@ sub run { 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)); + short_msg => sprintf('Number of results returned: %s', $num_entries)); - $self->{output}->perfdata_add(label => "time", unit => 's', + $self->{output}->perfdata_add(label => 'time', unit => 's', value => sprintf('%.3f', $timeelapsed), min => 0); - $self->{output}->perfdata_add(label => "entries", + $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'), diff --git a/centreon/common/protocols/ldap/lib/ldap.pm b/centreon/common/protocols/ldap/lib/ldap.pm new file mode 100644 index 000000000..3c850b893 --- /dev/null +++ b/centreon/common/protocols/ldap/lib/ldap.pm @@ -0,0 +1,112 @@ +# +# 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 centreon::common::protocols::ldap::lib::ldap; + +use strict; +use warnings; +use Net::LDAP; + +sub quit { + my (%options) = @_; + + if (defined($options{ldap_handle})) { + $options{ldap_handle}->unbind(); + } +} + +sub search { + my (%options) = @_; + my %ldap_search_options = (); + + $ldap_search_options{base} = $options{search_base}; + $ldap_search_options{filter} = $options{search_filter}; + my $attrs; + foreach my $option (@{$options{ldap_search_options}}) { + next if ($option !~ /^\s*(.+?)\s*=(.+)$/); + if ($1 eq 'attrs') { + $attrs = [] if (!defined($attrs)); + push @$attrs, $2; + } else { + $ldap_search_options{$1} = $2; + } + } + $ldap_search_options{attrs} = $attrs if (defined($attrs)); + my $search_result = $options{ldap_handle}->search(%ldap_search_options); + if ($search_result->code) { + return ($search_result, 1, 'Search operation error: ' . $search_result->error); + } + + return ($search_result, 0); +} + +sub connect { + my (%options) = @_; + my %ldap_connect_options = (); + my %ldap_bind_options = (); + + $ldap_connect_options{timeout} = $options{timeout} if (defined($options{timeout})); + foreach my $option (@{$options{ldap_connect_options}}) { + next if ($option !~ /^(.+?)=(.+)$/); + $ldap_connect_options{$1} = $2; + } + + my $ldap_handle = Net::LDAP->new($options{hostname}, %ldap_connect_options); + + if (!defined($ldap_handle)) { + return (undef, 1, 'Unable to connect to LDAP: ' . $@); + } + + # TLS Process + if (defined($options{use_tls})) { + my %ldap_starttls_options = (); + + foreach my $option (@{$options{ldap_starttls_options}}) { + next if ($option !~ /^(.+?)=(.+)$/); + $ldap_starttls_options{$1} = $2; + } + + my $tls_result = $ldap_handle->start_tls(%ldap_starttls_options); + if ($tls_result->code) { + return ($ldap_handle, 1, 'Start TLS operation error: ' . $tls_result->error); + } + } + + # Bind process + my $username; + if (defined($options{username}) && $options{username} ne '') { + $ldap_bind_options{password} = $options{password}; + $username = $options{username}; + } + + foreach my $option (@{$options{ldap_bind_options}}) { + next if ($option !~ /^(.+?)=(.+)$/); + $ldap_bind_options{$1} = $2; + } + + my $bind_result = $ldap_handle->bind($username, %ldap_bind_options); + if ($bind_result->code) { + return ($ldap_handle, 1, 'Bind operation error: ' . $bind_result->error); + } + + return ($ldap_handle, 0); +} + +1;