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; } }