Last Succesful Backup Bareos – Nagios Check

example:

./check_bareos_last_backup.pl -c 3 -w 6 -j CopyToTape -O 192.168.59.211 -d bareos -u bareos_webui -p citestpass

source:

#!/usr/bin/perl -w

# Author: Marco Braidotti
# Quite a lot copied from check_bacula.pl which is GPL.

## examples: 

##    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 3 of the License, or
##    (at your option) any later version.
##
##    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 check_bareos_last_backup.pl.  If not, see <http://www.gnu.org/licenses/>.


use strict;
use POSIX;
use File::Basename;
use DBI;
use DBD::Pg;
use Switch;
use Getopt::Long;
use vars qw(
       $opt_help
           $opt_job
           $opt_critical
           $opt_warning
           $opt_level
           $opt_usage
           $opt_version
           $out
           $sql
           $date_start
           $date_stop
           $state
           $count
           );
           
sub print_help();
sub print_usage();
sub get_now();
sub get_date;

my $sql;
my $sq11;
my $sq12;
my $sql3;
my $sql4;
my $sqlresult;
my $days;
my $end;
my $client;
my $test;
my $clientresult;
my $level;
my $progname = basename($0);
my $progVers = "0.9";
my $sqlHost;
my $sqlDB;
my $sqlUsername;
my $sqlPassword;


my %ERRORS = (  'UNKNOWN'       =>      '3',
                'OK'            =>      '0',
                'WARNING'       =>      '1',
                'CRITICAL'      =>      '2');
                
Getopt::Long::Configure('bundling');
GetOptions
        (
        "c=s"   =>      \$opt_critical, "critical=s"    =>      \$opt_critical,
        "w=s"   =>      \$opt_warning,  "warning=s"     =>      \$opt_warning,
        ##"L=s"   =>      \$opt_level,    "level=s"       =>      \$opt_level,
        "j=s"   =>      \$opt_job,      "job=s"         =>      \$opt_job,
        "h"     =>      \$opt_help,     "help"          =>      \$opt_help,
                                        "usage"         =>      \$opt_usage,
    "O=s"     =>      \$sqlHost,  "hostname"       =>      \$sqlHost,
    "d=s"     =>      \$sqlDB,  "database"       =>      \$sqlDB,
    "u=s"     =>      \$sqlUsername,  "username"       =>      \$sqlUsername,
    "p=s"     =>      \$sqlPassword,  "password"       =>      \$sqlPassword,
        "V"     =>      \$opt_version,  "version"       =>      \$opt_version
        ) || die "Try '$progname --help' for more information.\n";

    
sub print_help() {
 print "\n";
 print "If Bacula holds its MySQL-data behind password, you have to manually enter the password into the script as variable \$sqlPassword.\n";
 print "And be sure to prevent everybody from reading it!\n";
 print "\n";
 print "Options:\n";
 ##print "L       check backup level F for full, D for differential, I for incremential\n";
 print "c       max days since successful job for not returning critical\n";
 print "w       max days since successful job for not returning warning\n";
 print "j       name of the job to NOT check (case-sensitive)\n";
 print "h       show this help\n";
 print "V       print script version\n";
 print "O       host postgresql\n";
 print "d       database postgresql\n";
 print "u       database user postgresql\n";
 print "p       database password postgresql\n";
}

sub print_usage() {
 print "Usage: $progname -c <critical> -w <warning> -j <job-name> [ -h ] [ -V ]\n";
}

if ($opt_help) {
 print_usage();
 print_help();
 exit $ERRORS{'UNKNOWN'};
}

if ($opt_usage) {
 print_usage();
 exit $ERRORS{'UNKNOWN'};
}

if ($opt_version) {
 print "$progname $progVers\n";
 exit $ERRORS{'UNKNOWN'};
}

##switch ($opt_level) {

##		case "F"	{ $level = "full";}
##		case "D"	{ $level = "differential";}
##		case "I"	{ $level = "incremental";}
##		else		{ print_usage(); exit $ERRORS{'UNKNOWN'}; }
##}

if ($opt_job && $opt_warning && $opt_critical) {
 my $dsn = "DBI:Pg:dbname=$sqlDB;host=$sqlHost";
 my $dbh = DBI->connect( $dsn,$sqlUsername,$sqlPassword ) or die "Error connecting to: '$dsn': $DBI::errstr\n";

##$sql = "SELECT  Datediff(NOW(), EndTime) as \'days\', EndTime AS \'end\' FROM `Job` WHERE Name = \"$opt_job\" and JobStatus='T' AND Level = \"$opt_level\" ORDER BY Datediff(NOW(), EndTime) ASC LIMIT 1;";

my $sql1 = "select DATE_PART('day', now() - EndTime) as days, t.endtime as end, t.name as client, JobStatus from job t right join ( select name, max(endtime) as maxdate from job where jobstatus = 'T' group by name ) tm on t.name = tm.name and t.endtime = tm.maxdate where t.name <> 'RestoreFiles' and t.name not in ('";
my $sql2 = "') order by days";

$sql = join "", $sql1, $opt_job, $sql2;

my $sth = $dbh->prepare($sql) or die "Error preparing statement",$dbh->errstr;
$sth->execute;

my @row;
while ( @row = $sth->fetchrow_array()) {
($days,$end,$client) = @row;
}
$sth->finish;
$dbh->disconnect();

if (! $end) { print "Bareos Backup UNKNOWN: Successful job for $opt_job - $days - $end not found.\n"; exit $ERRORS{'UNKNOWN'}; }

$state = 'OK';
if ($days > $opt_warning) { $state='WARNING' }
if ($days > $opt_critical) { $state='CRITICAL' }
if ($days < 0) { $state='UNKNOWN';} ;


 my $dsnresult = "DBI:Pg:dbname=$sqlDB;host=$sqlHost";
 my $dbhresult = DBI->connect( $dsnresult,$sqlUsername,$sqlPassword ) or die "Error connecting to: '$dsn': $DBI::errstr\n";


my $sql3 = "select '1' as test, array_to_string(array_agg(t.name),', ') as clientresult from job t right join ( select name, max(endtime) as maxdate from job where jobstatus = 'T' group by name ) tm on t.name = tm.name and t.endtime = tm.maxdate where DATE_PART('day', now() - EndTime) > 30 and t.name <> 'RestoreFiles' and t.name not in ('";
my $sql4 = "') group by test";

$sqlresult = join "", $sql3, $opt_job, $sql4;

my $sthresult = $dbhresult->prepare($sqlresult) or die "Error preparing statement",$dbhresult->errstr;
$sthresult->execute;

my @rowresult;
while ( @rowresult = $sthresult->fetchrow_array()) {
($test,$clientresult) = @rowresult;
}
$sthresult->finish;
$dbhresult->disconnect();


print "Backup Clients $state. Trascorsi almeno 30 giorni dall'ultimo backup effettuato con successo dei clients: $clientresult\n";
exit $ERRORS{$state};

}
else
{
print_help();
exit $ERRORS{'UNKNOWN'};
}

 

Rispondi