diff --git a/centreon-plugins/centreon/plugins/dbi.pm b/centreon-plugins/centreon/plugins/dbi.pm index 690aa318b..3e766f96c 100644 --- a/centreon-plugins/centreon/plugins/dbi.pm +++ b/centreon-plugins/centreon/plugins/dbi.pm @@ -52,7 +52,7 @@ sub new { 'password:s@' => { name => 'password' }, 'connect-options:s@' => { name => 'connect_options' }, 'sql-errors-exit:s' => { name => 'sql_errors_exit', default => 'unknown' }, - 'timeout:i' => { name => 'timeout' }, + 'timeout:s' => { name => 'timeout' }, }); } $options{options}->add_help(package => __PACKAGE__, sections => 'DBI OPTIONS', once => 1); @@ -92,7 +92,7 @@ sub class_handle_ALRM { sub handle_ALRM { my $self = shift; - + $self->disconnect(); $self->{output}->output_add(severity => $self->{sql_errors_exit}, short_msg => 'Timeout'); diff --git a/centreon-plugins/database/oracle/dbi.pm b/centreon-plugins/database/oracle/dbi.pm new file mode 100644 index 000000000..b0b6a714c --- /dev/null +++ b/centreon-plugins/database/oracle/dbi.pm @@ -0,0 +1,86 @@ +# +# 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 database::oracle::dbi; + +use base qw(centreon::plugins::dbi); + +use strict; +use warnings; +use POSIX qw(:signal_h); + +sub connect_oracle { + my ($self, %options) = @_; + + $self->{instance} = DBI->connect( + 'DBI:' . $self->{data_source}, + $self->{username}, + $self->{password}, + { RaiseError => 0, PrintError => 0, AutoCommit => 1, %{$self->{connect_options_hash}} } + ); +} + +sub connect { + my ($self, %options) = @_; + my $dontquit = (defined($options{dontquit}) && $options{dontquit} == 1) ? 1 : 0; + + # Set ENV + if (defined($self->{env})) { + foreach (keys %{$self->{env}}) { + $ENV{$_} = $self->{env}->{$_}; + } + } + + if (defined($self->{timeout})) { + my $mask = POSIX::SigSet->new(SIGALRM); + my $action = POSIX::SigAction->new( + sub { $self->handle_ALRM() }, + $mask, + ); + my $oldaction = POSIX::SigAction->new(); + sigaction(SIGALRM, $action, $oldaction); + eval { + eval { + alarm($self->{timeout}); + $self->connect_oracle(); + }; + alarm(0); + }; + sigaction(SIGALRM, $oldaction); + } else { + $self->connect_oracle(); + } + + if (!defined($self->{instance})) { + my $err_msg = sprintf('Cannot connect: %s', defined($DBI::errstr) ? $DBI::errstr : '(no error string)'); + if ($dontquit == 0) { + $self->{output}->add_option_msg(short_msg => $err_msg); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } + return (-1, $err_msg); + } + + $self->set_version(); + return 0; +} + +1; + +__END__ diff --git a/centreon-plugins/database/oracle/plugin.pm b/centreon-plugins/database/oracle/plugin.pm index 4e0913299..fea0b3c87 100644 --- a/centreon-plugins/database/oracle/plugin.pm +++ b/centreon-plugins/database/oracle/plugin.pm @@ -61,7 +61,8 @@ sub new { 'undo-usage' => 'database::oracle::mode::undotablespace', ); - $self->{sql_modes}{sqlpluscmd} = 'database::oracle::sqlpluscmd'; + $self->{sql_modes}{dbi} = 'database::oracle::dbi'; + $self->{sql_modes}{sqlpluscmd} = 'database::oracle::sqlpluscmd'; return $self; }