diff --git a/centreon/plugins/dbi.pm b/centreon/plugins/dbi.pm index e981a93e1..4cf583fc1 100644 --- a/centreon/plugins/dbi.pm +++ b/centreon/plugins/dbi.pm @@ -93,6 +93,7 @@ sub class_handle_ALRM { sub handle_ALRM { my $self = shift; + $self->disconnect(); $self->{output}->output_add(severity => $self->{sql_errors_exit}, short_msg => "Timeout"); $self->{output}->display(); @@ -196,6 +197,12 @@ sub set_version { $self->{version} = $self->{instance}->get_info(18); # SQL_DBMS_VER } + +sub disconnect { + my ($self) = @_; + + $self->{instance}->disconnect if (defined($self->{instance})); +} sub connect { my ($self, %options) = @_; @@ -213,7 +220,7 @@ sub connect { "DBI:". $self->{data_source}, $self->{username}, $self->{password}, - { "RaiseError" => 0, "PrintError" => 0, "AutoCommit" => 1, %{$self->{connect_options_hash}} } + { RaiseError => 0, PrintError => 0, AutoCommit => 1, %{$self->{connect_options_hash}} } ); alarm(0) if (defined($self->{timeout})); @@ -267,6 +274,7 @@ sub query { $self->{statement_handle} = $self->{instance}->prepare($options{query}); if (!defined($self->{statement_handle})) { return 1 if ($continue_error == 1); + $self->disconnect(); $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{instance}->errstr); $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); } @@ -274,6 +282,7 @@ sub query { my $rv = $self->{statement_handle}->execute; if (!$rv) { return 1 if ($continue_error == 1); + $self->disconnect(); $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{statement_handle}->errstr); $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); } diff --git a/database/mysql/mysqlcmd.pm b/database/mysql/mysqlcmd.pm index b2d21d30d..1653a90ea 100644 --- a/database/mysql/mysqlcmd.pm +++ b/database/mysql/mysqlcmd.pm @@ -164,7 +164,6 @@ sub get_id { return $msg; } - sub get_unique_id4save { my ($self, %options) = @_; @@ -203,6 +202,8 @@ sub command_execution { return ($exit_code, $stdout); } +sub disconnect {} + # Connection initializer sub connect { my ($self, %options) = @_; diff --git a/database/oracle/mode/asmdiskgroupusage.pm b/database/oracle/mode/asmdiskgroupusage.pm index f2994e851..1940fb89f 100644 --- a/database/oracle/mode/asmdiskgroupusage.pm +++ b/database/oracle/mode/asmdiskgroupusage.pm @@ -245,6 +245,7 @@ sub manage_selection { 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(); + $options{sql}->disconnect(); $self->{dg} = {}; foreach my $row (@$result) { @@ -252,7 +253,7 @@ sub manage_selection { 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); + $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter name.", debug => 1); next; } diff --git a/database/oracle/mode/connectedusers.pm b/database/oracle/mode/connectedusers.pm index e9a98423f..46dae0008 100644 --- a/database/oracle/mode/connectedusers.pm +++ b/database/oracle/mode/connectedusers.pm @@ -31,11 +31,10 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + }); return $self; } @@ -62,6 +61,7 @@ sub run { $self->{sql}->connect(); $self->{sql}->query(query => q{SELECT COUNT(*) FROM v$session WHERE type = 'USER'}); my $users = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); my $exit_code = $self->{perfdata}->threshold_check(value => $users, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit_code, diff --git a/database/oracle/mode/corruptedblocks.pm b/database/oracle/mode/corruptedblocks.pm index 35891aef9..6c3cf9e69 100644 --- a/database/oracle/mode/corruptedblocks.pm +++ b/database/oracle/mode/corruptedblocks.pm @@ -31,11 +31,10 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + }); return $self; } @@ -62,6 +61,7 @@ sub run { $self->{sql}->connect(); $self->{sql}->query(query => q{SELECT COUNT(*) FROM v$database_block_corruption}); my $corrupted_blocks = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); my $exit_code = $self->{perfdata}->threshold_check(value => $corrupted_blocks, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit_code, diff --git a/database/oracle/mode/datacachehitratio.pm b/database/oracle/mode/datacachehitratio.pm index c6ec0e51d..325aab0a7 100644 --- a/database/oracle/mode/datacachehitratio.pm +++ b/database/oracle/mode/datacachehitratio.pm @@ -86,6 +86,7 @@ sub manage_selection { $options{sql}->connect(); $options{sql}->query(query => $query); my @result = $options{sql}->fetchrow_array(); + $options{sql}->disconnect(); $self->{global} = { physical_reads => $result[0], diff --git a/database/oracle/mode/datafilesstatus.pm b/database/oracle/mode/datafilesstatus.pm index 35756b569..9b2625c2b 100644 --- a/database/oracle/mode/datafilesstatus.pm +++ b/database/oracle/mode/datafilesstatus.pm @@ -122,6 +122,7 @@ sub manage_selection { $options{sql}->query(query => "SELECT file_name, tablespace_name, status FROM dba_data_files"); } my $result = $options{sql}->fetchall_arrayref(); + $options{sql}->disconnect(); $self->{df} = {}; foreach my $row (@$result) { diff --git a/database/oracle/mode/eventwaitsusage.pm b/database/oracle/mode/eventwaitsusage.pm index a897f1ea6..a1982bbc7 100644 --- a/database/oracle/mode/eventwaitsusage.pm +++ b/database/oracle/mode/eventwaitsusage.pm @@ -184,7 +184,9 @@ sub manage_selection { if ($self->{sql}->is_version_minimum(version => '10')) { $self->event_count_and_details(); } - + + $self->{sql}->disconnect(); + if (scalar(keys %{$self->{event}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No event found."); $self->{output}->option_exit(); diff --git a/database/oracle/mode/invalidobject.pm b/database/oracle/mode/invalidobject.pm index 156d88bd8..5c51ea251 100644 --- a/database/oracle/mode/invalidobject.pm +++ b/database/oracle/mode/invalidobject.pm @@ -82,11 +82,10 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "filter-message:s" => { name => 'filter_message' }, - "retention-objects:s" => { name => 'retention_objects', default => 3}, - }); + $options{options}->add_options(arguments => { + "filter-message:s" => { name => 'filter_message' }, + "retention-objects:s" => { name => 'retention_objects', default => 3 }, + }); return $self; } @@ -162,6 +161,8 @@ sub manage_selection { WHERE status <> 'VALID' AND status <> 'OPTION OFF' }); } + + $options{sql}->disconnect(); } 1; diff --git a/database/oracle/mode/listasmdiskgroups.pm b/database/oracle/mode/listasmdiskgroups.pm index 40f692750..105f1132f 100644 --- a/database/oracle/mode/listasmdiskgroups.pm +++ b/database/oracle/mode/listasmdiskgroups.pm @@ -58,6 +58,8 @@ SELECT name, state, type FROM V$ASM_DISKGROUP type => $row->[2], }; } + + $self->{sql}->disconnect(); } sub run { diff --git a/database/oracle/mode/longqueries.pm b/database/oracle/mode/longqueries.pm index 761069ff9..6aea83f79 100644 --- a/database/oracle/mode/longqueries.pm +++ b/database/oracle/mode/longqueries.pm @@ -75,13 +75,12 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning-status:s" => { name => 'warning_status', default => '' }, - "critical-status:s" => { name => 'critical_status', default => '' }, - "memory" => { name => 'memory' }, - "timezone:s" => { name => 'timezone' }, - }); + $options{options}->add_options(arguments => { + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '' }, + "memory" => { name => 'memory' }, + "timezone:s" => { name => 'timezone' }, + }); centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'DateTime', error_msg => "Cannot load module 'DateTime'."); @@ -164,6 +163,8 @@ sub manage_selection { if (defined($self->{option_results}->{memory})) { $self->{statefile_cache}->write(data => { last_time => $current_time }); } + + $self->{sql}->disconnect(); } 1; diff --git a/database/oracle/mode/passwordexpiration.pm b/database/oracle/mode/passwordexpiration.pm index 2f41b8898..407c8e30f 100644 --- a/database/oracle/mode/passwordexpiration.pm +++ b/database/oracle/mode/passwordexpiration.pm @@ -133,6 +133,8 @@ sub manage_selection { }; $i++; } + + $self->{sql}->disconnect(); } 1; diff --git a/database/oracle/mode/processusage.pm b/database/oracle/mode/processusage.pm index 30fdd6f24..2472fc187 100644 --- a/database/oracle/mode/processusage.pm +++ b/database/oracle/mode/processusage.pm @@ -31,11 +31,10 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + }); return $self; } @@ -62,6 +61,7 @@ sub run { $self->{sql}->connect(); $self->{sql}->query(query => q{SELECT current_utilization/limit_value*100 FROM v$resource_limit WHERE resource_name = 'processes'}); my $session = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); my $exit_code = $self->{perfdata}->threshold_check(value => $session, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit_code, diff --git a/database/oracle/mode/rmanbackupage.pm b/database/oracle/mode/rmanbackupage.pm index afafb33a9..6e5553fca 100644 --- a/database/oracle/mode/rmanbackupage.pm +++ b/database/oracle/mode/rmanbackupage.pm @@ -32,13 +32,13 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "skip-no-backup" => { name => 'skip_no_backup', }, - "filter-type:s" => { name => 'filter_type', }, - "timezone:s" => { name => 'timezone', }, - "incremental-level" => { name => 'incremental_level', }, - }); + $options{options}->add_options(arguments => { + "skip-no-backup" => { name => 'skip_no_backup', }, + "filter-type:s" => { name => 'filter_type', }, + "timezone:s" => { name => 'timezone', }, + "incremental-level" => { name => 'incremental_level', }, + }); + foreach (('db incr', 'db full', 'archivelog', 'controlfile')) { my $label = $_; $label =~ s/ /-/g; @@ -101,6 +101,7 @@ sub run { } $self->{sql}->query(query => $query); my $result = $self->{sql}->fetchall_arrayref(); + $self->{sql}->disconnect(); $self->{output}->output_add(severity => 'OK', short_msg => sprintf("Rman backup age are ok.")); diff --git a/database/oracle/mode/rmanbackupproblems.pm b/database/oracle/mode/rmanbackupproblems.pm index 550a656aa..071cc388f 100644 --- a/database/oracle/mode/rmanbackupproblems.pm +++ b/database/oracle/mode/rmanbackupproblems.pm @@ -31,12 +31,11 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "retention:s" => { name => 'retention', default => 3}, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + "retention:s" => { name => 'retention', default => 3 }, + }); return $self; } @@ -65,6 +64,7 @@ sub run { my $query = q{SELECT COUNT(*) FROM v$rman_status WHERE operation = 'BACKUP' AND status != 'COMPLETED' AND status != 'RUNNING' AND start_time > sysdate-} . $retention; $self->{sql}->query(query => $query); my $rman_backup_problems = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); my $exit_code = $self->{perfdata}->threshold_check(value => $rman_backup_problems, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit_code, diff --git a/database/oracle/mode/rmanonlinebackupage.pm b/database/oracle/mode/rmanonlinebackupage.pm index f07651c53..8578e68de 100644 --- a/database/oracle/mode/rmanonlinebackupage.pm +++ b/database/oracle/mode/rmanonlinebackupage.pm @@ -32,12 +32,11 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "timezone:s" => { name => 'timezone', }, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + "timezone:s" => { name => 'timezone', }, + }); return $self; } @@ -72,6 +71,7 @@ sub run { }; $self->{sql}->query(query => $query); my $result = $self->{sql}->fetchall_arrayref(); + $self->{sql}->disconnect(); $self->{output}->output_add(severity => 'OK', short_msg => sprintf("Backup online modes are ok.")); diff --git a/database/oracle/mode/rollbacksegmentusage.pm b/database/oracle/mode/rollbacksegmentusage.pm index 53f62c1d3..2b6b39b64 100644 --- a/database/oracle/mode/rollbacksegmentusage.pm +++ b/database/oracle/mode/rollbacksegmentusage.pm @@ -110,9 +110,9 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - }); + $options{options}->add_options(arguments => { + }); + return $self; } @@ -160,6 +160,7 @@ sub manage_selection { $self->{cache_name} = "oracle_" . $self->{mode} . '_' . $self->{sql}->get_unique_id4save() . '_' . (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + $self->{sql}->disconnect(); } 1; diff --git a/database/oracle/mode/sessionusage.pm b/database/oracle/mode/sessionusage.pm index 4bd67306e..6661a078e 100644 --- a/database/oracle/mode/sessionusage.pm +++ b/database/oracle/mode/sessionusage.pm @@ -31,11 +31,10 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + }); return $self; } @@ -62,6 +61,7 @@ sub run { $self->{sql}->connect(); $self->{sql}->query(query => q{SELECT current_utilization/limit_value*100 FROM v$resource_limit WHERE resource_name = 'sessions'}); my $session = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); my $exit_code = $self->{perfdata}->threshold_check(value => $session, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit_code, diff --git a/database/oracle/mode/tablespaceusage.pm b/database/oracle/mode/tablespaceusage.pm index 62737756b..effcac3be 100644 --- a/database/oracle/mode/tablespaceusage.pm +++ b/database/oracle/mode/tablespaceusage.pm @@ -348,6 +348,7 @@ sub manage_selection { } $self->{sql}->query(query => $query); my $result = $self->{sql}->fetchall_arrayref(); + $self->{sql}->disconnect(); $self->{tablespace} = {}; diff --git a/database/oracle/mode/temptablespace.pm b/database/oracle/mode/temptablespace.pm index 2f85ec104..7228ad317 100644 --- a/database/oracle/mode/temptablespace.pm +++ b/database/oracle/mode/temptablespace.pm @@ -42,11 +42,13 @@ sub custom_usage_perfdata { $total_options{cast_int} = 1; } - $self->{output}->perfdata_add(label => $label, - value => $value_perf, unit => 'B', - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), - min => 0, max => $self->{result_values}->{total}); + $self->{output}->perfdata_add( + label => $label, + value => $value_perf, unit => 'B', + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), + min => 0, max => $self->{result_values}->{total} + ); } sub custom_usage_threshold { @@ -159,6 +161,8 @@ sub manage_selection { while (my $result = $self->{sql}->fetchrow_hashref()) { $self->{tmptablespace}->{$result->{TABLESPACE}} = { used => $result->{B_USED}, total => $result->{B_TOTAL}, display => lc $result->{TABLESPACE} }; } + + $self->{sql}->disconnect(); } 1; diff --git a/database/oracle/mode/tnsping.pm b/database/oracle/mode/tnsping.pm index 093325535..0867fd90a 100644 --- a/database/oracle/mode/tnsping.pm +++ b/database/oracle/mode/tnsping.pm @@ -71,6 +71,7 @@ sub run { short_msg => sprintf("Connection established to listener '%s'.", $sid)); } + $self->{sql}->disconnect(); $self->{output}->display(); $self->{output}->exit(); } diff --git a/database/oracle/mode/undotablespace.pm b/database/oracle/mode/undotablespace.pm index 8c543d096..e034bbd26 100644 --- a/database/oracle/mode/undotablespace.pm +++ b/database/oracle/mode/undotablespace.pm @@ -166,6 +166,8 @@ group by a.tablespace_name while (my $result = $self->{sql}->fetchrow_hashref()) { $self->{undotablespace}->{$result->{TABLESPACE_NAME}} = { used => $result->{USED_BYTES}, total => $result->{TOTAL_BYTES}, display => lc $result->{TABLESPACE_NAME} }; } + + $self->{sql}->disconnect(); } 1; diff --git a/database/oracle/sqlpluscmd.pm b/database/oracle/sqlpluscmd.pm index 5fffcdce6..2d2847023 100644 --- a/database/oracle/sqlpluscmd.pm +++ b/database/oracle/sqlpluscmd.pm @@ -269,6 +269,8 @@ exit;"; return ($exit_code, $stdout); } +sub disconnect {} + # Connection initializer sub connect { my ($self, %options) = @_; diff --git a/database/postgres/psqlcmd.pm b/database/postgres/psqlcmd.pm index 861d8d963..5d5d02912 100644 --- a/database/postgres/psqlcmd.pm +++ b/database/postgres/psqlcmd.pm @@ -170,7 +170,6 @@ sub get_id { return $msg; } - sub get_unique_id4save { my ($self, %options) = @_; @@ -209,6 +208,8 @@ sub command_execution { return ($exit_code, $stdout); } +sub disconnect {} + # Connection initializer sub connect { my ($self, %options) = @_;