diff --git a/database/oracle/mode/asmdiskgroupusage.pm b/database/oracle/mode/asmdiskgroupusage.pm index 9d287abf2..6e67272a3 100644 --- a/database/oracle/mode/asmdiskgroupusage.pm +++ b/database/oracle/mode/asmdiskgroupusage.pm @@ -20,73 +20,11 @@ package database::oracle::mode::asmdiskgroupusage; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; - -my $thresholds = { - diskgroup => [ - ['dismounted', 'OK'], - ['mounted', 'OK'], - ['connected', 'OK'], - ], -}; -my $instance_mode; - -my $maps_counters = { - dg => { - '000_status' => { threshold => 0, set => { - key_values => [ { name => 'state' } ], - closure_custom_calc => \&custom_status_calc, - closure_custom_output => \&custom_status_output, - closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => \&custom_status_threshold, - }, - }, - '001_offline-disks' => { threshold => 0, set => { - key_values => [ { name => 'offline_disks' }, { name => 'type' }, { name => 'display' } ], - closure_custom_calc => \&custom_offline_calc, - closure_custom_output => \&custom_offline_output, - closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => \&custom_offline_threshold, - }, - }, - '002_usage' => { set => { - key_values => [ { name => 'used' }, { name => 'total' }, { name => 'display' } ], - closure_custom_calc => \&custom_usage_calc, - closure_custom_output => \&custom_usage_output, - closure_custom_perfdata => \&custom_usage_perfdata, - closure_custom_threshold_check => \&custom_usage_threshold, - }, - }, - }, -}; - -sub custom_offline_threshold { - my ($self, %options) = @_; - my $status = 'ok'; - my $message; - - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - - if (defined($instance_mode->{option_results}->{critical_offline_disks}) && $instance_mode->{option_results}->{critical_offline_disks} ne '' && - eval "$instance_mode->{option_results}->{critical_offline_disks}") { - $status = 'critical'; - } elsif (defined($instance_mode->{option_results}->{warning_offline_disks}) && $instance_mode->{option_results}->{warning_offline_disks} ne '' && - eval "$instance_mode->{option_results}->{warning_offline_disks}") { - $status = 'warning'; - } - }; - if (defined($message)) { - $self->{output}->output_add(long_msg => 'filter offline status issue: ' . $message); - } - - return $status; -} +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); sub custom_offline_output { my ($self, %options) = @_; @@ -104,33 +42,28 @@ sub custom_offline_calc { return 0; } -sub custom_status_threshold { - my ($self, %options) = @_; - - my $message; - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - - $instance_mode->{last_status} = 0; - if (eval "$instance_mode->{check_status}") { - $instance_mode->{last_status} = 1; - } - }; - return $instance_mode->get_severity(section => 'diskgroup', value => $self->{result_values}->{state}); -} - sub custom_status_output { my ($self, %options) = @_; - my $msg = 'Status : ' . $self->{result_values}->{state}; - + + my $msg = 'status: ' . $self->{result_values}->{status}; return $msg; } +sub custom_status_threshold { + my ($self, %options) = @_; + + my $status = catalog_status_threshold($self, %options); + $self->{instance_mode}->{last_status} = 0; + if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { + $self->{instance_mode}->{last_status} = 1; + } + return $status; +} + sub custom_status_calc { my ($self, %options) = @_; - $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; return 0; } @@ -139,14 +72,18 @@ sub custom_usage_perfdata { my $label = 'used'; my $value_perf = $self->{result_values}->{used}; - if ($self->{result_values}->{total} > 0 && defined($instance_mode->{option_results}->{free})) { + if ($self->{result_values}->{total} > 0 && defined($self->{instance_mode}->{option_results}->{free})) { $label = 'free'; $value_perf = $self->{result_values}->{free}; } + + $label .= '_' . $self->{result_values}->{label_ref} + if (defined($self->{result_values}->{label_ref})); + my $extra_label = ''; $extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0); my %total_options = (); - if ($self->{result_values}->{total} > 0 && $instance_mode->{option_results}->{units} eq '%') { + if ($self->{result_values}->{total} > 0 && $self->{instance_mode}->{option_results}->{units} eq '%') { $total_options{total} = $self->{result_values}->{total}; $total_options{cast_int} = 1; } @@ -162,13 +99,13 @@ sub custom_usage_threshold { my ($self, %options) = @_; # cannot use '%' or free option with unlimited system - return 'ok' if ($self->{result_values}->{total} <= 0 && ($instance_mode->{option_results}->{units} eq '%' || $instance_mode->{option_results}->{free})); + return 'ok' if ($self->{result_values}->{total} <= 0 && ($self->{instance_mode}->{option_results}->{units} eq '%' || $self->{instance_mode}->{option_results}->{free})); my ($exit, $threshold_value); $threshold_value = $self->{result_values}->{used}; - $threshold_value = $self->{result_values}->{free} if (defined($instance_mode->{option_results}->{free})); - if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free})); + if ($self->{instance_mode}->{option_results}->{units} eq '%') { $threshold_value = $self->{result_values}->{prct_used}; - $threshold_value = $self->{result_values}->{prct_free} if (defined($instance_mode->{option_results}->{free})); + $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free})); } $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]); return $exit; @@ -177,28 +114,41 @@ sub custom_usage_threshold { sub custom_usage_output { my ($self, %options) = @_; + my $label = 'Disk '; + if (defined($self->{result_values}->{label_ref})) { + $label = 'Disk Failure'; + } + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); my $msg; if ($self->{result_values}->{total} <= 0) { - $msg = sprintf("Used: %s (unlimited)", $total_used_value . " " . $total_used_unit); + $msg = sprintf("%s Used: %s (unlimited)", $label, $total_used_value . " " . $total_used_unit); } else { my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); - $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $msg = sprintf("%s Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $label, $total_size_value . " " . $total_size_unit, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); } + return $msg; } sub custom_usage_calc { my ($self, %options) = @_; - return -10 if (defined($instance_mode->{last_status}) && $instance_mode->{last_status} == 0); + return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0); + + my $label_used = 'used'; + $label_used .= '_' . $options{extra_options}->{label_ref} + if (defined($options{extra_options}->{label_ref})); + + $self->{result_values}->{label_ref} = defined($options{extra_options}->{label_ref}) ? $options{extra_options}->{label_ref} : undef; $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; - $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; + $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_' . $label_used}; return 0 if ($self->{result_values}->{total} == 0); $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; @@ -208,204 +158,121 @@ sub custom_usage_calc { return 0; } +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'dg', type => 1, cb_prefix_output => 'prefix_dg_output', message_multiple => 'All diskgroups are ok', skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{dg} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'status' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_output'), + } + }, + { label => 'offline-disks', set => { + key_values => [ { name => 'offline_disks' }, { name => 'type' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_offline_calc'), + closure_custom_output => $self->can('custom_offline_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold, + } + }, + { label => 'usage', set => { + key_values => [ { name => 'used' }, { name => 'total' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + { label => 'usage-failure', set => { + key_values => [ { name => 'used_failure' }, { name => 'total' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'failure' }, + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +sub prefix_dg_output { + my ($self, %options) = @_; + + return "Diskgroup '" . $options{instance_value}->{display} . "' "; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning-offline-disks:s" => { name => 'warning_offline_disks', default => '(%{offline_disks} > 0 && %{type} eq "extern") || (%{offline_disks} > 1 && %{type} eq "high")' }, - "critical-offline-disks:s" => { name => 'critical_offline_disks', default => '%{offline_disks} > 0 && %{type} =~ /^normal|high$/' }, - "filter-name:s" => { name => 'filter_name', }, - "units:s" => { name => 'units', default => '%' }, - "free" => { name => 'free' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - "no-component:s" => { name => 'no_component' }, - }); + $options{options}->add_options(arguments => { + "unknown-status:s" => { name => 'unknown_status', default => '' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + "warning-offline-disks:s" => { name => 'warning_offline_disks', default => '(%{offline_disks} > 0 && %{type} eq "extern") || (%{offline_disks} > 1 && %{type} eq "high")' }, + "critical-offline-disks:s" => { name => 'critical_offline_disks', default => '%{offline_disks} > 0 && %{type} =~ /^normal|high$/' }, + "filter-name:s" => { name => 'filter_name', }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + }); - foreach my $key (('dg')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } - return $self; } -sub change_macros { - my ($self, %options) = @_; - - foreach (('warning_offline_disks', 'critical_offline_disks')) { - if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; - } - } - - $self->{check_status} = '%{state} =~ /^(mounted|dismounted|connected)$/i'; - $self->{check_status} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; -} - sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); - - foreach my $key (('dg')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } - - $instance_mode = $self; - - $self->change_macros(); - - if (defined($self->{option_results}->{no_component})) { - if ($self->{option_results}->{no_component} ne '') { - $self->{no_components} = $self->{option_results}->{no_component}; - } else { - $self->{no_components} = 'critical'; - } - } - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } -} + $self->SUPER::check_options(%options); -sub run { - my ($self, %options) = @_; - $self->{sql} = $options{sql}; - - $self->manage_selection(); - - my $multiple = 1; - if (scalar(keys %{$self->{dg}}) == 1) { - $multiple = 0; - } - - if ($multiple == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All diskgroups are ok'); - } - - foreach my $id (sort keys %{$self->{dg}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{dg}}) { - my $obj = $maps_counters->{dg}->{$_}->{obj}; - $obj->set(instance => $id); - - my ($value_check) = $obj->execute(values => $self->{dg}->{$id}); - next if ($value_check == -10); # status issue - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(level => 1, extra_instance => $multiple); - } - - $self->{output}->output_add(long_msg => "Diskgroup '$self->{dg}->{$id}->{display}' $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "Diskgroup '$self->{dg}->{$id}->{display}' $short_msg" - ); - } - - if ($multiple == 0) { - $self->{output}->output_add(short_msg => "Diskgroup '$self->{dg}->{$id}->{display}' $long_msg"); - } - } - - $self->{output}->display(); - $self->{output}->exit(); -} - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; + $self->change_macros(macros => [ + 'warning_offline_disks', 'critical_offline_disks', + 'warning_status', 'critical_status', 'unknown_status', + ]); } sub manage_selection { my ($self, %options) = @_; - $self->{sql}->connect(); - my $query = q{SELECT name, state, type, total_mb, usable_file_mb, offline_disks FROM V$ASM_DISKGROUP}; - $self->{sql}->query(query => $query); - my $result = $self->{sql}->fetchall_arrayref(); + $options{sql}->connect(); + my $query = q{SELECT name, state, type, total_mb, usable_file_mb, offline_disks, FREE_MB FROM V$ASM_DISKGROUP}; + $options{sql}->query(query => $query); + my $result = $options{sql}->fetchall_arrayref(); $self->{dg} = {}; foreach my $row (@$result) { - my ($name, $state, $type, $total_mb, $usable_file_mb, $offline_disks) = @$row; + my ($name, $state, $type, $total_mb, $usable_file_mb, $offline_disks, $free_mb) = @$row; if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $name !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "Skipping '" . $name . "': no matching filter name.", debug => 1); next; } - - - $self->{dg}->{$name} = { display => $name, total => $total_mb * 1024 * 1024, - used => ($total_mb * 1024 * 1024) - ($usable_file_mb * 1024 * 1024), - state => $state, type => lc($type), offline_disks => $offline_disks }; + + my $used_failure = ($total_mb * 1024 * 1024) - ($usable_file_mb * 1024 * 1024); + if ($usable_file_mb < 0) { + $used_failure = ($total_mb * 1024 * 1024); + } + $self->{dg}->{$name} = { + display => $name, + total => $total_mb * 1024 * 1024, + used => ($total_mb * 1024 * 1024) - ($free_mb * 1024 * 1024), + used_failure => $used_failure, + status => $state, + type => lc($type), + offline_disks => $offline_disks + }; } if (scalar(keys %{$self->{dg}}) <= 0) { - $self->{output}->output_add(severity => defined($self->{no_components}) ? $self->{no_components} : 'unknown', - short_msg => 'No components are checked.'); + $self->{output}->add_option_msg(short_msg => "No diskgroup found."); + $self->{output}->option_exit(); } } @@ -427,6 +294,29 @@ Threshold warning. Threshold critical. +=item B<--warning-usage-failure> + +Threshold warning. + +=item B<--critical-usage-failure> + +Threshold critical. + +=item B<--unknown-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{status}, %{display} + +=item B<--warning-status> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{status}, %{display} + +=item B<--critical-status> + +Set critical threshold for status (Default: ''). +Can used special variables like: %{status}, %{display} + =item B<--warning-offline-disks> Set warning threshold for offline disks (Default: '(%{offline_disks} > 0 && %{type} eq "extern") || (%{offline_disks} > 1 && %{type} eq "high")'). @@ -449,12 +339,6 @@ Thresholds are on free space left. Filter by name (regexp can be used). -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='diskgroup,CRITICAL,^(?!(mounted|connected|dismounted)$)' - =back =cut diff --git a/database/oracle/mode/tnsping.pm b/database/oracle/mode/tnsping.pm index 4b44a00c2..093325535 100644 --- a/database/oracle/mode/tnsping.pm +++ b/database/oracle/mode/tnsping.pm @@ -66,7 +66,7 @@ sub run { $self->{output}->output_add(severity => 'CRITICAL', short_msg => $msg_error); } else { - my ($sid) = $self->{sql}->{data_source} =~ /sid=(\S+)/; + my ($sid) = $self->{sql}->{data_source} =~ /(?:sid|service_name)=(\S+)/; $self->{output}->output_add(severity => 'OK', short_msg => sprintf("Connection established to listener '%s'.", $sid)); } diff --git a/database/oracle/plugin.pm b/database/oracle/plugin.pm index 595711068..f97779416 100644 --- a/database/oracle/plugin.pm +++ b/database/oracle/plugin.pm @@ -63,13 +63,13 @@ sub new { sub init { my ($self, %options) = @_; - $self->{options}->add_options( - arguments => { - 'hostname:s@' => { name => 'hostname' }, - 'port:s@' => { name => 'port' }, - 'sid:s' => { name => 'sid' }, - } - ); + $self->{options}->add_options(arguments => { + 'hostname:s@' => { name => 'hostname' }, + 'port:s@' => { name => 'port' }, + 'sid:s' => { name => 'sid' }, + 'servicename:s' => { name => 'servicename' }, + }); + $self->{options}->parse_options(); my $options_result = $self->{options}->get_options(); $self->{options}->clean(); @@ -84,10 +84,14 @@ sub init { $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 '')) { + 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}; } + if (defined($options_result->{servicename}) && $options_result->{servicename} ne '') { + $self->{sqldefault}->{dbi}[$i]->{data_source} .= ';service_name=' . $options_result->{servicename}; + $self->{sqldefault}->{sqlpluscmd}[$i]->{service_name} = $options_result->{servicename}; + } } } $self->SUPER::init(%options); @@ -113,7 +117,11 @@ Database Server Port. =item B<--sid> -Database SID (SERVICE_NAME). +Database SID. + +=item B<--servicename> + +Database Service Name. =back diff --git a/database/oracle/sqlpluscmd.pm b/database/oracle/sqlpluscmd.pm index 8325e28b1..5fffcdce6 100644 --- a/database/oracle/sqlpluscmd.pm +++ b/database/oracle/sqlpluscmd.pm @@ -46,16 +46,17 @@ 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' }, + "sqlplus-cmd:s" => { name => 'sqlplus_cmd'}, + "oracle-home:s" => { name => 'oracle_home' }, + "tnsadmin-home:s" => { name => 'tnsadmin_home' }, + "tnsnames-sid:s" => { name => 'tnsnames_sid'}, + "tnsnames-servicename:s" => { name => 'tnsnames_servicename'}, + "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' }, } ); } @@ -109,6 +110,7 @@ sub check_options { # return 0 = no data_source left $self->{sid} = defined($self->{option_results}->{sid}[0]) ? $self->{option_results}->{sid}[0]: $self->{option_results}->{tnsnames_sid}; + $self->{service_name} = defined($self->{option_results}->{service_name}[0]) ? $self->{option_results}->{service_name}[0]: $self->{option_results}->{tnsnames_servicename}; $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}; @@ -117,9 +119,9 @@ sub check_options { $self->{output}->output_add(long_msg => "*** DEBUG MODE****\n", debug => 1); $self->{output}->output_add(long_msg => Data::Dumper::Dumper($self->{option_results}), debug => 1); - # check the SID prerequisite option - if (!defined($self->{sid}) || $self->{sid} eq '') { - $self->{output}->add_option_msg(short_msg => "Need to specify sid argument."); + if ((!defined($self->{sid}) || $self->{sid} eq '') && + (!defined($self->{service_name}) || $self->{service_name} eq '')) { + $self->{output}->add_option_msg(short_msg => "Need to specify sid or servicename argument."); $self->{output}->option_exit(exit_litteral => $self->{option_results}->{sql_errors_exit}); } @@ -131,12 +133,12 @@ sub check_options { # construct the TNSADMIN variable if not available 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 if(!$self->{sqlplus_cmd}) { - $self->{sqlplus_cmd} = $self->{oracle_home}."/bin/sqlplus"; + $self->{sqlplus_cmd} = $self->{oracle_home} . "/bin/sqlplus"; } $self->{args} = ['-L', '-S']; @@ -155,17 +157,25 @@ sub check_options { 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}; + $connection_string .= "\@//" . $self->{option_results}->{hostname}[0] . ":" . $port . "/" . (defined($self->{sid}) && $self->{sid} ne '') ? $self->{sid} : $self->{service_name}); } else { - $connection_string .= "\@" . $self->{sid}; + $connection_string .= "\@" . (defined($self->{sid}) && $self->{sid} ne '') ? $self->{sid} : $self->{service_name}); } } else { $self->{output}->output_add(long_msg => "*** LOCAL CONNEXION MODE****", debug => 1); - $ENV{ORACLE_SID} = $self->{sid}; + if (defined($self->{sid}) && $self->{sid} ne '') { + $ENV{ORACLE_SID} = $self->{sid}; + } else { + $ENV{TWO_TASK} = '/' . $self->{service_name}; + } } # register a false data_source to be compliant with tnsping mode - $self->{data_source} = "sid=" . $self->{sid}; + if (defined($self->{sid}) && $self->{sid} ne '') { + $self->{data_source} = "sid=" . $self->{sid}; + } else { + $self->{data_source} = "service_name=" . $self->{service_name}; + } push @{$self->{args}}, $connection_string; @@ -415,7 +425,11 @@ Oracle TNS Admin Home. Where to locate tnsnames.ora file (default: ${ORACLE_HOME =item B<--tnsnames-sid> -Oracle SID defined in tnsnames.ora. +Oracle SID defined in tnsnames.ora. + +=item B<--tnsnames-servicename> + +Oracle Service Name defined in tnsnames.ora. =item B<--username>