From 32f0b49e2966fd48dd6817179ce51bdd9992686f Mon Sep 17 00:00:00 2001 From: "jerome.bolliet@capgemini.com" <root@dbsrv01.labs.local> Date: Mon, 7 May 2018 09:38:57 +0200 Subject: [PATCH 1/4] Add the sqlplus command line mode to oracle database plugin --- centreon-plugins/database/oracle/plugin.pm | 2 + .../database/oracle/sqlpluscmd.pm | 466 ++++++++++++++++++ 2 files changed, 468 insertions(+) create mode 100644 centreon-plugins/database/oracle/sqlpluscmd.pm diff --git a/centreon-plugins/database/oracle/plugin.pm b/centreon-plugins/database/oracle/plugin.pm index 187c3ee12..e35ad8cda 100644 --- a/centreon-plugins/database/oracle/plugin.pm +++ b/centreon-plugins/database/oracle/plugin.pm @@ -55,6 +55,8 @@ sub new { 'tnsping' => 'database::oracle::mode::tnsping', ); + $self->{sql_modes}{sqlpluscmd} = 'database::oracle::sqlpluscmd'; + return $self; } diff --git a/centreon-plugins/database/oracle/sqlpluscmd.pm b/centreon-plugins/database/oracle/sqlpluscmd.pm new file mode 100644 index 000000000..1d87e3184 --- /dev/null +++ b/centreon-plugins/database/oracle/sqlpluscmd.pm @@ -0,0 +1,466 @@ +# +# Copyright 2018 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::sqlpluscmd; + +use strict; +use warnings; +use centreon::plugins::misc; +use Digest::MD5 qw(md5_hex); +use File::Temp qw(tempfile); +use Data::Dumper; + +my $debug = 0; + +sub printd { + my @msg = @_; + + if($debug) { + print @msg; + } +} + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + # $options{options} = options object + # $options{output} = output object + # $options{exit_value} = integer + # $options{noptions} = integer + + if (!defined($options{output})) { + print "Class sqlpluscmd: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class sqlpluscmd: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => + { "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, + "oracle-sid:s" => { name => 'oracle_sid' }, + "oracle-home:s" => { name => 'oracle_home' }, + "tnsadmin-home:s" => { name => 'tnsadmin_home' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "local-connexion" => { name => 'local_connexion', default=>0 }, + "oracle-debug" => { name => 'oracle_debug', default=>0 }, + "sysdba" => { name => 'sysdba', default=>0 }, + "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, + "tempdir:s" => { name => 'tempdir', default => '/tmp' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'sqlpluscmd OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{args} = undef; + $self->{stdout} = undef; + $self->{columns} = undef; + $self->{version} = undef; + + $self->{oracle_sid} = undef; + $self->{oracle_home} = undef; + $self->{tnsadmin_home} = undef; + $self->{local_connexion} = undef; + $self->{sysdba} = undef; + $self->{username} = undef; + $self->{password} = undef; + $self->{tempdir} = undef; + + return $self; +} + +# Method to manage multiples +sub set_options { + my ($self, %options) = @_; + # options{options_result} + + $self->{option_results} = $options{option_results}; +} + +# Method to manage multiples +sub set_defaults { + my ($self, %options) = @_; + # options{default} + + # Manage default value + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + # return 1 = ok still data_source + # return 0 = no data_source left + + $self->{oracle_sid} = $self->{option_results}->{oracle_sid}; + $self->{oracle_home} = $self->{option_results}->{oracle_home}; + $self->{tnsadmin_home} = $self->{option_results}->{tnsadmin_home}; + $self->{username} = $self->{option_results}->{username}; + $self->{password} = $self->{option_results}->{password}; + $self->{local_connexion} = $self->{option_results}->{local_connexion}; + $self->{sysdba} = $self->{option_results}->{sysdba}; + $self->{sql_errors_exit} = $self->{option_results}->{sql_errors_exit}; + $self->{sqlplus_cmd} = $self->{option_results}->{sqlplus_cmd}; + $self->{tempdir} = $self->{option_results}->{tempdir}; + $debug = $self->{option_results}->{oracle_debug}; + + printd "*** DEBUG MODE****\n"; + printd Dumper($self->{option_results}),"\n"; + + if (!defined($self->{oracle_sid}) || $self->{oracle_sid} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify sid argument."); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } + + if (!defined($self->{oracle_home}) && defined($ENV{'ORACLE_HOME'})) { + $self->{oracle_home} = $ENV{'ORACLE_HOME'}; + } + if (!defined($self->{oracle_home}) || $self->{oracle_home} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify oracle-home argument."); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } + + if (!defined($self->{tnsadmin_home}) && defined($ENV{'TNSADMIN'})) { + $self->{tnsadmin_home} = $ENV{'TNSADMIN'}; + } elsif(!defined($self->{tnsadmin_home})) { + $self->{tnsadmin_home} = $self->{oracle_home}."/network/admin"; + } + + if(!$self->{sqlplus_cmd}) { + $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; + } + + $self->{args} = ['-L', '-S']; + my $connection_string = ""; + if($self->{sysdba} == 1) { + printd "*** SYDBA MODE****\n"; + $connection_string="/ as sysdba"; + $self->{local_connexion} = 1; + } elsif(defined($self->{username}) && defined($self->{password})) { + $connection_string=$self->{username}."/".$self->{password}; + } else { + $self->{output}->add_option_msg(short_msg => "Need to specify username/password arguments or sysdba option."); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } + + if($self->{local_connexion} == 0) { + $connection_string .= "\@".$self->{oracle_sid}; + } else { + printd "*** LOCAL CONNEXION MODE****\n"; + $ENV{ORACLE_SID} = $self->{oracle_sid}; + } + + # register a false data_source to be compliant with tnsping mode + $self->{data_source}="sid=".$self->{oracle_sid}; + + push @{$self->{args}}, $connection_string; + + # set oracle env variable + $ENV{ORACLE_HOME} = $self->{oracle_home}; + $ENV{TNSADMIN} = $self->{tnsadmin_home}; + + if (defined($self->{option_results}->{oracle_sid})) { + return 0; + } + return 1; +} + +sub is_version_minimum { + my ($self, %options) = @_; + # $options{version} = string version to check + + my @version_src = split /\./, $self->{version}; + my @versions = split /\./, $options{version}; + for (my $i = 0; $i < scalar(@versions); $i++) { + return 1 if ($versions[$i] eq 'x'); + return 1 if (!defined($version_src[$i])); + $version_src[$i] =~ /^([0-9]*)/; + next if ($versions[$i] == int($1)); + return 0 if ($versions[$i] > int($1)); + return 1 if ($versions[$i] < int($1)); + } + + return 1; +} + +sub get_id { + my ($self, %options) = @_; + + my $msg = $self->{oracle_sid}; + return $msg; +} + + +sub get_unique_id4save { + my ($self, %options) = @_; + + my $msg = $self->{oracle_sid}; + return md5_hex($msg); +} + +sub quote { + my $self = shift; + + return undef; +} + +sub command_execution { + my ($self, %options) = @_; + + my ($fh, $tempfile) = tempfile( DIR => $self->{tempdir}, TEMPLATE => "centreonOracle.".$self->{oracle_sid}.".XXXXXX", UNLINK => 1 ); + print $fh "set echo off +-- set heading off +set feedback off +set linesize 16000 +set pagesize 50000 +set colsep '#&!#' +set numwidth 15 +$options{request}; +exit;"; + + printd "*** COMMAND: ",$self->{sqlplus_cmd},' ',join(' ',(@{$self->{args}},'@', $tempfile)),"\n"; + printd "*** REQUEST: ",$options{request},"\n"; + my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick( + command => $self->{sqlplus_cmd}, + arguments => [@{$self->{args}}, '@', $tempfile], + timeout => 30, + wait_exit => 1, + redirect_stderr => 1 + ); + printd "REQ. STDOUT: '$stdout'\n"; + printd "REQ. EXIT_CODE: $exit_code\n"; + + # search oracle error lines + $exit_code = -1 if($stdout =~ /ORA\-\d+|TNS\-\d+|SP\d\-\d+/); + + if ($exit_code <= -1000) { + if ($exit_code == -1000) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => $stdout); + } + $self->{output}->display(); + $self->{output}->exit(); + } + + return ($exit_code, $stdout); +} + +# Connection initializer +sub connect { + my ($self, %options) = @_; + my $dontquit = (defined($options{dontquit}) && $options{dontquit} == 1) ? 1 : 0; + + (my $exit_code, $self->{stdout}) = $self->command_execution(request => "select version from v\$instance"); + if ($exit_code != 0) { + if ($dontquit == 0) { + $self->{output}->add_option_msg(short_msg => "Cannot connect: " . $self->{stdout}); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } + return (-1, "Cannot connect: " . $self->{stdout}); + } + + $self->{version} = $self->fetchrow_array(); + + printd "VERSION: ",$self->{version},"\n"; + return 0; +} + +sub fetchall_arrayref { + my ($self, %options) = @_; + my $array_ref = []; + + if($self->{stdout} eq '') { + printd "fetchall_arrayref: no data returned (no rows selected)\n"; + return $array_ref; + } + + if (!defined($self->{columns})) { + $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; + my $line = $1; + printd "fetchall_arrayref COLUMNS: $line\n" if(defined($line)); + @{$self->{columns}} = split(/#&!#/, $line); + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + } + foreach (split /\n/, $self->{stdout}) { + my $line = $_; + $line =~ s/^\s+|\s+$//g; + $line =~ s/#&!#\s+/#&!#/g; + $line =~ s/\s+#&!#/#&!#/g; + + printd "fetchall_arrayref VALUE: ",$line,"\n"; + push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))]; + } + return $array_ref; +} + +sub fetchrow_array { + my ($self, %options) = @_; + my @array_result = (); + + if($self->{stdout} eq '') { + printd "fetchrow_array: no data returned (no rows selected)\n"; + return @array_result; + } + + if (!defined($self->{columns})) { + $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; + my $line = $1; + printd "fetchrow_array COLUMNS: $line\n"; + @{$self->{columns}} = split(/#&!#/, $line); + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + } + printd "fetchrow_array STDOUT: '",$self->{stdout},"'\n"; + if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) { + my $line = $1; + printd "fetchrow_array VALUE: '",$line,"'\n"; + push @array_result, map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line)); + map { s/^\s+|\s+$//g; } @array_result; + printd "ARRAY: ",Dumper(@array_result),"\n"; + } + + printd "RETURN: ",Dumper(@array_result),"\n"; + return scalar(@array_result) == 1 ? $array_result[0] : @array_result ; +} + +sub fetchrow_hashref { + my ($self, %options) = @_; + my $array_result = undef; + + if($self->{stdout} eq '') { + printd "fetchrow_hashref: no data returned (no rows selected)\n"; + return $array_result; + } + + if (!defined($self->{columns})) { + $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; + my $line = $1; + printd "fetchrow_hashref COLUMNS: $line\n"; + @{$self->{columns}} = split(/#&!#/, $line); + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + } + if ($self->{stdout} ne '' && $self->{stdout} =~ s/^(.*?)(\n|$)//) { + my $line = $1; + printd "fetchrow_hashref VALUE: ",$line,"\n"; + $array_result = {}; + my @values = split(/#&!#/, $line); + for (my $i = 0; $i < scalar(@values); $i++) { + my $value = $values[$i]; + $value =~ s/^\s+|\s+$//g; + $value =~ s/\\n/\x{0a}/g; + $value =~ s/\\t/\x{09}/g; + $value =~ s/\\/\x{5c}/g; + printd "fetchrow_hashref RES: '",$self->{columns}[$i],"' = '$value'\n"; + $array_result->{$self->{columns}[$i]} = $value; + } + } + + return $array_result; +} + +sub query { + my ($self, %options) = @_; + + $self->{columns} = undef; + (my $exit_code, $self->{stdout}) = $self->command_execution(request => $options{query}); + if ($exit_code != 0) { + $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{stdout}); + $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + } +} + +1; + +__END__ + +=head1 NAME + +sqlpluscmd global + +=head1 SYNOPSIS + +sqlpluscmd class + +=head1 sqlpluscmd OPTIONS + +=over 8 + +=item B<--sqlplus-cmd> + +sqlplus command (Default: 'sqlplus'). + +=item B<--sid> + +Database Oracle ID (SID). + +=item B<--oracle-home> + +Oracle Database Server Home. + +=item B<--tnsadmin-home> + +Oracle TNS Admin Home. Where to locate tnsnames.ora file (default: ${ORACLE_HOME}/network/admin) + +=item B<--username> + +Database username. + +=item B<--password> + +Database password. + +=item B<--sysdba> + +Use a sysdba connexion, need to be execute under the oracle local user on the server, and use a local connexion. + +=item B<--local-connexion> + +Use a local connexion, don't need listener. + +=item B<--oracle-debug> + +Active the debug mode in the plugin (displaying request, output, etc.) + +=item B<--sql-errors-exit> + +Exit code for DB Errors (default: unknown) + +=back + +=head1 DESCRIPTION + +B<snmp>. + +=cut From 4be51e2ed727836b972e753eaf39f1e767f5b791 Mon Sep 17 00:00:00 2001 From: "jerome.bolliet@capgemini.com" <root@dbsrv01.labs.local> Date: Mon, 21 May 2018 12:38:08 +0200 Subject: [PATCH 2/4] Change to fit the coding guidelines Add the possibility to connect with tnsnames.ora entry (hostname/port/sid) --- centreon-plugins/database/oracle/plugin.pm | 5 + .../database/oracle/sqlpluscmd.pm | 167 ++++++++---------- 2 files changed, 79 insertions(+), 93 deletions(-) diff --git a/centreon-plugins/database/oracle/plugin.pm b/centreon-plugins/database/oracle/plugin.pm index e35ad8cda..43c3cd982 100644 --- a/centreon-plugins/database/oracle/plugin.pm +++ b/centreon-plugins/database/oracle/plugin.pm @@ -23,6 +23,7 @@ package database::oracle::plugin; use strict; use warnings; use base qw(centreon::plugins::script_sql); +use Data::Dumper; sub new { my ($class, %options) = @_; @@ -76,13 +77,17 @@ sub init { if (defined($options_result->{hostname})) { @{$self->{sqldefault}->{dbi}} = (); + @{$self->{sqldefault}->{sqlpluscmd}} = (); for (my $i = 0; $i < scalar(@{$options_result->{hostname}}); $i++) { $self->{sqldefault}->{dbi}[$i] = { data_source => 'Oracle:host=' . $options_result->{hostname}[$i] }; + $self->{sqldefault}->{sqlpluscmd}[$i] = { hostname => $options_result->{hostname}[$i] }; if (defined($options_result->{port}[$i])) { $self->{sqldefault}->{dbi}[$i]->{data_source} .= ';port=' . $options_result->{port}[$i]; + $self->{sqldefault}->{sqlpluscmd}[$i]->{port} = $options_result->{port}[$i]; } if ((defined($options_result->{sid})) && ($options_result->{sid} ne '')) { $self->{sqldefault}->{dbi}[$i]->{data_source} .= ';sid=' . $options_result->{sid}; + $self->{sqldefault}->{sqlpluscmd}[$i]->{sid} = $options_result->{sid}; } } } diff --git a/centreon-plugins/database/oracle/sqlpluscmd.pm b/centreon-plugins/database/oracle/sqlpluscmd.pm index 1d87e3184..2030c0a03 100644 --- a/centreon-plugins/database/oracle/sqlpluscmd.pm +++ b/centreon-plugins/database/oracle/sqlpluscmd.pm @@ -27,16 +27,6 @@ use Digest::MD5 qw(md5_hex); use File::Temp qw(tempfile); use Data::Dumper; -my $debug = 0; - -sub printd { - my @msg = @_; - - if($debug) { - print @msg; - } -} - sub new { my ($class, %options) = @_; my $self = {}; @@ -55,20 +45,21 @@ sub new { $options{output}->option_exit(); } if (!defined($options{noptions})) { - $options{options}->add_options(arguments => - { "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, - "oracle-sid:s" => { name => 'oracle_sid' }, - "oracle-home:s" => { name => 'oracle_home' }, - "tnsadmin-home:s" => { name => 'tnsadmin_home' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "local-connexion" => { name => 'local_connexion', default=>0 }, - "oracle-debug" => { name => 'oracle_debug', default=>0 }, - "sysdba" => { name => 'sysdba', default=>0 }, - "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, - "tempdir:s" => { name => 'tempdir', default => '/tmp' }, - }); - } + $options{options}->add_options( + arguments => { + "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, + "oracle-home:s" => { name => 'oracle_home' }, + "tnsadmin-home:s" => { name => 'tnsadmin_home' }, + "tnsnames-sid:s" => { name => 'tnsnames_sid'}, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "local-connexion" => { name => 'local_connexion', default=>0 }, + "sysdba" => { name => 'sysdba', default=>0 }, + "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, + "tempdir:s" => { name => 'tempdir', default => '/tmp' }, + } + ); + } $options{options}->add_help(package => __PACKAGE__, sections => 'sqlpluscmd OPTIONS', once => 1); $self->{output} = $options{output}; @@ -78,14 +69,10 @@ sub new { $self->{columns} = undef; $self->{version} = undef; - $self->{oracle_sid} = undef; + $self->{sid} = undef; $self->{oracle_home} = undef; $self->{tnsadmin_home} = undef; $self->{local_connexion} = undef; - $self->{sysdba} = undef; - $self->{username} = undef; - $self->{password} = undef; - $self->{tempdir} = undef; return $self; } @@ -122,66 +109,64 @@ sub check_options { # return 1 = ok still data_source # return 0 = no data_source left - $self->{oracle_sid} = $self->{option_results}->{oracle_sid}; - $self->{oracle_home} = $self->{option_results}->{oracle_home}; - $self->{tnsadmin_home} = $self->{option_results}->{tnsadmin_home}; - $self->{username} = $self->{option_results}->{username}; - $self->{password} = $self->{option_results}->{password}; + $self->{sid} = defined($self->{option_results}->{sid}[0]) ? $self->{option_results}->{sid}[0]: $self->{option_results}->{tnsnames_sid}; + $self->{oracle_home} = defined($self->{option_results}->{oracle_home}) ? $self->{option_results}->{oracle_home} : $ENV{'ORACLE_HOME'}; + $self->{tnsadmin_home} = defined($self->{option_results}->{tnsadmin_home}) ? $self->{option_results}->{tnsadmin_home} : $ENV{'TNSADMIN'}; $self->{local_connexion} = $self->{option_results}->{local_connexion}; - $self->{sysdba} = $self->{option_results}->{sysdba}; - $self->{sql_errors_exit} = $self->{option_results}->{sql_errors_exit}; $self->{sqlplus_cmd} = $self->{option_results}->{sqlplus_cmd}; - $self->{tempdir} = $self->{option_results}->{tempdir}; - $debug = $self->{option_results}->{oracle_debug}; - printd "*** DEBUG MODE****\n"; - printd Dumper($self->{option_results}),"\n"; + $self->{output}->output_add(long_msg=>"*** DEBUG MODE****\n"); + $self->{output}->output_add(long_msg=>Dumper($self->{option_results}));; - if (!defined($self->{oracle_sid}) || $self->{oracle_sid} eq '') { + # check the SID prerequisite option + if (!defined($self->{sid}) || $self->{sid} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify sid argument."); - $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } - if (!defined($self->{oracle_home}) && defined($ENV{'ORACLE_HOME'})) { - $self->{oracle_home} = $ENV{'ORACLE_HOME'}; - } + # check the ORACLE_HOME variable if (!defined($self->{oracle_home}) || $self->{oracle_home} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify oracle-home argument."); - $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } - - if (!defined($self->{tnsadmin_home}) && defined($ENV{'TNSADMIN'})) { - $self->{tnsadmin_home} = $ENV{'TNSADMIN'}; - } elsif(!defined($self->{tnsadmin_home})) { + + # construct the TNSADMIN variable if not available + if(!defined($self->{tnsadmin_home})) { $self->{tnsadmin_home} = $self->{oracle_home}."/network/admin"; } + # check the SQLPLUS command to use if(!$self->{sqlplus_cmd}) { $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; } $self->{args} = ['-L', '-S']; my $connection_string = ""; - if($self->{sysdba} == 1) { - printd "*** SYDBA MODE****\n"; + if($self->{option_results}->{sysdba} == 1) { + $self->{output}->output_add(long_msg=>"*** SYDBA MODE****\n"); $connection_string="/ as sysdba"; $self->{local_connexion} = 1; - } elsif(defined($self->{username}) && defined($self->{password})) { - $connection_string=$self->{username}."/".$self->{password}; + } elsif(defined($self->{option_results}->{username}) && defined($self->{option_results}->{password})) { + $connection_string=$self->{option_results}->{username}."/".$self->{option_results}->{password}; } else { $self->{output}->add_option_msg(short_msg => "Need to specify username/password arguments or sysdba option."); - $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } if($self->{local_connexion} == 0) { - $connection_string .= "\@".$self->{oracle_sid}; + if(defined($self->{option_results}->{hostname})) { + my $port = defined($self->{option_results}->{port}) ? $self->{option_results}->{port}[0] : 1521; + $connection_string .= "\@//".$self->{option_results}->{hostname}[0].":".$port."/".$self->{sid}; + } else { + $connection_string .= "\@".$self->{sid}; + } } else { - printd "*** LOCAL CONNEXION MODE****\n"; - $ENV{ORACLE_SID} = $self->{oracle_sid}; + $self->{output}->output_add(long_msg=>"*** LOCAL CONNEXION MODE****\n"); + $ENV{ORACLE_SID} = $self->{sid}; } # register a false data_source to be compliant with tnsping mode - $self->{data_source}="sid=".$self->{oracle_sid}; + $self->{data_source}="sid=".$self->{sid}; push @{$self->{args}}, $connection_string; @@ -189,7 +174,7 @@ sub check_options { $ENV{ORACLE_HOME} = $self->{oracle_home}; $ENV{TNSADMIN} = $self->{tnsadmin_home}; - if (defined($self->{option_results}->{oracle_sid})) { + if (defined($self->{option_results}->{sid})) { return 0; } return 1; @@ -216,7 +201,7 @@ sub is_version_minimum { sub get_id { my ($self, %options) = @_; - my $msg = $self->{oracle_sid}; + my $msg = $self->{sid}; return $msg; } @@ -224,7 +209,7 @@ sub get_id { sub get_unique_id4save { my ($self, %options) = @_; - my $msg = $self->{oracle_sid}; + my $msg = $self->{sid}; return md5_hex($msg); } @@ -237,7 +222,7 @@ sub quote { sub command_execution { my ($self, %options) = @_; - my ($fh, $tempfile) = tempfile( DIR => $self->{tempdir}, TEMPLATE => "centreonOracle.".$self->{oracle_sid}.".XXXXXX", UNLINK => 1 ); + my ($fh, $tempfile) = tempfile( DIR => $self->{option_results}->{tempdir}, TEMPLATE => "centreonOracle.".$self->{sid}.".XXXXXX", UNLINK => 1 ); print $fh "set echo off -- set heading off set feedback off @@ -248,8 +233,8 @@ set numwidth 15 $options{request}; exit;"; - printd "*** COMMAND: ",$self->{sqlplus_cmd},' ',join(' ',(@{$self->{args}},'@', $tempfile)),"\n"; - printd "*** REQUEST: ",$options{request},"\n"; + $self->{output}->output_add(long_msg=>"*** COMMAND: ".$self->{sqlplus_cmd}.' '.join(' ',(@{$self->{args}},'@', $tempfile))."\n"); + $self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n"); my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick( command => $self->{sqlplus_cmd}, arguments => [@{$self->{args}}, '@', $tempfile], @@ -257,11 +242,11 @@ exit;"; wait_exit => 1, redirect_stderr => 1 ); - printd "REQ. STDOUT: '$stdout'\n"; - printd "REQ. EXIT_CODE: $exit_code\n"; + $self->{output}->output_add(long_msg=>"REQ. STDOUT: '$stdout'\n"); + $self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n"); # search oracle error lines - $exit_code = -1 if($stdout =~ /ORA\-\d+|TNS\-\d+|SP\d\-\d+/); + $exit_code = -1 if($stdout =~ /^(ORA\-\d+|TNS\-\d+|SP\d\-\d+)/); if ($exit_code <= -1000) { if ($exit_code == -1000) { @@ -284,14 +269,14 @@ sub connect { if ($exit_code != 0) { if ($dontquit == 0) { $self->{output}->add_option_msg(short_msg => "Cannot connect: " . $self->{stdout}); - $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } return (-1, "Cannot connect: " . $self->{stdout}); } $self->{version} = $self->fetchrow_array(); - printd "VERSION: ",$self->{version},"\n"; + $self->{output}->output_add(long_msg=>"VERSION: ".$self->{version}."\n"); return 0; } @@ -300,14 +285,14 @@ sub fetchall_arrayref { my $array_ref = []; if($self->{stdout} eq '') { - printd "fetchall_arrayref: no data returned (no rows selected)\n"; + $self->{output}->output_add(long_msg=>"fetchall_arrayref: no data returned (no rows selected)\n"); return $array_ref; } if (!defined($self->{columns})) { $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; my $line = $1; - printd "fetchall_arrayref COLUMNS: $line\n" if(defined($line)); + $self->{output}->output_add(long_msg=>"fetchall_arrayref COLUMNS: $line\n") if(defined($line)); @{$self->{columns}} = split(/#&!#/, $line); map { s/^\s+|\s+$//g; } @{$self->{columns}}; $self->{stdout} =~ s/[\-#&!]+(\n|$)//; @@ -318,7 +303,7 @@ sub fetchall_arrayref { $line =~ s/#&!#\s+/#&!#/g; $line =~ s/\s+#&!#/#&!#/g; - printd "fetchall_arrayref VALUE: ",$line,"\n"; + $self->{output}->output_add(long_msg=>"fetchall_arrayref VALUE: ".$line."\n"); push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))]; } return $array_ref; @@ -329,28 +314,28 @@ sub fetchrow_array { my @array_result = (); if($self->{stdout} eq '') { - printd "fetchrow_array: no data returned (no rows selected)\n"; + $self->{output}->output_add("fetchrow_array: no data returned (no rows selected)\n"); return @array_result; } if (!defined($self->{columns})) { $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; my $line = $1; - printd "fetchrow_array COLUMNS: $line\n"; + $self->{output}->output_add(long_msg=>"fetchrow_array COLUMNS: $line\n"); @{$self->{columns}} = split(/#&!#/, $line); map { s/^\s+|\s+$//g; } @{$self->{columns}}; $self->{stdout} =~ s/[\-#&!]+(\n|$)//; } - printd "fetchrow_array STDOUT: '",$self->{stdout},"'\n"; + $self->{output}->output_add(long_msg=>"fetchrow_array STDOUT: '".$self->{stdout}."'\n"); if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) { my $line = $1; - printd "fetchrow_array VALUE: '",$line,"'\n"; + $self->{output}->output_add(long_msg=>"fetchrow_array VALUE: '".$line."'\n"); push @array_result, map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line)); map { s/^\s+|\s+$//g; } @array_result; - printd "ARRAY: ",Dumper(@array_result),"\n"; + $self->{output}->output_add(long_msg=>"ARRAY: ".Dumper(@array_result)."\n"); } - printd "RETURN: ",Dumper(@array_result),"\n"; + $self->{output}->output_add(long_msg=>"RETURN: ".Dumper(@array_result)."\n"); return scalar(@array_result) == 1 ? $array_result[0] : @array_result ; } @@ -359,21 +344,21 @@ sub fetchrow_hashref { my $array_result = undef; if($self->{stdout} eq '') { - printd "fetchrow_hashref: no data returned (no rows selected)\n"; + $self->{output}->output_add(long_msg=>"fetchrow_hashref: no data returned (no rows selected)\n"); return $array_result; } if (!defined($self->{columns})) { $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; my $line = $1; - printd "fetchrow_hashref COLUMNS: $line\n"; + $self->{output}->output_add(long_msg=>"fetchrow_hashref COLUMNS: $line\n"); @{$self->{columns}} = split(/#&!#/, $line); map { s/^\s+|\s+$//g; } @{$self->{columns}}; $self->{stdout} =~ s/[\-#&!]+(\n|$)//; } if ($self->{stdout} ne '' && $self->{stdout} =~ s/^(.*?)(\n|$)//) { my $line = $1; - printd "fetchrow_hashref VALUE: ",$line,"\n"; + $self->{output}->output_add(long_msg=>"fetchrow_hashref VALUE: ".$line."\n"); $array_result = {}; my @values = split(/#&!#/, $line); for (my $i = 0; $i < scalar(@values); $i++) { @@ -382,7 +367,7 @@ sub fetchrow_hashref { $value =~ s/\\n/\x{0a}/g; $value =~ s/\\t/\x{09}/g; $value =~ s/\\/\x{5c}/g; - printd "fetchrow_hashref RES: '",$self->{columns}[$i],"' = '$value'\n"; + $self->{output}->output_add(long_msg=>"fetchrow_hashref RES: '".$self->{columns}[$i]."' = '$value'\n"); $array_result->{$self->{columns}[$i]} = $value; } } @@ -396,8 +381,8 @@ sub query { $self->{columns} = undef; (my $exit_code, $self->{stdout}) = $self->command_execution(request => $options{query}); if ($exit_code != 0) { - $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{stdout}); - $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); + $self->{output}->add_option_msg(short_msg => "Cannot execute query:\nQuery:".$options{query}."\nOutput: ". $self->{stdout}); + $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } } @@ -421,10 +406,6 @@ sqlpluscmd class sqlplus command (Default: 'sqlplus'). -=item B<--sid> - -Database Oracle ID (SID). - =item B<--oracle-home> Oracle Database Server Home. @@ -433,6 +414,10 @@ Oracle Database Server Home. Oracle TNS Admin Home. Where to locate tnsnames.ora file (default: ${ORACLE_HOME}/network/admin) +=item B<--tnsnames-sid> + +Oracle SID defined in tnsnames.ora. + =item B<--username> Database username. @@ -449,10 +434,6 @@ Use a sysdba connexion, need to be execute under the oracle local user on the se Use a local connexion, don't need listener. -=item B<--oracle-debug> - -Active the debug mode in the plugin (displaying request, output, etc.) - =item B<--sql-errors-exit> Exit code for DB Errors (default: unknown) From 7c2174e16a16ea032a54664047bf5fdb0b6442eb Mon Sep 17 00:00:00 2001 From: "jerome.bolliet@capgemini.com" <root@dbsrv01.labs.local> Date: Mon, 21 May 2018 12:42:05 +0200 Subject: [PATCH 3/4] Replace tab by spaces --- .../database/oracle/sqlpluscmd.pm | 250 +++++++++--------- 1 file changed, 125 insertions(+), 125 deletions(-) diff --git a/centreon-plugins/database/oracle/sqlpluscmd.pm b/centreon-plugins/database/oracle/sqlpluscmd.pm index 2030c0a03..ec4243c56 100644 --- a/centreon-plugins/database/oracle/sqlpluscmd.pm +++ b/centreon-plugins/database/oracle/sqlpluscmd.pm @@ -46,20 +46,20 @@ sub new { } if (!defined($options{noptions})) { $options{options}->add_options( - arguments => { - "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, - "oracle-home:s" => { name => 'oracle_home' }, - "tnsadmin-home:s" => { name => 'tnsadmin_home' }, - "tnsnames-sid:s" => { name => 'tnsnames_sid'}, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "local-connexion" => { name => 'local_connexion', default=>0 }, - "sysdba" => { name => 'sysdba', default=>0 }, - "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, - "tempdir:s" => { name => 'tempdir', default => '/tmp' }, - } - ); - } + arguments => { + "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, + "oracle-home:s" => { name => 'oracle_home' }, + "tnsadmin-home:s" => { name => 'tnsadmin_home' }, + "tnsnames-sid:s" => { name => 'tnsnames_sid'}, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "local-connexion" => { name => 'local_connexion', default=>0 }, + "sysdba" => { name => 'sysdba', default=>0 }, + "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, + "tempdir:s" => { name => 'tempdir', default => '/tmp' }, + } + ); + } $options{options}->add_help(package => __PACKAGE__, sections => 'sqlpluscmd OPTIONS', once => 1); $self->{output} = $options{output}; @@ -114,66 +114,66 @@ sub check_options { $self->{tnsadmin_home} = defined($self->{option_results}->{tnsadmin_home}) ? $self->{option_results}->{tnsadmin_home} : $ENV{'TNSADMIN'}; $self->{local_connexion} = $self->{option_results}->{local_connexion}; $self->{sqlplus_cmd} = $self->{option_results}->{sqlplus_cmd}; - - $self->{output}->output_add(long_msg=>"*** DEBUG MODE****\n"); - $self->{output}->output_add(long_msg=>Dumper($self->{option_results}));; + + $self->{output}->output_add(long_msg=>"*** DEBUG MODE****\n"); + $self->{output}->output_add(long_msg=>Dumper($self->{option_results}));; - # check the SID prerequisite option + # check the SID prerequisite option if (!defined($self->{sid}) || $self->{sid} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify sid argument."); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } - # check the ORACLE_HOME variable + # check the ORACLE_HOME variable if (!defined($self->{oracle_home}) || $self->{oracle_home} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify oracle-home argument."); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } - - # construct the TNSADMIN variable if not available - if(!defined($self->{tnsadmin_home})) { - $self->{tnsadmin_home} = $self->{oracle_home}."/network/admin"; - } - - # check the SQLPLUS command to use - if(!$self->{sqlplus_cmd}) { - $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; - } - + + # construct the TNSADMIN variable if not available + if(!defined($self->{tnsadmin_home})) { + $self->{tnsadmin_home} = $self->{oracle_home}."/network/admin"; + } + + # check the SQLPLUS command to use + if(!$self->{sqlplus_cmd}) { + $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; + } + $self->{args} = ['-L', '-S']; - my $connection_string = ""; - if($self->{option_results}->{sysdba} == 1) { - $self->{output}->output_add(long_msg=>"*** SYDBA MODE****\n"); - $connection_string="/ as sysdba"; - $self->{local_connexion} = 1; - } elsif(defined($self->{option_results}->{username}) && defined($self->{option_results}->{password})) { - $connection_string=$self->{option_results}->{username}."/".$self->{option_results}->{password}; - } else { + my $connection_string = ""; + if($self->{option_results}->{sysdba} == 1) { + $self->{output}->output_add(long_msg=>"*** SYDBA MODE****\n"); + $connection_string="/ as sysdba"; + $self->{local_connexion} = 1; + } elsif(defined($self->{option_results}->{username}) && defined($self->{option_results}->{password})) { + $connection_string=$self->{option_results}->{username}."/".$self->{option_results}->{password}; + } else { $self->{output}->add_option_msg(short_msg => "Need to specify username/password arguments or sysdba option."); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); - } + } - if($self->{local_connexion} == 0) { - if(defined($self->{option_results}->{hostname})) { - my $port = defined($self->{option_results}->{port}) ? $self->{option_results}->{port}[0] : 1521; - $connection_string .= "\@//".$self->{option_results}->{hostname}[0].":".$port."/".$self->{sid}; - } else { - $connection_string .= "\@".$self->{sid}; - } - } else { - $self->{output}->output_add(long_msg=>"*** LOCAL CONNEXION MODE****\n"); - $ENV{ORACLE_SID} = $self->{sid}; - } - - # register a false data_source to be compliant with tnsping mode - $self->{data_source}="sid=".$self->{sid}; - - push @{$self->{args}}, $connection_string; - - # set oracle env variable - $ENV{ORACLE_HOME} = $self->{oracle_home}; - $ENV{TNSADMIN} = $self->{tnsadmin_home}; - + if($self->{local_connexion} == 0) { + if(defined($self->{option_results}->{hostname})) { + my $port = defined($self->{option_results}->{port}) ? $self->{option_results}->{port}[0] : 1521; + $connection_string .= "\@//".$self->{option_results}->{hostname}[0].":".$port."/".$self->{sid}; + } else { + $connection_string .= "\@".$self->{sid}; + } + } else { + $self->{output}->output_add(long_msg=>"*** LOCAL CONNEXION MODE****\n"); + $ENV{ORACLE_SID} = $self->{sid}; + } + + # register a false data_source to be compliant with tnsping mode + $self->{data_source}="sid=".$self->{sid}; + + push @{$self->{args}}, $connection_string; + + # set oracle env variable + $ENV{ORACLE_HOME} = $self->{oracle_home}; + $ENV{TNSADMIN} = $self->{tnsadmin_home}; + if (defined($self->{option_results}->{sid})) { return 0; } @@ -222,8 +222,8 @@ sub quote { sub command_execution { my ($self, %options) = @_; - my ($fh, $tempfile) = tempfile( DIR => $self->{option_results}->{tempdir}, TEMPLATE => "centreonOracle.".$self->{sid}.".XXXXXX", UNLINK => 1 ); - print $fh "set echo off + my ($fh, $tempfile) = tempfile( DIR => $self->{option_results}->{tempdir}, TEMPLATE => "centreonOracle.".$self->{sid}.".XXXXXX", UNLINK => 1 ); + print $fh "set echo off -- set heading off set feedback off set linesize 16000 @@ -232,9 +232,9 @@ set colsep '#&!#' set numwidth 15 $options{request}; exit;"; - - $self->{output}->output_add(long_msg=>"*** COMMAND: ".$self->{sqlplus_cmd}.' '.join(' ',(@{$self->{args}},'@', $tempfile))."\n"); - $self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n"); + + $self->{output}->output_add(long_msg=>"*** COMMAND: ".$self->{sqlplus_cmd}.' '.join(' ',(@{$self->{args}},'@', $tempfile))."\n"); + $self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n"); my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick( command => $self->{sqlplus_cmd}, arguments => [@{$self->{args}}, '@', $tempfile], @@ -242,12 +242,12 @@ exit;"; wait_exit => 1, redirect_stderr => 1 ); - $self->{output}->output_add(long_msg=>"REQ. STDOUT: '$stdout'\n"); - $self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n"); - - # search oracle error lines - $exit_code = -1 if($stdout =~ /^(ORA\-\d+|TNS\-\d+|SP\d\-\d+)/); - + $self->{output}->output_add(long_msg=>"REQ. STDOUT: '$stdout'\n"); + $self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n"); + + # search oracle error lines + $exit_code = -1 if($stdout =~ /^(ORA\-\d+|TNS\-\d+|SP\d\-\d+)/); + if ($exit_code <= -1000) { if ($exit_code == -1000) { $self->{output}->output_add(severity => 'UNKNOWN', @@ -275,8 +275,8 @@ sub connect { } $self->{version} = $self->fetchrow_array(); - - $self->{output}->output_add(long_msg=>"VERSION: ".$self->{version}."\n"); + + $self->{output}->output_add(long_msg=>"VERSION: ".$self->{version}."\n"); return 0; } @@ -284,27 +284,27 @@ sub fetchall_arrayref { my ($self, %options) = @_; my $array_ref = []; - if($self->{stdout} eq '') { - $self->{output}->output_add(long_msg=>"fetchall_arrayref: no data returned (no rows selected)\n"); - return $array_ref; - } - + if($self->{stdout} eq '') { + $self->{output}->output_add(long_msg=>"fetchall_arrayref: no data returned (no rows selected)\n"); + return $array_ref; + } + if (!defined($self->{columns})) { $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; - my $line = $1; - $self->{output}->output_add(long_msg=>"fetchall_arrayref COLUMNS: $line\n") if(defined($line)); + my $line = $1; + $self->{output}->output_add(long_msg=>"fetchall_arrayref COLUMNS: $line\n") if(defined($line)); @{$self->{columns}} = split(/#&!#/, $line); - map { s/^\s+|\s+$//g; } @{$self->{columns}}; - $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; } foreach (split /\n/, $self->{stdout}) { - my $line = $_; - $line =~ s/^\s+|\s+$//g; - $line =~ s/#&!#\s+/#&!#/g; - $line =~ s/\s+#&!#/#&!#/g; - - $self->{output}->output_add(long_msg=>"fetchall_arrayref VALUE: ".$line."\n"); - push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))]; + my $line = $_; + $line =~ s/^\s+|\s+$//g; + $line =~ s/#&!#\s+/#&!#/g; + $line =~ s/\s+#&!#/#&!#/g; + + $self->{output}->output_add(long_msg=>"fetchall_arrayref VALUE: ".$line."\n"); + push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))]; } return $array_ref; } @@ -312,62 +312,62 @@ sub fetchall_arrayref { sub fetchrow_array { my ($self, %options) = @_; my @array_result = (); - - if($self->{stdout} eq '') { - $self->{output}->output_add("fetchrow_array: no data returned (no rows selected)\n"); - return @array_result; - } - - if (!defined($self->{columns})) { - $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; - my $line = $1; - $self->{output}->output_add(long_msg=>"fetchrow_array COLUMNS: $line\n"); - @{$self->{columns}} = split(/#&!#/, $line); - map { s/^\s+|\s+$//g; } @{$self->{columns}}; - $self->{stdout} =~ s/[\-#&!]+(\n|$)//; - } - $self->{output}->output_add(long_msg=>"fetchrow_array STDOUT: '".$self->{stdout}."'\n"); - if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) { - my $line = $1; - $self->{output}->output_add(long_msg=>"fetchrow_array VALUE: '".$line."'\n"); - push @array_result, map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line)); - map { s/^\s+|\s+$//g; } @array_result; - $self->{output}->output_add(long_msg=>"ARRAY: ".Dumper(@array_result)."\n"); + + if($self->{stdout} eq '') { + $self->{output}->output_add("fetchrow_array: no data returned (no rows selected)\n"); + return @array_result; } - $self->{output}->output_add(long_msg=>"RETURN: ".Dumper(@array_result)."\n"); - return scalar(@array_result) == 1 ? $array_result[0] : @array_result ; + if (!defined($self->{columns})) { + $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; + my $line = $1; + $self->{output}->output_add(long_msg=>"fetchrow_array COLUMNS: $line\n"); + @{$self->{columns}} = split(/#&!#/, $line); + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + } + $self->{output}->output_add(long_msg=>"fetchrow_array STDOUT: '".$self->{stdout}."'\n"); + if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) { + my $line = $1; + $self->{output}->output_add(long_msg=>"fetchrow_array VALUE: '".$line."'\n"); + push @array_result, map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line)); + map { s/^\s+|\s+$//g; } @array_result; + $self->{output}->output_add(long_msg=>"ARRAY: ".Dumper(@array_result)."\n"); + } + + $self->{output}->output_add(long_msg=>"RETURN: ".Dumper(@array_result)."\n"); + return scalar(@array_result) == 1 ? $array_result[0] : @array_result ; } sub fetchrow_hashref { my ($self, %options) = @_; my $array_result = undef; - if($self->{stdout} eq '') { - $self->{output}->output_add(long_msg=>"fetchrow_hashref: no data returned (no rows selected)\n"); - return $array_result; - } - + if($self->{stdout} eq '') { + $self->{output}->output_add(long_msg=>"fetchrow_hashref: no data returned (no rows selected)\n"); + return $array_result; + } + if (!defined($self->{columns})) { $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; - my $line = $1; - $self->{output}->output_add(long_msg=>"fetchrow_hashref COLUMNS: $line\n"); + my $line = $1; + $self->{output}->output_add(long_msg=>"fetchrow_hashref COLUMNS: $line\n"); @{$self->{columns}} = split(/#&!#/, $line); - map { s/^\s+|\s+$//g; } @{$self->{columns}}; - $self->{stdout} =~ s/[\-#&!]+(\n|$)//; + map { s/^\s+|\s+$//g; } @{$self->{columns}}; + $self->{stdout} =~ s/[\-#&!]+(\n|$)//; } if ($self->{stdout} ne '' && $self->{stdout} =~ s/^(.*?)(\n|$)//) { - my $line = $1; - $self->{output}->output_add(long_msg=>"fetchrow_hashref VALUE: ".$line."\n"); + my $line = $1; + $self->{output}->output_add(long_msg=>"fetchrow_hashref VALUE: ".$line."\n"); $array_result = {}; my @values = split(/#&!#/, $line); for (my $i = 0; $i < scalar(@values); $i++) { my $value = $values[$i]; - $value =~ s/^\s+|\s+$//g; + $value =~ s/^\s+|\s+$//g; $value =~ s/\\n/\x{0a}/g; $value =~ s/\\t/\x{09}/g; $value =~ s/\\/\x{5c}/g; - $self->{output}->output_add(long_msg=>"fetchrow_hashref RES: '".$self->{columns}[$i]."' = '$value'\n"); + $self->{output}->output_add(long_msg=>"fetchrow_hashref RES: '".$self->{columns}[$i]."' = '$value'\n"); $array_result->{$self->{columns}[$i]} = $value; } } From c6ce7f47f61e2f6c5f236826dc6805fd0d7d9900 Mon Sep 17 00:00:00 2001 From: Jerome <Jerome@obiwan> Date: Fri, 25 May 2018 17:38:31 +0200 Subject: [PATCH 4/4] Define a SUFFIX arg with tempfile function and remove unused TEMPLATE arg --- centreon-plugins/database/oracle/sqlpluscmd.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/centreon-plugins/database/oracle/sqlpluscmd.pm b/centreon-plugins/database/oracle/sqlpluscmd.pm index ec4243c56..5bcc64a26 100644 --- a/centreon-plugins/database/oracle/sqlpluscmd.pm +++ b/centreon-plugins/database/oracle/sqlpluscmd.pm @@ -222,7 +222,7 @@ sub quote { sub command_execution { my ($self, %options) = @_; - my ($fh, $tempfile) = tempfile( DIR => $self->{option_results}->{tempdir}, TEMPLATE => "centreonOracle.".$self->{sid}.".XXXXXX", UNLINK => 1 ); + my ($fh, $tempfile) = tempfile( DIR => $self->{option_results}->{tempdir}, SUFFIX => ".sql", UNLINK => 1 ); print $fh "set echo off -- set heading off set feedback off