Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 260 lines (230 sloc) 7.73 KB
#!/usr/bin/perl
#
# check_backuppc: a Nagios plugin to check the status of BackupPC
#
# Tested against BackupPC 2.1.2 and Nagios 1.3
# <http://backuppc.sourceforge.net>
# <http://nagios.org>
#
# AUTHORS
# Seneca Cunningham <tetragon@users.sourceforge.net>
#
# COPYRIGHT
# Copyright (C) 2006,2007 Seneca Cunningham
#
# 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, 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# CHANGELOG
# Mar 31, 2010 -- Matt Edlefsen <matt@xforty.com>
# refactored code and added age checks
#
use strict;
no utf8;
# Nagios
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS $TIMEOUT);
use POSIX qw(strftime difftime);
use Getopt::Long;
Getopt::Long::Configure('bundling');
# BackupPC
use lib "/usr/share/backuppc/lib";
use BackupPC::Lib;
my $version = '1.2.0';
my $warnLevel = 0;
my $daysOld = 7;
my $ageWarningLevel = undef;
my $ageCriticalLevel = undef;
my $verbose = 0;
my $opt_V = 0;
my $opt_h = 0;
my $goodOpt = 0;
my $reduce = 0;
my $backupOnly = 0;
my $archiveOnly = 0;
my $statusOnly = 0;
my @hostsDesired;
my @hostsExcluded;
# Process options
$goodOpt = GetOptions(
'v+' => \$verbose, 'verbose+' => \$verbose,
'c=f' => \$daysOld, 'critical=f' => \$daysOld,
'w=f' => \$warnLevel, 'warning=f' => \$warnLevel,
'age-critical=f' => \$ageCriticalLevel, 'age-warning=f' => \$ageWarningLevel,
'V' => \$opt_V, 'version' => \$opt_V,
'h' => \$opt_h, 'help' => \$opt_h,
'r=i' => \$reduce, 'reduce' => \$reduce,
'b' => \$backupOnly, 'backup-only' => \$backupOnly,
'a' => \$archiveOnly, 'archive-only' => \$archiveOnly,
's' => \$statusOnly, 'status-only' => \$statusOnly,
'H=s' => \@hostsDesired, 'hostname=s' => \@hostsDesired,
'x=s' => \@hostsExcluded, 'exclude=s' => \@hostsExcluded);
@hostsDesired = () if $#hostsDesired < 0;
@hostsExcluded = () if $#hostsExcluded < 0;
if ($opt_V)
{
print "check_backuppc - " . $version . "\n";
exit $ERRORS{'OK'};
}
if ($backupOnly and $archiveOnly)
{
$goodOpt = 0;
print "Cannot apply both --backup-only and --archive-only, contradictory\n\n";
}
if ($opt_h or not $goodOpt)
{
print "check_backuppc - " . $version . "\n";
print "A Nagios plugin to check on BackupPC backup status.\n\n";
print "Options:\n";
print " --hostname,-H only check the specified host\n";
print " --exclude,-x do not check the specified host\n";
print " --archive-only,-a only check the archive hosts\n";
print " --backup-only,-b only check the backup hosts\n";
print " --status-only,-s only check backup status, omit connection failures that are\n";
print " less than \$Conf{FullPeriod} old\n";
print " --warning,-w days a host must be in an error state to cause a warning\n";
print " --critical,-c days a host must be in an error state to be critical\n";
print " --age-warning days old a hosts last backup must be to cause a warning\n";
print " --age-critical days old a hosts last backup must be to be critical\n";
print " --reduce,-r maximum number of failed hosts for severity reduction\n";
print " --verbose,-v increase verbosity\n";
print " --version,-V display plugin version\n";
print " --help,-h display this message\n\n";
exit $ERRORS{'OK'} if $goodOpt;
exit $ERRORS{'UNKNOWN'};
}
if ($warnLevel > $daysOld)
{
print("BACKUPPC UNKNOWN - Warning threshold must be <= critical\n");
exit $ERRORS{'UNKNOWN'};
}
if ($ageWarningLevel > $ageCriticalLevel)
{
print("BACKUPPC UNKNOWN - Age Warning threshold must be <= Age Critical\n");
exit $ERRORS{'UNKNOWN'};
}
# Connect to BackupPC
my $server;
if (!($server = BackupPC::Lib->new))
{
print "BACKUPPC CRITICAL - Couldn't connect to BackupPC\n";
exit $ERRORS{'CRITICAL'};
}
my %Conf = $server->Conf();
$server->ChildInit();
my $err = $server->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
if ($err)
{
print("BACKUPPC UNKNOWN - Can't connect to server ($err)\n");
exit $ERRORS{'UNKNOWN'};
}
# hashes that BackupPC uses for varios status info
my %Status;
my %Jobs;
my %Info;
# query the BackupPC server for host, job, and server info
my $info_raw = $server->ServerMesg('status info');
my $jobs_raw = $server->ServerMesg('status jobs');
my $status_raw = $server->ServerMesg('status hosts');
# undump the output... BackupPC uses Data::Dumper
eval $info_raw;
eval $jobs_raw;
eval $status_raw;
# check the dumped output
foreach my $host (@hostsDesired, @hostsExcluded)
{
if (not grep {/$host/} keys(%Status))
{
print("BACKUPPC UNKNOWN - Unknown host ($host)\n");
exit $ERRORS{'UNKNOWN'};
}
}
my @oks;
my @warnings;
my @criticals;
# host status checks
foreach my $host (sort(keys(%Status)))
{
next if $host =~ /^ /;
next if (@hostsDesired and not grep {/$host/} @hostsDesired);
next if (@hostsExcluded and grep {/$host/} @hostsExcluded);
next if ($backupOnly and $Status{$host}{'type'} eq 'archive');
next if ($archiveOnly and $Status{$host}{'type'} ne 'archive');
# Debug
if ($verbose == 3)
{
print "Host $host state " . $Status{$host}{'state'};
print " with error: " . $Status{$host}{'error'} . "\n";
}
$Status{$host}{'lastGoodBackupTime'} = $Status{$host}{'startTime'} if (not $Status{$host}{'lastGoodBackupTime'});
# Check if the host is in an error state
# Prevent is_error from being set if we're using status only, the error is a connection error, and it lasted
# less than a FullPeriod
my $is_error = $Status{$host}{'error'} && !($statusOnly &&
$Status{$host}{'error'} =~ /^ping too slow|no ping response|host not found$/ &&
($Status{$host}{'lastGoodBackupTime'} - $Status{$host}{'startTime'} > $Conf{FullPeriod} * 3600 * 24));
my $age = difftime(time(), $Status{$host}{'lastGoodBackupTime'});
my $msg = $is_error ? $Status{$host}{'error'} : int($age / 24 / 3600)." days since last backup";
my $host_info = [$host, $msg];
if( ( $is_error && $age > ($daysOld * 360 * 24) ) ||
( defined($ageCriticalLevel) && $age > ($ageCriticalLevel * 3600 * 24) ) )
{
push @criticals, $host_info;
$is_error = 1;
}
elsif( ( $is_error && $age > ($warnLevel * 360 * 24) ) ||
( defined($ageWarningLevel) && $age > ($ageWarningLevel * 3600 * 24) ) )
{
push @warnings, $host_info;
$is_error = 1;
}
else
{
push @oks, $host_info;
}
# Debug
if ($is_error && $verbose == 2)
{
print "Host $host state " . $Status{$host}{'state'};
print " with error: " . $Status{$host}{'error'} . "\n";
}
}
my $failures = @warnings + @criticals;
my $total = $failures + @oks;
my $code;
my $msg;
if ( $failures <= $reduce )
{
$code = 'OK';
}
elsif( @criticals <= $reduce )
{
$code = 'WARNING';
}
else
{
$code = 'CRITICAL';
}
if(! $verbose )
{
$msg = " ($failures/$total) failures";
}
else
{
$msg = '';
$msg .= ' criticals= '.join(', ', map( "$_->[0] ($_->[1])", @criticals ) ) if @criticals;
$msg .= ' warnings= '.join(', ', map( "$_->[0] ($_->[1])", @warnings ) ) if @warnings;
}
print "BACKUPPC $code -$msg\n";
exit $ERRORS{$code};