clean sqlplus

This commit is contained in:
garnier-quentin 2019-02-20 16:29:32 +01:00
parent f6ce0d57c8
commit c91fb92a2f
2 changed files with 56 additions and 57 deletions

View File

@ -23,7 +23,6 @@ package database::oracle::plugin;
use strict; use strict;
use warnings; use warnings;
use base qw(centreon::plugins::script_sql); use base qw(centreon::plugins::script_sql);
use Data::Dumper;
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;

View File

@ -115,8 +115,8 @@ sub check_options {
$self->{local_connexion} = $self->{option_results}->{local_connexion}; $self->{local_connexion} = $self->{option_results}->{local_connexion};
$self->{sqlplus_cmd} = $self->{option_results}->{sqlplus_cmd}; $self->{sqlplus_cmd} = $self->{option_results}->{sqlplus_cmd};
$self->{output}->output_add(long_msg=>"*** DEBUG MODE****\n"); $self->{output}->output_add(long_msg => "*** DEBUG MODE****\n", debug => 1);
$self->{output}->output_add(long_msg=>Dumper($self->{option_results}));; $self->{output}->output_add(long_msg => Data::Dumper::Dumper($self->{option_results}), debug => 1);
# check the SID prerequisite option # check the SID prerequisite option
if (!defined($self->{sid}) || $self->{sid} eq '') { if (!defined($self->{sid}) || $self->{sid} eq '') {
@ -143,7 +143,7 @@ sub check_options {
$self->{args} = ['-L', '-S']; $self->{args} = ['-L', '-S'];
my $connection_string = ""; my $connection_string = "";
if ($self->{option_results}->{sysdba} == 1) { if ($self->{option_results}->{sysdba} == 1) {
$self->{output}->output_add(long_msg=>"*** SYDBA MODE****\n"); $self->{output}->output_add(long_msg => "*** SYDBA MODE****", debug => 1);
$connection_string="/ as sysdba"; $connection_string="/ as sysdba";
$self->{local_connexion} = 1; $self->{local_connexion} = 1;
} elsif (defined($self->{option_results}->{username}) && defined($self->{option_results}->{password})) { } elsif (defined($self->{option_results}->{username}) && defined($self->{option_results}->{password})) {
@ -161,7 +161,7 @@ sub check_options {
$connection_string .= "\@" . $self->{sid}; $connection_string .= "\@" . $self->{sid};
} }
} else { } else {
$self->{output}->output_add(long_msg=>"*** LOCAL CONNEXION MODE****\n"); $self->{output}->output_add(long_msg => "*** LOCAL CONNEXION MODE****", debug => 1);
$ENV{ORACLE_SID} = $self->{sid}; $ENV{ORACLE_SID} = $self->{sid};
} }
@ -233,8 +233,8 @@ set numwidth 15
$options{request}; $options{request};
exit;"; exit;";
$self->{output}->output_add(long_msg=>"*** COMMAND: ".$self->{sqlplus_cmd}.' '.join(' ',(@{$self->{args}},'@', $tempfile))."\n"); $self->{output}->output_add(long_msg => "*** COMMAND: " . $self->{sqlplus_cmd} . ' ' . join(' ', (@{$self->{args}}, '@', $tempfile)), debug => 1);
$self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n"); $self->{output}->output_add(long_msg => "*** REQUEST: " . $options{request}, debug => 1);
my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick( my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick(
command => $self->{sqlplus_cmd}, command => $self->{sqlplus_cmd},
arguments => [@{$self->{args}}, '@', $tempfile], arguments => [@{$self->{args}}, '@', $tempfile],
@ -242,8 +242,8 @@ exit;";
wait_exit => 1, wait_exit => 1,
redirect_stderr => 1 redirect_stderr => 1
); );
$self->{output}->output_add(long_msg=>"REQ. STDOUT: '$stdout'\n"); $self->{output}->output_add(long_msg => "REQ. STDOUT: '$stdout'", debug => 1);
$self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n"); $self->{output}->output_add(long_msg => "REQ. EXIT_CODE: $exit_code", debug => 1);
# search oracle error lines # 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+)/);
@ -276,7 +276,7 @@ sub connect {
$self->{version} = $self->fetchrow_array(); $self->{version} = $self->fetchrow_array();
$self->{output}->output_add(long_msg=>"VERSION: ".$self->{version}."\n"); $self->{output}->output_add(long_msg => "VERSION: " . $self->{version}, debug => 1);
return 0; return 0;
} }
@ -285,14 +285,14 @@ sub fetchall_arrayref {
my $array_ref = []; my $array_ref = [];
if($self->{stdout} eq '') { if($self->{stdout} eq '') {
$self->{output}->output_add(long_msg=>"fetchall_arrayref: no data returned (no rows selected)\n"); $self->{output}->output_add(long_msg => "fetchall_arrayref: no data returned (no rows selected)", debug => 1);
return $array_ref; return $array_ref;
} }
if (!defined($self->{columns})) { if (!defined($self->{columns})) {
$self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//;
my $line = $1; my $line = $1;
$self->{output}->output_add(long_msg=>"fetchall_arrayref COLUMNS: $line\n") if(defined($line)); $self->{output}->output_add(long_msg => "fetchall_arrayref COLUMNS: $line", debug => 1) if(defined($line));
@{$self->{columns}} = split(/#&!#/, $line); @{$self->{columns}} = split(/#&!#/, $line);
map { s/^\s+|\s+$//g; } @{$self->{columns}}; map { s/^\s+|\s+$//g; } @{$self->{columns}};
$self->{stdout} =~ s/[\-#&!]+(\n|$)//; $self->{stdout} =~ s/[\-#&!]+(\n|$)//;
@ -303,7 +303,7 @@ sub fetchall_arrayref {
$line =~ s/#&!#\s+/#&!#/g; $line =~ s/#&!#\s+/#&!#/g;
$line =~ s/\s+#&!#/#&!#/g; $line =~ s/\s+#&!#/#&!#/g;
$self->{output}->output_add(long_msg=>"fetchall_arrayref VALUE: ".$line."\n"); $self->{output}->output_add(long_msg => "fetchall_arrayref VALUE: " . $line, debug => 1);
push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))]; push @$array_ref, [map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line))];
} }
return $array_ref; return $array_ref;
@ -314,28 +314,28 @@ sub fetchrow_array {
my @array_result = (); my @array_result = ();
if($self->{stdout} eq '') { if($self->{stdout} eq '') {
$self->{output}->output_add("fetchrow_array: no data returned (no rows selected)\n"); $self->{output}->output_add("fetchrow_array: no data returned (no rows selected)", debug => 1);
return @array_result; return @array_result;
} }
if (!defined($self->{columns})) { if (!defined($self->{columns})) {
$self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//;
my $line = $1; my $line = $1;
$self->{output}->output_add(long_msg=>"fetchrow_array COLUMNS: $line\n"); $self->{output}->output_add(long_msg => "fetchrow_array COLUMNS: $line", debug => 1);
@{$self->{columns}} = split(/#&!#/, $line); @{$self->{columns}} = split(/#&!#/, $line);
map { s/^\s+|\s+$//g; } @{$self->{columns}}; map { s/^\s+|\s+$//g; } @{$self->{columns}};
$self->{stdout} =~ s/[\-#&!]+(\n|$)//; $self->{stdout} =~ s/[\-#&!]+(\n|$)//;
} }
$self->{output}->output_add(long_msg=>"fetchrow_array STDOUT: '".$self->{stdout}."'\n"); $self->{output}->output_add(long_msg => "fetchrow_array STDOUT: '" . $self->{stdout} . "'", debug => 1);
if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) { if (($self->{stdout} =~ s/^(.*?)(\n|$)//)) {
my $line = $1; my $line = $1;
$self->{output}->output_add(long_msg=>"fetchrow_array VALUE: '".$line."'\n"); $self->{output}->output_add(long_msg => "fetchrow_array VALUE: '" . $line . "'", debug => 1);
push @array_result, map({ s/\\n/\x{0a}/g; s/\\t/\x{09}/g; s/\\/\x{5c}/g; $_; } split(/#&!#/, $line)); 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; map { s/^\s+|\s+$//g; } @array_result;
$self->{output}->output_add(long_msg=>"ARRAY: ".Dumper(@array_result)."\n"); $self->{output}->output_add(long_msg => "ARRAY: " . Data::Dumper::Dumper(@array_result), debug => 1);
} }
$self->{output}->output_add(long_msg=>"RETURN: ".Dumper(@array_result)."\n"); $self->{output}->output_add(long_msg => "RETURN: " . Data::Dumper::Dumper(@array_result), debug => 1);
return scalar(@array_result) == 1 ? $array_result[0] : @array_result; return scalar(@array_result) == 1 ? $array_result[0] : @array_result;
} }
@ -344,21 +344,21 @@ sub fetchrow_hashref {
my $array_result = undef; my $array_result = undef;
if($self->{stdout} eq '') { if($self->{stdout} eq '') {
$self->{output}->output_add(long_msg=>"fetchrow_hashref: no data returned (no rows selected)\n"); $self->{output}->output_add(long_msg => "fetchrow_hashref: no data returned (no rows selected)", debug => 1);
return $array_result; return $array_result;
} }
if (!defined($self->{columns})) { if (!defined($self->{columns})) {
$self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//; $self->{stdout} =~ s/^\s*\n(.*?)(\n|$)//;
my $line = $1; my $line = $1;
$self->{output}->output_add(long_msg=>"fetchrow_hashref COLUMNS: $line\n"); $self->{output}->output_add(long_msg => "fetchrow_hashref COLUMNS: $line", debug => 1);
@{$self->{columns}} = split(/#&!#/, $line); @{$self->{columns}} = split(/#&!#/, $line);
map { s/^\s+|\s+$//g; } @{$self->{columns}}; map { s/^\s+|\s+$//g; } @{$self->{columns}};
$self->{stdout} =~ s/[\-#&!]+(\n|$)//; $self->{stdout} =~ s/[\-#&!]+(\n|$)//;
} }
if ($self->{stdout} ne '' && $self->{stdout} =~ s/^(.*?)(\n|$)//) { if ($self->{stdout} ne '' && $self->{stdout} =~ s/^(.*?)(\n|$)//) {
my $line = $1; my $line = $1;
$self->{output}->output_add(long_msg=>"fetchrow_hashref VALUE: ".$line."\n"); $self->{output}->output_add(long_msg => "fetchrow_hashref VALUE: " . $line, debug => 1);
$array_result = {}; $array_result = {};
my @values = split(/#&!#/, $line); my @values = split(/#&!#/, $line);
for (my $i = 0; $i < scalar(@values); $i++) { for (my $i = 0; $i < scalar(@values); $i++) {
@ -367,7 +367,7 @@ sub fetchrow_hashref {
$value =~ s/\\n/\x{0a}/g; $value =~ s/\\n/\x{0a}/g;
$value =~ s/\\t/\x{09}/g; $value =~ s/\\t/\x{09}/g;
$value =~ s/\\/\x{5c}/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'", debug => 1);
$array_result->{$self->{columns}[$i]} = $value; $array_result->{$self->{columns}[$i]} = $value;
} }
} }
@ -381,7 +381,7 @@ sub query {
$self->{columns} = undef; $self->{columns} = undef;
(my $exit_code, $self->{stdout}) = $self->command_execution(request => $options{query}); (my $exit_code, $self->{stdout}) = $self->command_execution(request => $options{query});
if ($exit_code != 0) { if ($exit_code != 0) {
$self->{output}->add_option_msg(short_msg => "Cannot execute query:\nQuery:".$options{query}."\nOutput: ". $self->{stdout}); $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{stdout});
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit});
} }
} }
@ -442,6 +442,6 @@ Exit code for DB Errors (default: unknown)
=head1 DESCRIPTION =head1 DESCRIPTION
B<snmp>. B<sql>.
=cut =cut