Replace tab by spaces

This commit is contained in:
jerome.bolliet@capgemini.com 2018-05-21 12:42:05 +02:00
parent 4437679706
commit 2cc75e7c65

View File

@ -46,20 +46,20 @@ sub new {
} }
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options( $options{options}->add_options(
arguments => { arguments => {
"sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, "sqlplus-cmd:s" => { name => 'sqlplus_cmd'},
"oracle-home:s" => { name => 'oracle_home' }, "oracle-home:s" => { name => 'oracle_home' },
"tnsadmin-home:s" => { name => 'tnsadmin_home' }, "tnsadmin-home:s" => { name => 'tnsadmin_home' },
"tnsnames-sid:s" => { name => 'tnsnames_sid'}, "tnsnames-sid:s" => { name => 'tnsnames_sid'},
"username:s" => { name => 'username' }, "username:s" => { name => 'username' },
"password:s" => { name => 'password' }, "password:s" => { name => 'password' },
"local-connexion" => { name => 'local_connexion', default=>0 }, "local-connexion" => { name => 'local_connexion', default=>0 },
"sysdba" => { name => 'sysdba', default=>0 }, "sysdba" => { name => 'sysdba', default=>0 },
"sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' }, "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' },
"tempdir:s" => { name => 'tempdir', default => '/tmp' }, "tempdir:s" => { name => 'tempdir', default => '/tmp' },
} }
); );
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'sqlpluscmd OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'sqlpluscmd OPTIONS', once => 1);
$self->{output} = $options{output}; $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->{tnsadmin_home} = defined($self->{option_results}->{tnsadmin_home}) ? $self->{option_results}->{tnsadmin_home} : $ENV{'TNSADMIN'};
$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");
$self->{output}->output_add(long_msg=>Dumper($self->{option_results}));; $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 '') { if (!defined($self->{sid}) || $self->{sid} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify sid argument."); $self->{output}->add_option_msg(short_msg => "Need to specify sid argument.");
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); $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 '') { if (!defined($self->{oracle_home}) || $self->{oracle_home} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify oracle-home argument."); $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}); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit});
} }
# construct the TNSADMIN variable if not available # construct the TNSADMIN variable if not available
if(!defined($self->{tnsadmin_home})) { if(!defined($self->{tnsadmin_home})) {
$self->{tnsadmin_home} = $self->{oracle_home}."/network/admin"; $self->{tnsadmin_home} = $self->{oracle_home}."/network/admin";
} }
# check the SQLPLUS command to use # check the SQLPLUS command to use
if(!$self->{sqlplus_cmd}) { if(!$self->{sqlplus_cmd}) {
$self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus";
} }
$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****\n");
$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})) {
$connection_string=$self->{option_results}->{username}."/".$self->{option_results}->{password}; $connection_string=$self->{option_results}->{username}."/".$self->{option_results}->{password};
} else { } else {
$self->{output}->add_option_msg(short_msg => "Need to specify username/password arguments or sysdba option."); $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}); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit});
} }
if($self->{local_connexion} == 0) { if($self->{local_connexion} == 0) {
if(defined($self->{option_results}->{hostname})) { if(defined($self->{option_results}->{hostname})) {
my $port = defined($self->{option_results}->{port}) ? $self->{option_results}->{port}[0] : 1521; my $port = defined($self->{option_results}->{port}) ? $self->{option_results}->{port}[0] : 1521;
$connection_string .= "\@//".$self->{option_results}->{hostname}[0].":".$port."/".$self->{sid}; $connection_string .= "\@//".$self->{option_results}->{hostname}[0].":".$port."/".$self->{sid};
} else { } else {
$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****\n");
$ENV{ORACLE_SID} = $self->{sid}; $ENV{ORACLE_SID} = $self->{sid};
} }
# register a false data_source to be compliant with tnsping mode # register a false data_source to be compliant with tnsping mode
$self->{data_source}="sid=".$self->{sid}; $self->{data_source}="sid=".$self->{sid};
push @{$self->{args}}, $connection_string; push @{$self->{args}}, $connection_string;
# set oracle env variable # set oracle env variable
$ENV{ORACLE_HOME} = $self->{oracle_home}; $ENV{ORACLE_HOME} = $self->{oracle_home};
$ENV{TNSADMIN} = $self->{tnsadmin_home}; $ENV{TNSADMIN} = $self->{tnsadmin_home};
if (defined($self->{option_results}->{sid})) { if (defined($self->{option_results}->{sid})) {
return 0; return 0;
} }
@ -222,8 +222,8 @@ sub quote {
sub command_execution { sub command_execution {
my ($self, %options) = @_; 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}, TEMPLATE => "centreonOracle.".$self->{sid}.".XXXXXX", UNLINK => 1 );
print $fh "set echo off print $fh "set echo off
-- set heading off -- set heading off
set feedback off set feedback off
set linesize 16000 set linesize 16000
@ -232,9 +232,9 @@ set colsep '#&!#'
set numwidth 15 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))."\n");
$self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n"); $self->{output}->output_add(long_msg=>"*** REQUEST: ".$options{request}."\n");
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,12 +242,12 @@ 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'\n");
$self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n"); $self->{output}->output_add(long_msg=>"REQ. EXIT_CODE: $exit_code\n");
# 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+)/);
if ($exit_code <= -1000) { if ($exit_code <= -1000) {
if ($exit_code == -1000) { if ($exit_code == -1000) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(severity => 'UNKNOWN',
@ -275,8 +275,8 @@ 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}."\n");
return 0; return 0;
} }
@ -284,27 +284,27 @@ sub fetchall_arrayref {
my ($self, %options) = @_; my ($self, %options) = @_;
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)\n");
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\n") 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|$)//;
} }
foreach (split /\n/, $self->{stdout}) { foreach (split /\n/, $self->{stdout}) {
my $line = $_; my $line = $_;
$line =~ s/^\s+|\s+$//g; $line =~ s/^\s+|\s+$//g;
$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."\n");
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;
} }
@ -312,62 +312,62 @@ sub fetchall_arrayref {
sub fetchrow_array { sub fetchrow_array {
my ($self, %options) = @_; my ($self, %options) = @_;
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)\n");
return @array_result; 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");
} }
$self->{output}->output_add(long_msg=>"RETURN: ".Dumper(@array_result)."\n"); if (!defined($self->{columns})) {
return scalar(@array_result) == 1 ? $array_result[0] : @array_result ; $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 { sub fetchrow_hashref {
my ($self, %options) = @_; my ($self, %options) = @_;
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)\n");
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\n");
@{$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."\n");
$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++) {
my $value = $values[$i]; my $value = $values[$i];
$value =~ s/^\s+|\s+$//g; $value =~ s/^\s+|\s+$//g;
$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'\n");
$array_result->{$self->{columns}[$i]} = $value; $array_result->{$self->{columns}[$i]} = $value;
} }
} }