################################################################################ # Copyright 2005-2013 MERETHIS # Centreon is developped by : Julien Mathis and Romain Le Merlus under # GPL Licence 2.0. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation ; either version 2 of the License. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, see . # # Linking this program statically or dynamically with other modules is making a # combined work based on this program. Thus, the terms and conditions of the GNU # General Public License cover the whole combination. # # As a special exception, the copyright holders of this program give MERETHIS # permission to link this program with independent modules to produce an executable, # regardless of the license terms of these independent modules, and to copy and # distribute the resulting executable under terms of MERETHIS choice, provided that # MERETHIS also meet, for each linked independent module, the terms and conditions # of the license of that module. An independent module is a module which is not # derived from this program. If you modify this program, you may extend this # exception to your version of the program, but you are not obliged to do so. If you # do not wish to do so, delete this exception statement from your version. # # For more information : contact@centreon.com # Authors : Kevin Duret # #################################################################################### package database::oracle::mode::rmanbackupage; use base qw(centreon::plugins::mode); use strict; use warnings; use DateTime; 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 => { "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; $options{options}->add_options(arguments => { 'warning-' . $label . ':s' => { name => 'warning-' . $label }, 'critical-' . $label . ':s' => { name => 'critical-' . $label }, 'no-' . $label => { name => 'no-' . $label }, }); } return $self; } sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); foreach (('db incr', 'db full', 'archivelog', 'controlfile')) { my $label = $_; $label =~ s/ /-/g; foreach my $threshold (('warning', 'critical')) { if (($self->{perfdata}->threshold_validate(label => $threshold . '-' . $label, value => $self->{option_results}->{$threshold . '-' . $label})) == 0) { $self->{output}->add_option_msg(short_msg => "Wrong " . $threshold . '-' . $label . " threshold '" . $self->{option_results}->{warning_db_incr} . "'."); $self->{output}->option_exit(); } } } if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') { $ENV{TZ} = $self->{option_results}->{timezone}; } if (defined($self->{option_results}->{incremental_level})) { # the special request don't retrieve controlfiles. But controlfiles are saved with archivelog. $self->{option_results}->{'no-controlfile'} = 1; } } sub run { my ($self, %options) = @_; # $options{sql} = sqlmode object $self->{sql} = $options{sql}; $self->{sql}->connect(); my $query; if (defined($self->{option_results}->{incremental_level})) { $query = q{SELECT j.input_type as object_type, ((max(j.start_time) - date '1970-01-01')*24*60*60) as last_time, x.incremental_level FROM v$rman_backup_job_details j LEFT OUTER JOIN (SELECT d.session_recid, d.session_stamp, (case when sum(case when d.backup_type||d.incremental_level = 'I1' then 1 else 0 end) = 0 then 0 else 1 end) incremental_level FROM v$backup_set_details d JOIN V$backup_set s on s.set_stamp = d.set_stamp and s.set_count = d.set_count WHERE s.input_file_scan_only = 'NO' GROUP BY d.session_recid, d.session_stamp) x on x.session_recid = j.session_recid and x.session_stamp = j.session_stamp GROUP BY input_type, incremental_level ORDER BY last_time DESC}; } else { $query = q{SELECT object_type, ((max(start_time) - date '1970-01-01')*24*60*60) as last_time FROM v$rman_status WHERE operation='BACKUP' GROUP BY object_type}; } $self->{sql}->query(query => $query); my $result = $self->{sql}->fetchall_arrayref(); $self->{output}->output_add(severity => 'OK', short_msg => sprintf("Rman backup age are ok.")); my $count_backups = 0; my $already_checked = {}; foreach (('db incr', 'db full', 'archivelog', 'controlfile')) { my $executed = 0; my $label = $_; $label =~ s/ /-/g; foreach my $row (@$result) { next if (defined($already_checked->{$$row[0]})); if (defined($self->{option_results}->{incremental_level})) { # db incr with incremental level 0 = DB FULL if (/db full/ && $$row[0] =~ /db incr/i && defined($$row[2]) && $$row[2] == 0) { # it's a full. we get $$row[0] = 'DB FULL'; } else { next if (/db incr/ && $$row[0] =~ /db incr/i && defined($$row[2]) && $$row[2] == 0); # it's a full. we skip. next if ($$row[0] !~ /$_/i); } $already_checked->{$$row[0]} = 1; } else { next if ($$row[0] !~ /$_/i); } $count_backups++; $executed = 1; my ($type, $last_time) = @$row; next if (defined($self->{option_results}->{filter_type}) && $type !~ /$self->{option_results}->{filter_type}/); my @values = localtime($last_time); my $dt = DateTime->new( year => $values[5] + 1900, month => $values[4] + 1, day => $values[3], hour => $values[2], minute => $values[1], second => $values[0], time_zone => 'UTC', ); my $offset = $last_time - $dt->epoch; $last_time = $last_time + $offset; my $backup_age = time() - $last_time; my $backup_age_convert = centreon::plugins::misc::change_seconds(value => $backup_age); my $type_perfdata = $type; $type_perfdata =~ s/ /_/g; $self->{output}->output_add(long_msg => sprintf("Last Rman '%s' backups : %s", $type, $backup_age_convert)); $self->{output}->perfdata_add(label => sprintf('%s_backup_age', $type_perfdata), value => $backup_age, unit => 's', warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $label), critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $label), min => 0); my $exit_code = $self->{perfdata}->threshold_check(value => $backup_age, threshold => [ { label => 'critical-' . $label, exit_litteral => 'critical' }, { label => 'warning-' . $label, exit_litteral => 'warning' } ]); if (!$self->{output}->is_status(value => $exit_code, compare => 'ok', litteral => 1)) { $self->{output}->output_add(severity => $exit_code, short_msg => sprintf("Last Rman '%s' backups : %s", $type, $backup_age_convert)); } } if ($executed == 0 && !defined($self->{option_results}->{'no-' . $label})) { $self->{output}->output_add(severity => 'CRITICAL', short_msg => sprintf("Rman '%s' backups never executed", uc($_))); } } if (($count_backups == 0) && (!defined($self->{option_results}->{skip_no_backup}))) { $self->{output}->output_add(severity => 'CRITICAL', short_msg => sprintf("Rman backups never executed.")); } $self->{output}->display(); $self->{output}->exit(); } 1; __END__ =head1 MODE Check Oracle rman backup age. =over 8 =item B<--warning-*> Threshold warning in seconds. Can be: 'db-incr', 'db-full', 'archivelog', 'controlfile'. =item B<--critical-*> Threshold critical in seconds. Can be: 'db-incr', 'db-full', 'archivelog', 'controlfile'. =item B<--no-*> Skip error if never executed. Can be: 'db-incr', 'db-full', 'archivelog', 'controlfile'. =item B<--filter-type> Filter backup type. (type can be : 'DB INCR', 'DB FULL', 'ARCHIVELOG') =item B<--skip-no-backup> Return ok if no backup found. =item B<--timezone> Timezone of oracle server (If not set, we use current server execution timezone). =item B<--incremental-level> Please use the following option if your using incremental level 0 for full backup. =back =cut